178.18. 使用 JMSReplyTo 进行延迟的回复

当将 Camel 用作 JMS 侦听器时,它会使用 ReplyTo javax.jms.Destination 对象的值设置 Exchange 属性,其键为 ReplyTo。您可以获得这个 目的地,如下所示:

Destination replyDestination = exchange.getIn().getHeader(JmsConstants.JMS_REPLY_DESTINATION, Destination.class);

然后,稍后使用它来使用常规 JMS 或 Camel 发送回复。

// we need to pass in the JMS component, and in this sample we use ActiveMQ
JmsEndpoint endpoint = JmsEndpoint.newInstance(replyDestination, activeMQComponent);
// now we have the endpoint we can use regular Camel API to send a message to it
template.sendBody(endpoint, "Here is the late reply.");

发送时要发送回复的不同解决方案是在同一 Exchange 属性中提供 回复 对象。然后 Camel 会获取此属性并将其用于真实目的地。但是,端点 URI 必须包含一个 dummy 目标。例如:

// we pretend to send it to some non existing dummy queue
template.send("activemq:queue:dummy, new Processor() {
   public void process(Exchange exchange) throws Exception {
      // and here we override the destination with the ReplyTo destination object so the message is sent to there instead of dummy
      exchange.getIn().setHeader(JmsConstants.JMS_DESTINATION, replyDestination);
      exchange.getIn().setBody("Here is the late reply.");
    }
}