Apache Camel动态路由基于文件内容

时间:2015-04-27 09:46:47

标签: apache-camel apache-servicemix

我正在尝试使用apache camel将文件内容路由到activemq队列。我使用camel从特定文件夹中轮询xml文件,然后将其内容发送到队列。我的问题是我想根据内容值将内容发送到动态队列名称。例如:
xml文件内容为

<name="queue1"/>

然后它意味着这个需要发送到名为“queue1”的队列。如何将“queue1”动态值用于camel route spring dsl中的“to:”参数?

由于

2 个答案:

答案 0 :(得分:2)

假设您有一个xml文档,您可以使用xpath提取信息并将其设置在标题中,您可以使用简单的

http://camel.apache.org/simple.html

http://camel.apache.org/xpath.html

http://camel.apache.org/recipient-list.html

@EndpointInject(uri = "direct:start")
private ProducerTemplate start;

@EndpointInject(uri = "mock:result1")
private MockEndpoint result1;

@Test
public void testName3() throws Exception {
    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
                .setHeader("address", xpath("/address/text()", String.class))
                .recipientList(simple("mock:${header.address}"));
        }
    });
    result1.expectedMessageCount(1);
    start.sendBody("<address>result1</address>");
    result1.assertIsSatisfied();
}

答案 1 :(得分:1)

我认为最好的方法是将该名称保存到标题中,如下例所示: https://stackoverflow.com/a/9637840/3703819

然后在端点中使用Simple表达式,如下所示:

<recipientList>
    <simple>activemq:queue:${headers.dest}</simple>
</recipientList>
相关问题