Delayer has been edited by Claus Ibsen (Jun 04, 2009).

(View changes)

Content:

Delayer

The Delayer Pattern allows you to delay the delivery of messages to some destination.

The Delayer in Camel 1.x works a bit differently than Camel 2.0 onwards. In Camel 1.x the _expression_ is used to calculate an absolute time in millis.
So if you want to wait 3 sec from now and want to use the _expression_ for that you have to set the absolute time as currentTimeInMillis() + 3000.
If you want to wait 3 sec use the delayTime in Spring DSL to set it.

In Camel 2.0 the _expression_ is a value in millis to wait from the current time, so the _expression_ should just be 3000.

See the Spring DSL samples for Delayer in Camel 1.x vs. Camel 2.0

Using the Fluent Builders

from("seda:a").delay().header("MyDelay").to("mock:result");

So the above example will delay all messages received on queue:a 3 seconds before sending them to mock:result.

You can of course use many different _expression_ languages such as XPath, XQuery, SQL or various Scripting Languages. You can just delay things a fixed amount of time from the point at which the delayer receives the message. For example to delay things 2 seconds

delayer(2000)

The above assume that the delivery order is maintained and that the messages are delivered in delay order. If you want to reorder the messages based on delivery time, you can use the Resequencer with this pattern. For example

from("activemq:someQueue").resequencer(header("MyDeliveryTime")).delay("MyRedeliveryTime").to("activemq:aDelayedQueue");

Camel 2.0 - Spring DSL

The sample below demonstrates the delay in Spring DSL:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
    <route>
        <from uri="seda:a"/>
        <delay>
            <header>MyDelay</header>
        </delay>
        <to uri="mock:result"/>
    </route>
    <route>
        <from uri="seda:b"/>
        <delay>
            <constant>1000</constant>
        </delay>
        <to uri="mock:result"/>
    </route>
</camelContext>

Camel 1.x - Spring DSL

The delayer is using slightly different names in Camel 1.x:

<delayer>
  <delayTime>3000</delayTime>
  </_expression_>
</delayer>

The empty tag </_expression_> is needed to fulfill the XSD validation as its an optional element and we use JAXB annotations to generated the XSD in Camel and some combinations is hard to auto generate with optional elements.

For further examples of this pattern in use you could look at the junit test case

Creating a custom delay

You can use an _expression_ to determine when to send a message using something like this

from("activemq:foo").
  delay().method("someBean", "computeDelay").
  to("activemq:bar");

then the bean would look like this...

public class SomeBean {
  public long computeDelay() { 
     long delay = 0;
     // use java code to compute a delay value in millis
     return delay;
 }
}

Using This Pattern

If you would like to use this EIP Pattern then please read the Getting Started, you may also find the Architecture useful particularly the description of Endpoint and URIs. Then you could try out some of the Examples first before trying this pattern out.

Reply via email to