52.16. 关于生存时间
阅读上面的有关时钟时钟的上方。
当您使用 Camel 通过 Camel 对 JMS 进行请求/回复(InOut)时,Camel 会使用发送端的超时,这是 requestTimeout
选项的默认 20 秒。您可以通过设置更高的/低值来控制这一点。但是,在要发送的消息上仍然设置了生存时间值。这需要在系统间同步时钟。如果没有,您可能需要禁用要设置的实时值的时间。现在,可以使用 Camel 2.8 中的 disableTimeToLive
选项。因此,如果您将此选项设置为 disableTimeToLive=true
,则 Camel 在发送 JMS 消息时 不会将 任何时间设置为 live 值。但是 请求超时仍处于活动状态。例如,如果您通过 JMS 进行请求/回复,并且禁用了生存时间,则 Camel 仍将使用 20 秒( requestTimeout
选项)的超时。也可以配置该选项。因此,这两个选项 requestTimeout
和 disableTimeToLive
可让您在进行 request/reply 时进行精细的控制。
您可以在消息中提供标头来覆盖,并用作请求超时值,而不是端点配置的值。例如:
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");
在上面的路由中,我们有一个 endpoint configured requestTimeout
为 30 秒。因此,Camel 将等待 30 秒,以便回复消息回到栏队列。如果没有收到回复消息,则在 Exchange 上设置了 org.apache.camel.ExchangeTimedOutException
,并且 Camel 继续路由消息,然后因为异常而失败,Camel 的错误处理器响应响应。
如果要使用每个消息超时值,您可以使用键 org.apache.camel.component.jms.JmsConstants#JMS_REQUEST_TIMEOUT
设置标头,其常量值为 "CamelJmsRequestTimeout"
,其超时值为长类型。
例如,我们可以使用 bean 计算每个独立消息的超时值,如调用服务 bean 上的 "whatIs TheTimeout"
方法,如下所示:
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 不会 随时将消息的值设置为 live 值。您可以使用 timeToLive
选项配置值。例如,要指示 5 秒。设置 timeToLive=5000
。选项 disableTimeToLive
可用于强制禁用生存时间,也可以用于 InOnly messaging。requestTimeout
选项不用于 InOnly messaging。