178.18. 遅延応答に対する JMSReplyTo の使用
Camel を JMS リスナーとして使用する場合、キー ReplyTo を持つ ReplyTo javax.jms.Destination オブジェクトの値で Exchange プロパティーを設定します。この Destination は次のように取得できます。
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 にはダミーの宛先を含める必要があります。以下に例を示します。
// 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.");
}
}