25.17. 使用 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 属性中提供 replyDestination
对象。然后,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."); } }