001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.processor;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Expression;
021    import org.apache.camel.Processor;
022    
023    /**
024     * A <a href="http://camel.apache.org/delayer.html">Delayer</a> which
025     * delays processing the exchange until the correct amount of time has elapsed
026     * using an expression to determine the delivery time.
027     * 
028     * @version $Revision: 836213 $
029     */
030    public class Delayer extends DelayProcessorSupport implements Traceable {
031        private Expression delay;
032        private long delayValue;
033    
034        public Delayer(Processor processor, Expression delay) {
035            super(processor);
036            this.delay = delay;
037        }
038    
039        @Override
040        public String toString() {
041            return "Delayer[" + delay + " to: " + getProcessor() + "]";
042        }
043    
044        public String getTraceLabel() {
045            return "delay[" + delay + "]";
046        }
047    
048        public Expression getDelay() {
049            return delay;
050        }
051    
052        public long getDelayValue() {
053            return delayValue;
054        }
055    
056        public void setDelay(Expression delay) {
057            this.delay = delay;
058        }
059    
060        // Implementation methods
061        // -------------------------------------------------------------------------
062    
063        /**
064         * Waits for an optional time period before continuing to process the
065         * exchange
066         */
067        protected void delay(Exchange exchange) throws Exception {
068            long time = 0;
069            if (delay != null) {
070                Long longValue = delay.evaluate(exchange, Long.class);
071                if (longValue != null) {
072                    delayValue = longValue;
073                    time = longValue;
074                } else {
075                    delayValue = 0;
076                }
077            }
078            if (time <= 0) {
079                // no delay
080                return;
081            }
082    
083            // now add the current time
084            time += defaultProcessTime(exchange);
085    
086            waitUntil(time, exchange);
087        }
088    
089        /**
090         * A Strategy Method to allow derived implementations to decide the current
091         * system time or some other default exchange property
092         */
093        protected long defaultProcessTime(Exchange exchange) {
094            return currentSystemTime();
095        }
096    
097    }