使用camel进行动态路由不会转发消息

时间:2017-10-10 08:19:12

标签: java apache-camel dynamic-routing

我正在使用基于webservice和jms队列的Camel中的动态路由管理器。我有以下架构:

endpoint:cxf -> jms:queue -> dynamic routing to a jms:queue -> processing

这是我的路线定义:

@Override
public void configure() throws Exception {
    routeDefinition = from(fromEndpoint).routeId(name)
            .dynamicRouter(method(DynamicRoutingManager.class, "getRoute")).process(exchange -> {
                final List<?> soaList = exchange.getIn().getBody(List.class);
                final String type = (String) soaList.get(0);
                final String documentNumber = (String) soaList.get(1);
                final String productionStepNumber = (String) soaList.get(2);
                final String message = (String) soaList.get(3);

                final String messageToSend = "Route ID=" + name + ", from=" + fromEndpoint + ", type=" + type
                        + ", document number=" + documentNumber + ", production step number" + productionStepNumber
                        + ", message=" + message;
                LOG.debug("==> message={}", messageToSend);
                exchange.getOut().setBody(messageToSend);
            }); // .to(DLQ);
}

这里是我的动态路由管理器(我保持简单):

public String getRoute(String body, @Header(Exchange.SLIP_ENDPOINT) String previous) {
    LOG.debug("========> BODY={}", body);
    return "jms:topic:urgent_doc1_prod1";
}

路由jms:topic:urgent_doc1_prod1在运行时定义并运行(在日志中查看)

事实是,当我发送这样的请求时(见下文),我收到超时错误......

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hel="http://cxf.apache.org/wsse/handler/helloworld">
   <soapenv:Header/>
   <soapenv:Body>
      <hel:message>
         <!--Optional:-->
         <type>urgent</type>
         <!--Optional:-->
         <document_number>1</document_number>
         <!--Optional:-->
         <production_step_number>1</production_step_number>
         <!--Optional:-->
         <message>un message</message>
      </hel:message>
   </soapenv:Body>
</soapenv:Envelope>

因为我认为我的消息没有被提交到第二个jms:队列,所以任何处理都可以完成......

我做错了什么?

2 个答案:

答案 0 :(得分:0)

如果您要将邮件路由到单个动态目的地,请使用recipientListtoD - 请参阅此常见问题解答:http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html

动态路由器就像一个while loop,它一直保持路由,直到你通过空/空目的地告诉它停止。请参阅顶部的文档:http://camel.apache.org/dynamic-router.html。并且您的示例不会将其作为硬编码的目标值。

答案 1 :(得分:0)

实际上,这是对我的一种误解。我没有宣布&#34;来自&#34;在动态路由之后独立路由,所以没有消费者,所以超时。

@Override
public void configure() throws Exception {
    from(fromEndpoint).routeId(name + "a").recipientList(method(DynamicRoutingManager.class, "getRoute"));

    from(toEndpoint).routeId(name + "b").process(exchange -> {

        final List<?> soaList = exchange.getIn().getBody(List.class);
        final String type = (String) soaList.get(0);
        final Integer documentNumber = (Integer) soaList.get(1);
        final Integer productionStepNumber = (Integer) soaList.get(2);
        final String message = (String) soaList.get(3);

        final String messageToSend = "Route ID=" + name + ", from=" + fromEndpoint + ", type=" + type
                + ", document number=" + documentNumber + ", production step number" + productionStepNumber
                + ", message=" + message;

        LOG.debug("==> message={}", messageToSend);
        exchange.getOut().setBody(messageToSend);
    });
}

让它有效。