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: 788621 $
029     */
030    public class Delayer extends DelayProcessorSupport implements Traceable {
031        private final Expression delay;
032    
033        public Delayer(Processor processor, Expression delay) {
034            super(processor);
035            this.delay = delay;
036        }
037    
038        @Override
039        public String toString() {
040            return "Delayer[" + delay + " to: " + getProcessor() + "]";
041        }
042    
043        public String getTraceLabel() {
044            return "Delayer[" + delay + "]";
045        }
046    
047        // Implementation methods
048        // -------------------------------------------------------------------------
049    
050        /**
051         * Waits for an optional time period before continuing to process the
052         * exchange
053         */
054        protected void delay(Exchange exchange) throws Exception {
055            long time = 0;
056            if (delay != null) {
057                Long longValue = delay.evaluate(exchange, Long.class);
058                if (longValue != null) {
059                    time = longValue;
060                }
061            }
062            if (time <= 0) {
063                // no delay
064                return;
065            }
066    
067            // now add the current time
068            time += defaultProcessTime(exchange);
069    
070            waitUntil(time, exchange);
071        }
072    
073        /**
074         * A Strategy Method to allow derived implementations to decide the current
075         * system time or some other default exchange property
076         */
077        protected long defaultProcessTime(Exchange exchange) {
078            return currentSystemTime();
079        }
080    
081    }