如何使用Camel的ZipAggregationStrategy访问呼叫交换

时间:2014-06-29 03:58:38

标签: java zip apache-camel

我使用ZipAggregationStrategy在Camel中创建zip文件,需要根据调用交换中设置的自定义属性设置输出文件名。但是,我在zip聚合后收到的交换不包含任何属性。

我的总体流程是:

  1. 在交换机上设置自定义属性的处理器(动态,基于数据库查询)
  2. 生成List zip文件内容的Bean
  3. Camel aggregator使用ZipAggregationStrategy,基于Camel文档中的example
  4. 在第3步之后,我希望动态命名我在步骤1中在交换机上设置的zip属性,但交换只包含Camel属性(CamelFileExchangeFile以及zip文件的路径等。)因为正在aggregate()创建一个新的交换:

    @Override
    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
        File zipFile;
        Exchange answer = oldExchange;
    
        // ...
    
        DefaultEndpoint endpoint = (DefaultEndpoint) newExchange.getFromEndpoint();
        answer = endpoint.createExchange();
        answer.addOnCompletion(new DeleteZipFileOnCompletion(zipFile));
    
        // ...
    
        genericFile.bindToExchange(answer);  
    
        // ...
    
        return answer;
    }
    

1 个答案:

答案 0 :(得分:2)

我猜这是一个错误。应保留属性,而不是由聚合策略删除。我打开了一个Jira问题,希望能解决这个问题:CAMEL-7555

解决方法扩展ZipAggregationStrategy并覆盖aggregate

public class MyZipAggregationStrategy extends ZipAggregationStrategy {
    @Override
    public Exchange aggregate(final Exchange oldExchange, final Exchange newExchange) {
        final Exchange answer = super.aggregate(oldExchange, newExchange);
        answer.setProperty("myZipName", newExchange.getProperty("myZipName")); // Hack!!
        return answer;
    }
}

使用MyZipAggregationStrategy,如下所示:

public class MyRouteBuilder extends RouteBuilder {
    @Override
    public void configure() {
        from("file:zipper/in?include=.*.xml&noop=true")
            .process(new Processor() {
                @Override
                public void process(final Exchange exchange) throws Exception {
                    exchange.setProperty("myZipName", "messages.zip");
                }
            })
            .aggregate(new MyZipAggregationStrategy())
            .constant(true)
            .completionFromBatchConsumer()
            .eagerCheckCompletion()
            .setHeader(Exchange.FILE_NAME, simple("${property.myZipName}")) // setting ZIP file name
            .to("file:zipper/out");
    }
}