Delay
The Delay EIP is used for delaying messages during routing.
Options
The Delay eip supports 3 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
expression |
Required Expression to define how long time to wait (in millis). |
ExpressionDefinition |
|
asyncDelayed |
Enables asynchronous delay which means the thread will not block while delaying. |
true |
Boolean |
callerRunsWhenRejected |
Whether or not the caller should run the task when it was rejected by the thread pool. Is by default true. |
true |
Boolean |
executorService |
To use a custom Thread Pool if asyncDelay has been enabled. |
ExecutorService |
|
description |
Sets the description of this node. |
DescriptionDefinition |
Example
The example below will delay all messages received on seda:b 1 second before sending them to mock:result.
from("seda:b")
.delay(1000)
.to("mock:result");
And in XML:
<route>
<from uri="seda:b"/>
<delay>
<constant>1000</constant>
</delay>
<to uri="mock:result"/>
</route>
The delayed value can be a dynamic Expression.
For example to delay a random between 1 and 5 seconds, we can use the Simple language:
from("seda:b")
.delay(simple("${random(1000,5000)}"))
.to("mock:result");
And in XML DSL:
<route>
<from uri="seda:b"/>
<delay>
<simple>${random(1000,5000)}</simple>
</delay>
<to uri="mock:result"/>
</route>
You can also call a Bean Method to compute the delayed value from Java code:
from("activemq:foo")
.delay().method("someBean", "computeDelay")
.to("activemq:bar");
Then the bean would look something like this:
public class SomeBean {
public long computeDelay() {
long delay = 0;
// use java code to compute a delay value in millis
return delay;
}
}
Asynchronous delaying
You can let the Delayer use non-blocking asynchronous delaying,
which means Camel will use scheduled thread pool (ScheduledExecutorService
)
to schedule a task to be executed in the future.
This allows the caller thread to not block and be able to service other messages.
You use the asyncDelayed()
to enable the async behavior.
from("activemq:queue:foo")
.delay(1000).asyncDelayed()
.to("activemq:aDelayedQueue");
In XML DSL you use the asyncDelayed
attribute to enable the async mode:
<route>
<from uri="activemq:queue:foo"/>
<delay asyncDelayed="true">
<constant>1000</constant>
</delay>
<to uri="activemq:aDealyedQueue"/>
</route>