2017-09-01 56 views
0

問:如何能夠獲得遞增的指數 - 即,在RouteBuilder循環()過程中使用 - 這樣反覆呼籲「直接:thingC」,將「過程」後續元素(在ArrayList中) ?如何獲得可增量索引 - 即在RouteBuilder的循環()中使用?

這裏是配置()方法...

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 

解決方案:按亞歷山德羅的建議,下面


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

回答

1

不要使用loop。從文檔:

該循環允許多次處理消息,可能以不同的方式進行每次迭代。主要在測試過程中有用
默認模式
通知默認情況下,循環使用整個循環中的相同交換。所以,既然你要處理的東西單一的元素,你可以「循環」上,將其設置爲體,並使用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。

+0

謝謝你亞歷山德羅。我改變了使用上面的「split()」函數。順便說一句,我將如何從「簡單的{AA}」表達式中提取「AA」字符串? thx,再次! – sairn

相關問題