使用Apache Camel的RequestReply的RecipientList

时间:2014-08-08 21:55:40

标签: apache-camel

我认为我对 RecipientList 的理解存在差距。我的理解是 RecipientList EIP可用于表示动态目的地。我试图使用RequestReply EIP,但我得到了一些奇怪的结果。

以下代码是RequestReply的单元测试,并将回复聚合回发件人。消息到达incomingMessages1-update,被路由到outgoingMessages- [123] -update队列。结果返回到outgoingMessages- [123] -reply队列。结果将汇总并发送回incomingMessages1-reply队列。

见下面有效的单元测试:

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;
        }
    }
}    

我尝试将上面的代码(// 1,// 2和// 3更改为如下所示的收件人列表)并且它无法正常工作:

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

我加载了这样的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);

我收到原始邮件,但我没有看到创建的回复队列。你能指点一下我错过的东西吗?

1 个答案:

答案 0 :(得分:2)

您无法将列表/地图等作为JMS标头发送。 JMS规范不允许这样做。

发送时,请参阅消息格式部分

还有JMS spec / api / javadoc等。

您可以将值存储在以逗号分隔的字符串中。 Camel收件人列表将自动使用逗号作为分隔符,因此应该开箱即用。