当前位置: 首页 > 知识库问答 >
问题:

Apache Camel DynamicRouter-从bean方法返回null后仍然路由

满增
2023-03-14

我在Spring XML中用动态路由器bean定义了路由,如下所示

<route id="dynamic-recipientList-route" >
        <from uri="direct-vm:b2bfwk-post" />
                <log message="dynamic-recipientList-route received=${body}" />
                <dynamicRouter>
                    <method ref="dynamicRouter" method="route" />
                </dynamicRouter>
    </route>
public String route(Exchange inExch) {

    LOG.info("Enter DynamicRouterBean ... " + inExch.getIn().getBody());
    return whereToGo(inExch);
}

/**
 * Method which computes where to go next
 */
private String whereToGo(Exchange inExch) {

    int invoked = 0;
    Object current = inExch.getProperty("invoked");

    if(current != null){
        invoked = Integer.valueOf(current.toString());
        LOG.info("In progress of dynamic route... invoked = " + invoked + " ... " + inExch.getIn().getBody());
    }
    else{
        //Message enter dynamic router for the first time... invoked = 0
        LOG.info("Message enter dynamic router for the first time... invoked = 0 ... " + inExch.getIn().getBody());
    }
    invoked++;
    inExch.setProperty("invoked", invoked);

    if (invoked == 1) {
        // 1st time
        LOG.info("route to step 1 ... " + inExch.getIn().getBody());
        return "direct-vm://step1";

    } else if (invoked == 2) {
        // 2nd time 
        LOG.info("route to step 2 ... " + inExch.getIn().getBody());
        return "direct-vm://step2";

    } else {
        // no more, so return null to indicate end of dynamic router
        LOG.info("All Steps finished for message " + inExch.getIn().getBody());
        return null;
    }
}
<route id="app-route-step1" >
        <from uri="direct-vm:step1" />
            <log message="step1 received=${headers}, ${body}" />
            <delay><constant>1000</constant></delay>
            <to uri="direct-vm:b2bfwk-post" />
    </route>

    <route id="app-route-step2" >       
        <from uri="direct-vm:step2" />
            <log message="step2 received=${headers}, ${body}" />
            <delay><constant>1000</constant></delay>
            <to uri="direct-vm:b2bfwk-post" />
    </route>

下面补充日志...请注意“所有步骤都完成了...”声明出现了两次...

2015-08-26 12:46:42,418 INFO  multiconsumer-route            - multiconsumer-route received, header={breadcrumbId=ID--53870-1440564388141-0-1, CamelFileAbsolute=false, CamelFileAbsolutePath=D:\EclipseLunaSR2\eclipse_workspace\camelInAction\dynamic-route\target\inbox\smallfile.csv, CamelFileContentType=application/vnd.ms-excel, CamelFileLastModified=1440560415206, CamelFileLength=16, CamelFileName=smallfile.csv, CamelFileNameConsumed=smallfile.csv, CamelFileNameOnly=smallfile.csv, CamelFileParent=target\inbox, CamelFilePath=target\inbox\smallfile.csv, CamelFileRelativePath=smallfile.csv, JMSCorrelationID=null, JMSDeliveryMode=2, JMSDestination=Queue[camelInAction.sample.inventory], JMSExpiration=0, JMSMessageID=ID:EMS-SERVER.20B455DAC6251A7:1, JMSPriority=4, JMSRedelivered=false, JMSReplyTo=null, JMSTimestamp=1440564402407, JMSType=null, JMSXGroupID=null, JMSXUserID=null, testHeader=headerValue}
2015-08-26 12:46:42,419 INFO  dynamic-recipientList-route    - dynamic-recipientList-route received=123,0,bumper,4
2015-08-26 12:46:42,422 INFO  DynamicRouterBean              - Enter DynamicRouterBean ... 123,0,bumper,4
2015-08-26 12:46:42,422 INFO  DynamicRouterBean              - Message enter dynamic router for the first time... invoked = 0 ... 123,0,bumper,4
2015-08-26 12:46:42,422 INFO  DynamicRouterBean              - route to step 1 ... 123,0,bumper,4
2015-08-26 12:46:42,433 INFO  app-route-step1                - step1 received=123,0,bumper,4
2015-08-26 12:46:42,433 INFO  dynamic-recipientList-route    - dynamic-recipientList-route received=123,0,bumper,4
2015-08-26 12:46:42,434 INFO  DynamicRouterBean              - Enter DynamicRouterBean ... 123,0,bumper,4
2015-08-26 12:46:42,434 INFO  DynamicRouterBean              - In progress of dynamic route... invoked = 1 ... 123,0,bumper,4
2015-08-26 12:46:42,434 INFO  DynamicRouterBean              - route to step 2 ... 123,0,bumper,4
2015-08-26 12:46:42,434 INFO  app-route-step2                - step2 received=123,0,bumper,4
2015-08-26 12:46:42,435 INFO  dynamic-recipientList-route    - dynamic-recipientList-route received=123,0,bumper,4
2015-08-26 12:46:42,435 INFO  DynamicRouterBean              - Enter DynamicRouterBean ... 123,0,bumper,4
2015-08-26 12:46:42,435 INFO  DynamicRouterBean              - In progress of dynamic route... invoked = 2 ... 123,0,bumper,4
2015-08-26 12:46:42,435 INFO  DynamicRouterBean              - All Steps finished for message 123,0,bumper,4
2015-08-26 12:46:42,437 INFO  DynamicRouterBean              - Enter DynamicRouterBean ... 123,0,bumper,4
2015-08-26 12:46:42,437 INFO  DynamicRouterBean              - In progress of dynamic route... invoked = 3 ... 123,0,bumper,4
2015-08-26 12:46:42,437 INFO  DynamicRouterBean              - All Steps finished for message 123,0,bumper,4
2015-08-26 12:46:42,438 INFO  DynamicRouterBean              - Enter DynamicRouterBean ... 123,0,bumper,4
2015-08-26 12:46:42,438 INFO  DynamicRouterBean              - In progress of dynamic route... invoked = 4 ... 123,0,bumper,4
2015-08-26 12:46:42,438 INFO  DynamicRouterBean              - All Steps finished for message 123,0,bumper,4

