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. <p/> For example if you
027     * wish to delay JMS messages by 25 seconds from their publish time you could
028     * create an instance of this class with the expression
029     * <code>header("JMSTimestamp")</code> and a delay value of 25000L.
030     * 
031     * @version $Revision: 747062 $
032     */
033    public class Delayer extends DelayProcessorSupport {
034        private Expression timeExpression;
035        private long delay;
036    
037        public Delayer(Processor processor, Expression timeExpression, long delay) {
038            super(processor);
039            this.timeExpression = timeExpression;
040            this.delay = delay;
041        }
042    
043        @Override
044        public String toString() {
045            return "Delayer[on: " + timeExpression + " delay: " + delay + " to: " + getProcessor() + "]";
046        }
047    
048        // Properties
049        // -------------------------------------------------------------------------
050        public long getDelay() {
051            return delay;
052        }
053    
054        /**
055         * Sets the delay from the publish time; which is typically the time from
056         * the expression or the current system time if none is available
057         */
058        public void setDelay(long delay) {
059            this.delay = delay;
060        }
061    
062        // Implementation methods
063        // -------------------------------------------------------------------------
064    
065        /**
066         * Waits for an optional time period before continuing to process the
067         * exchange
068         */
069        protected void delay(Exchange exchange) throws Exception {
070            long time = 0;
071            if (timeExpression != null) {
072                Long longValue = timeExpression.evaluate(exchange, Long.class);
073                if (longValue != null) {
074                    time = longValue;
075                }
076            }
077            if (time <= 0) {
078                time = defaultProcessTime(exchange);
079            }
080    
081            time += delay;
082    
083            waitUntil(time, exchange);
084        }
085    
086        /**
087         * A Strategy Method to allow derived implementations to decide the current
088         * system time or some other default exchange property
089         */
090        protected long defaultProcessTime(Exchange exchange) {
091            return currentSystemTime();
092        }
093    
094    }