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 java.util.concurrent.ScheduledExecutorService;
020    
021    import org.apache.camel.CamelContext;
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Expression;
024    import org.apache.camel.Processor;
025    import org.apache.camel.Traceable;
026    
027    /**
028     * A <a href="http://camel.apache.org/delayer.html">Delayer</a> which
029     * delays processing the exchange until the correct amount of time has elapsed
030     * using an expression to determine the delivery time.
031     * <p/>
032     * This implementation will block while waiting.
033     *
034     * @version 
035     */
036    public class Delayer extends DelayProcessorSupport implements Traceable {
037        private Expression delay;
038        private long delayValue;
039    
040        public Delayer(CamelContext camelContext, Processor processor, Expression delay,
041                       ScheduledExecutorService executorService, boolean shutdownExecutorService) {
042            super(camelContext, processor, executorService, shutdownExecutorService);
043            this.delay = delay;
044        }
045    
046        @Override
047        public String toString() {
048            return "Delayer[" + delay + " to: " + getProcessor() + "]";
049        }
050    
051        public String getTraceLabel() {
052            return "delay[" + delay + "]";
053        }
054    
055        public Expression getDelay() {
056            return delay;
057        }
058    
059        public long getDelayValue() {
060            return delayValue;
061        }
062    
063        public void setDelay(Expression delay) {
064            this.delay = delay;
065        }
066    
067        // Implementation methods
068        // -------------------------------------------------------------------------
069    
070        protected long calculateDelay(Exchange exchange) {
071            long time = 0;
072            if (delay != null) {
073                Long longValue = delay.evaluate(exchange, Long.class);
074                if (longValue != null) {
075                    delayValue = longValue;
076                    time = longValue;
077                } else {
078                    delayValue = 0;
079                }
080            }
081            if (time <= 0) {
082                // no delay
083                return 0;
084            }
085    
086            return time;
087        }
088    
089    }