2016-12-02 55 views
0

問題聲明Spring基於駱駝聚合器讀取多個文件

我有兩個文件File1和File2需要使用駱駝Bindy解析。 來自File1和File2的數據需要進行彙總並插入到數據庫中。

什麼是定義agregator最好的辦法是有一個有人可以提供一個樣本實現

這裏是我想

<!--route1 --> 
<route> 
    <from ref="file1Endpoint" /> 
    <unmarshal ref="pojo1" /> 
</route> 
<!--route2 --> 
<route> 
    <from ref="file2Endpoint" /> 
    <unmarshal ref="pojo2" /> 
</route> 

回答

0

使用Content Enricher提供Aggregation Strategy,像下面:

@Component 
MyAggregationStrategy implements AggregationStrategy { 

    public static final String PRE_ENRICH_KEY = "PRE_ENRICH_KEY"; 

    //this is meant to be called *before* the actual enrich call 
    //to store the state before the enrichment 
    public void init(Exchange exchange) { 
    final String originalBody = exchange.getIn().getBody(String.class); 
    exchange.setProperty(PRE_ENRICH_KEY, originalBody); 
    } 

    @Override 
    public Exchange aggregate(Exchange original, Exchange enrichmentResponse) { 

    final String originalBody = original.getProperty(PRE_ENRICH_KEY, String.class); 

    //enrichmentResponse is the Exchange coming from the 2nd file endpoint 
    //the enricher returns 

    //your processing logic to merge the 2 happens here 
    String result = //.. 
    original.getIn().setBody(result); 

    return original; 

    } 

} 

最後的路徑看起來像:

@Component 
public class MyRouteBuilder extends RouteBuilder { 

    @Autowired 
    private MyAggregationStrategy myAggregateStrategy; 

    @Override 
    public void configure() throws Exception { 
     from("fileEndpoint1") 
      .bean(myAggregateStrategy, "init") 
      .enrich("fileEndpoint2", myMerger); 
      //continue from here one to Bindy 
    } 

    //.. 
} 
+0

感謝您的回覆。我會嘗試在sdping DSL方式 –

+0

@Harish Malavade - 如果你覺得這個有用,請考慮upvoting和/或接受答案 – dimitrisli