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