简体   繁体   English

使用Apache Camel的带有RequestReply的RecipientList

[英]RecipientList with RequestReply Using Apache Camel

I think that I have a gap in my understanding of the RecipientList . 我认为我对RecipientList理解存在差距。 My understanding is that RecipientList EIP can be used to represent dynamic destinations. 我的理解是RecipientList EIP可用于表示动态目的地。 I am attempting to use is with the RequestReply EIP but I am getting some strange results. 我正在尝试将其与RequestReply EIP一起使用,但得到了一些奇怪的结果。

The code below is a unit test for RequestReply and aggregation of replies back to the sender. 下面的代码是对RequestReply的单元测试,并将回复汇总到发件人。 Message arrives at incomingMessages1-update, gets routed to outgoingMessages-[123]-update queues. 消息到达incomingMessages1-update,路由到outingMessages- [123] -update队列。 The results come back on outgoingMessages-[123]-reply queues. 结果返回到outingMessages- [123]-答复队列。 The results are aggregated and sent back on incomingMessages1-reply queue. 将结果汇总并发送回incomingMessages1-reply队列。

See below a unit test that works: 请参阅下面的有效单元测试:

public class AggregateStrategyTestOnMultipleReplyQueues extends CamelTestSupport {

@Test
public void testRequestReplyWithRecipientListAndCustomGather()
        throws Exception {
    int numberOfMessages = 5;
    getMockEndpoint("mock:end").setExpectedMessageCount(numberOfMessages);

    context.addRoutes(new RouteBuilder() {
        public void configure() throws Exception {
            from("jms:incomingMessages1-update")
                .multicast(new GatherResponses())
                    .to("jms:outgoingMessages1-update?exchangePattern=InOut&replyTo=queue:outgoingMessages1-reply&preserveMessageQos=true") //1
                    .to("jms:outgoingMessages2-update?exchangePattern=InOut&replyTo=queue:outgoingMessages2-reply&preserveMessageQos=true") //2
                    .to("jms:outgoingMessages3-update?exchangePattern=InOut&replyTo=queue:outgoingMessages3-reply&preserveMessageQos=true") //3
                    .to("mock:end");

            //this is what the adapters will be doing
            from("jms:outgoingMessages1-update").setBody(constant("Hello World")).to(
                    "mock:end");
            from("jms:outgoingMessages2-update").setBody(constant("Welcome World")).to(
                    "mock:end");
            from("jms:outgoingMessages3-update").setBody(constant("Hi World")).to(
                    "mock:end");
        }
    });

    String messageSent = "Message sent from template";

    Object response = template
            .requestBodyAndHeader(
                    "jms:incomingMessages1-update?exchangePattern=InOut&preserveMessageQos=true",
                    messageSent, "JMSReplyTo", "incomingMessages1-reply");
    assertEquals("Hello World" + " "+ "Welcome World"+ " "+ "Hi World"+ " " + messageSent ,
            response);

    }

    private class GatherResponses implements AggregationStrategy {
        public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
            if (oldExchange == null) {
                return newExchange;
            }
            String oldBody = oldExchange.getIn().getBody(String.class);
            String newBody = newExchange.getIn().getBody(String.class);
            String body = oldBody + " " + newBody;
            oldExchange.getIn().setBody(body);
            return oldExchange;
        }
    }
}    

I attempted to change the code above (//1, //2 and //3 to a recipient list like below) and it didn't work: 我试图将上面的代码(// 1,// 2/2和/// 3更改为如下所示的收件人列表),但它不起作用:

from("jms:incomingMessages1-update")
                    .recipientList(header("myRecipientList")).aggregationStrategy(new GatherResponses()).parallelProcessing().end()
                    .to("mock:end");

I loaded the URIs like this: 我这样加载了URI:

List<String> recipientList = new ArrayList<String>();
recipientList.add("jms:outgoingMessages1-update?exchangePattern=InOut&replyTo=queue:outgoingMessages1-reply&preserveMessageQos=true");
recipientList.add("jms:outgoingMessages2-update?exchangePattern=InOut&replyTo=queue:outgoingMessages1-reply&preserveMessageQos=true");
recipientList.add("jms:outgoingMessages3-update?exchangePattern=InOut&replyTo=queue:outgoingMessages1-reply&preserveMessageQos=true");

Map<String, Object> headers = new HashMap<String, Object>();
headers.put("JMSReplyTo", "incomingMessages1-reply");
headers.put("myRecipientList", recipientList);

I am getting the original message back and I am not seeing the reply queues created. 我收到了原始消息,但没有看到创建的回复队列。 Can you please point me to what I am missing? 您能指出我所缺少的吗?

You cannot send a List/Map etc as JMS headers. 您不能将列表/地图等作为JMS标头发送。 The JMS spec does not allow that. JMS规范不允许这样做。

See section Message format when sending at 请参阅在发送时的消息格式部分

And also the JMS spec / api / javadoc etc. 还有JMS规范/ api / javadoc等。

You can instead store the values in a String separated by comma. 您可以将值存储在以逗号分隔的字符串中。 The Camel recipient list will automatic use comma as delimiter, so that should then work out of the box. 骆驼收件人列表将自动使用逗号作为分隔符,因此应该可以立即使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM