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: 781923 $
029 */
030 public class Delayer extends DelayProcessorSupport {
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 // Implementation methods
044 // -------------------------------------------------------------------------
045
046 /**
047 * Waits for an optional time period before continuing to process the
048 * exchange
049 */
050 protected void delay(Exchange exchange) throws Exception {
051 long time = 0;
052 if (delay != null) {
053 Long longValue = delay.evaluate(exchange, Long.class);
054 if (longValue != null) {
055 time = longValue;
056 }
057 }
058 if (time <= 0) {
059 // no delay
060 return;
061 }
062
063 // now add the current time
064 time += defaultProcessTime(exchange);
065
066 waitUntil(time, exchange);
067 }
068
069 /**
070 * A Strategy Method to allow derived implementations to decide the current
071 * system time or some other default exchange property
072 */
073 protected long defaultProcessTime(Exchange exchange) {
074 return currentSystemTime();
075 }
076
077 }