28.15. 关于生存时间
先阅读关于已同步时钟的上面。
当您使用 Camel 通过 JMS 进行请求/恢复(InOut)时,Camel 使用发送者侧的超时,即 requestTimeout
选项中的 20 秒。您可以通过设置更高的/较低值来控制它。但是,生存时间值仍然在要发送的消息上设置。以便在系统之间同步时钟。如果没有,您可能要禁用设置的实时值。现在,可以使用 Camel 2.8 中的 disableTimeToLive
选项来实现。因此,如果您将此选项设置为 disableTimeToLive=true
,则 Camel 在发送 JMS 消息时 不会将 任何时间设置为实时值。但是 请求超时仍处于活动状态。例如,如果您对 JMS 进行请求/恢复,并且已禁用生存时间,则 Camel 仍会使用超时为 20 秒( requestTimeout
选项)。也可以为课程配置该选项。因此,两个选项 requestTimeout
和 disableTimeToLive
可让您在发出请求/回复时进行精细控制。
您可以在消息中提供标头来覆盖,并将其用作请求超时值,而不是端点配置的值。例如:
from("direct:someWhere") .to("jms:queue:foo?replyTo=bar&requestTimeout=30s") .to("bean:processReply");
from("direct:someWhere")
.to("jms:queue:foo?replyTo=bar&requestTimeout=30s")
.to("bean:processReply");
在上面的路由中,我们有一个端点配置的 requestTimeout
为 30 秒。因此,Camel 将等待 till 30 秒,以便该回复消息返回条队列。如果没有收到回复消息,则 Exchange 上设置了 org.apache.camel.ExchangeTimedOutException
,Camel 会继续路由消息,然后 Camel 的错误处理程序会响应。
如果要使用每个消息超时值,您可以使用键 org.apache.camel.component.jms.JmsConstants#JMS_REQUEST_TIMEOUT
设置标头,其具有恒定值 "CamelJmsRequestTimeout"
,其超时值为长类型。
例如,我们可以使用 bean 按各个消息计算超时值,如在服务 Bean 上调用 "whatIsTheTimeout"
方法,如下所示:
from("direct:someWhere") .setHeader("CamelJmsRequestTimeout", method(ServiceBean.class, "whatIsTheTimeout")) .to("jms:queue:foo?replyTo=bar&requestTimeout=30s") .to("bean:processReply");
from("direct:someWhere")
.setHeader("CamelJmsRequestTimeout", method(ServiceBean.class, "whatIsTheTimeout"))
.to("jms:queue:foo?replyTo=bar&requestTimeout=30s")
.to("bean:processReply");
当您使用 Camel 对 JMS 进行触发和忘记(InOut)时,Camel 默认 不会将 消息上的任何时间设置为实时值。您可以使用 timeToLive
选项配置值。例如,要指明 5 sec。,您可以设置 timeToLive=5000
。选项 disableTimeToLive
选项可用于强制禁用生存时间,以及仅限消息传递。requestTimeout
选项不用于 InOnly messaging。