0

问题: 如何获得一个可增加的索引——即在 RouteBuilder 中的 loop() 期间使用——以便对“direct:thingC”的迭代调用将“处理”后续元素(在数组列表中)?

这是 configure() 方法...

    private final org.apache.camel.Processor proc1 = new Processor1();
    private static final List<String> searchList = Arrays.asList("AA", "BB");
    private static final int z = searchList.size();
    private static int x = 0;    

    //***idea is to both elements using an index during the "loop".... Not working....

    @Override
    public void configure() throws Exception {

    from("timer://foo?fixedRate=true&period=" + 5000) //5 seconds...            
        .to("direct:thingB");

    from("direct:thingB")
        .log("---------------------- (from(\"direct:thingB\"))... ----------x=" + x)
        .loop(searchList.size()).to("direct:thingC");

    from("direct:thingC")
        .log("---------------------- (from(\"direct:thingC\"))... ----------searchList.get(" + x++ + ")=" + searchList.get(x));
    }

日志输出看起来像这样(索引不增加:总是选择相同的元素)... :-(

2017-09-01 16:13:19,142 | INFO  | 43 - timer://foo | route15                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingB"))... ----------x=0
2017-09-01 16:13:19,142 | INFO  | 43 - timer://foo | route16                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingC"))... ----------searchList.get(0)=BB
2017-09-01 16:13:19,143 | INFO  | 43 - timer://foo | route16                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingC"))... ----------searchList.get(0)=BB
2017-09-01 16:13:24,141 | INFO  | 43 - timer://foo | route15                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingB"))... ----------x=0
2017-09-01 16:13:24,141 | INFO  | 43 - timer://foo | route16                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingC"))... ----------searchList.get(0)=BB
2017-09-01 16:13:24,142 | INFO  | 43 - timer://foo | route16                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingC"))... ----------searchList.get(0)=BB

目标是 - 而不是 - 有这样的输出....

2017-09-01 16:13:19,142 | INFO  | 43 - timer://foo | route15                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingB"))... ----------x=0
2017-09-01 16:13:19,142 | INFO  | 43 - timer://foo | route16                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingC"))... ----------searchList.get(0)=AA
2017-09-01 16:13:19,143 | INFO  | 43 - timer://foo | route16                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingC"))... ----------searchList.get(1)=BB
2017-09-01 16:13:24,141 | INFO  | 43 - timer://foo | route15                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingB"))... ----------x=0
2017-09-01 16:13:24,141 | INFO  | 43 - timer://foo | route16                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingC"))... ----------searchList.get(0)=AA
2017-09-01 16:13:24,142 | INFO  | 43 - timer://foo | route16                          | 232 - org.apache.camel.camel-core - 2.17.0.redhat-630187 | ---------------------- (from("direct:thingC"))... ----------searchList.get(1)=BB

解决方案:根据 Alessandro 的建议,如下


private final String s = "AA,BB";

@Override
public void configure() throws Exception {

    from("timer://foo?fixedRate=true&period=" + 5000) //5 seconds... 
            .setBody(constant(s))            
            .to("direct:thingB")

    from("direct:thingB")
            .split().tokenize(",")
            .to("direct:thingC");       

    from("direct:thingC")
            .log("body=" + body());  //note: this value looks like simple{AA}
}
4

1 回答 1

2

不要使用循环。从文档:

循环允许多次处理消息,每次迭代可能以不同的方式处理。主要在测试期间有用。
默认模式
注意默认情况下循环在整个循环中使用相同的交换。所以上一次迭代的结果将用于下一次迭代

由于您要处理可以“循环”的事物的单个元素,请将其设置为 body 并改用split

from("timer://foo?fixedRate=true&period=" + 5000) //5 seconds...            
    setBody(searchList)
    .to("direct:thingB");

from("direct:thingB")
    .split()
        .simple("${body}")
        .log("This is element: ${body} [Element number ${exchangeProperties.CamelSplitIndex} of ${exchangeProperties.CamelSplitSize} total elements]")
    .end()

拆分器将“分解”List成单个部分并在一个循环中处理所有内容。

static通常,如果字段是输入或输出数据,请避免使用它们。在这种情况下,最好将它们设置为主体,例如使用Processor设置所需数据的 bean。

于 2017-09-04T07:22:58.123 回答