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