共有1个答案

蓝恩
2023-03-14

我不知道您的代码在到达该路线之前做了什么,但使用该代码时,我得到了以下内容:

@Test
public void testName() throws Exception {
    context.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:a").dynamicRouter().method(new WhereToGo());
            from("direct-vm://step1").log("Step1: ${body}");
            from("direct-vm://step2").log("Step2: ${body}");
        }
    });

    template.sendBody("direct:a", "data");
    Thread.sleep(6000);
}

给出以下内容:

13:42:56.000 [main] INFO  n.n.WhereToGoTest - Enter DynamicRouterBean ... data
13:42:56.000 [main] INFO  n.n.WhereToGoTest - Message enter dynamic router for the first time... invoked = 0 ... data
13:42:56.001 [main] INFO  n.n.WhereToGoTest - route to step 1 ... data
13:42:56.009 [main] INFO  route2 - Step1: data
13:42:56.012 [main] INFO  n.n.WhereToGoTest - Enter DynamicRouterBean ... data
13:42:56.012 [main] INFO  n.n.WhereToGoTest - In progress of dynamic route... invoked = 1 ... data
13:42:56.012 [main] INFO  n.n.WhereToGoTest - route to step 2 ... data
13:42:56.013 [main] INFO  route3 - Step2: data
13:42:56.014 [main] INFO  n.n.WhereToGoTest - Enter DynamicRouterBean ... data
13:42:56.014 [main] INFO  n.n.WhereToGoTest - In progress of dynamic route... invoked = 2 ... data
13:42:56.014 [main] INFO  n.n.WhereToGoTest - All Steps finished for message data
 类似资料:
  • 我对反应性编程相对较新。我的问题是关于

  • 我对我的代码有个问题。我已经将类Employee声明为抽象。其下有3种抽象方法:抽象字符串部门、抽象int work_days和抽象void print_info。 现在,我创建了扩展Employee抽象类的第二类Tester。我实现了抽象方法。

  • 我试图模拟一些方法调用,但不幸的是我一直返回null。你能帮我指出我可能出错的地方吗?我正在使用time(). thenBack(),我觉得我正确地模拟了返回变量。事先非常感谢。我是JUnit和Mockito的新手,所以如果我错过了任何明显的东西,我很抱歉。 ServiceTest.java Service.java

  • 我有一个bean,它有一个构造对象列表的方法。我想从POJO的这个列表中生成一个json和jackson。 然后在我的骆驼路线中,我想从我的bean方法生成json。这里我做了什么,似乎不起作用 camel返回一个UUID不带扩展名且内容为NULL的文件。伙计们,我错过了什么!?

  • 这是我的代码,我不知道为什么我的bean没有被注入,它在构造函数中总是空的 下面是我的配置类: 我的堆栈跟踪 启动ApplicationContext时出错。若要显示自动配置报告,请在启用“debug”的情况下重新运行应用程序。11:55:29.139[restartedMain]错误O.S.Boot.SpringApplication-应用程序启动失败org.SpringFramework.Be

  • 我正在尝试编写一个sokoban求解器,我的代码可以工作,但是计算解决方案需要很多时间。我想这是因为我使用ArrayList,我尝试使用Hashtable,但是方法get不起作用, 因此,当我填充哈希表,并使用键获取列表时,我得到空值。 然而,顶点存在于图形中。 如何解决此问题以提高Sokoban解算器的速度。