57.16. 关于生存时间
阅读以上关于同步时钟的先读。
当您使用 Camel 通过 Camel 对 JMS 进行请求/回复(InOut)时,Camel 会使用发送端的超时,这是 requestTimeout 选项的默认 20 秒。您可以通过设置更高的/lower 值来控制它。但是,仍然会在要发送的消息上设置生存时间值。因此,这需要在系统间同步时钟。如果没有,您可能想禁用设置生存时间值。现在,可以使用 Camel 2.8 onwards 中的 disableTimeToLive 选项。因此,如果您将此选项设置为 disableTimeToLive=true,则 Camel 在发送 JMS 消息时 不会将 任何时间设置为 live 值。但是 请求超时仍处于活动状态。例如,如果您通过 JMS 进行请求/回复,并且禁用了生存时间,则 Camel 仍将使用 20 秒( requestTimeout 选项)的超时。也可自行配置该选项。因此,两个选项 requestTimeout 和 disableTimeToLive 可让您在进行请求/回复时进行精细的控制。
您可以在消息中提供标头来覆盖并使用 作为请求超时值,而不是端点配置的值。例如:
from("direct:someWhere")
.to("jms:queue:foo?replyTo=bar&requestTimeout=30s")
.to("bean:processReply");
在上面的路由中,我们有一个端点将 requestTimeout 配置为 30 秒。因此,Camel 将等待 30 秒,以便该回复消息回到栏队列中。如果没有收到回复消息,则在 Exchange 中设置 org.apache.camel.ExchangeTimedOutException,Camel 会继续路由消息,然后因为例外和 Camel 的错误处理程序响应而失败。
如果要使用每个消息超时值,您可以使用键 org.apache.camel.component.jms.JmsConstants SerialJMS_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");
当您使用 Camel 对 JMS 进行触发和忘记(InOut)时,默认情况下 Camel 不会 随时将消息的值设置为 live 值。您可以使用 timeToLive 选项配置值。例如,要指明 5 sec,您可以设置 timeToLive=5000。选项 disableTimeToLive 可用于强制将时间禁用为 live,也用于 InOnly messaging。requestTimeout 选项不用于 InOnly messaging。