apache-camel – 如何使用异步子路由连接Apache Camel同步请求/回复端点

我想将Web服务请求路由到jms队列的InOnly端点.然后将从单独的InOnly端点接收的响应jms消息作为响应路由回web服务客户端. Web服务请求/响应是同步InOut模式,子路由是异步的.我有什么选择来实现Camel?

这里的Camel路线用于解释我的问题:

String uri={webserice uri}
from(uri)    
    .process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            ServcieRequest req =            
            exchange.getIn().getBody(ServcieRequest.class);                 
            // One option to me is to spawn another route here to route to jms queue...         
            ProducerTemplate template = exchange.getContext().createProducerTemplate();
            template.sendBodyAndHeaders("jms:queue:INQueue", req.getPayload(), headers);
            // then need to wait ...until received jms response from the route below        

   }});


from("jms:queue:OUTQueue")
   .process(new Processor() {
       public void process(Exchange exchange) throws Exception {
           // received jms response message
           // need to update the exchange data in the above route based on jms message
           // so the final response to the webservice cilent can receive the data ...
       }});

最佳答案 我认为你应该依赖Camel中的请求回复机制来完成这项任务.

Camel Doc, Exclusive Fixed Reply Queue

所以我猜以下的DSL路由几乎可以满足您的需求(如果没有其他原因可以为JMS部分使用InOnly模式?).如果需要,请确保调整requestTimeout(因为它默认为20Sec超时).

from(webserviceURI)
  .inOut().to("jms:queue:INQueue?replyTo=OUTQueue?replyToType=Exclusive");

是的,还有一件事.如果在多个节点上运行此操作,则每个节点需要一个独占队列.

点赞