java - Apache Camel 支持嵌套路由吗?

标签 java apache-camel

Apache Camel 路线:

    from("file:/tmp/test?include=.*.csv").process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            // set output file name
            exchange.setProperty("outputFile", exchange.getIn().getHeader(Exchange.FILE_NAME, String.class) + ".tmp." + exchange.getExchangeId());
        }
    }).onCompletion().split().tokenize("\n", 100).process(new RequestProcessor()).to("direct:response").end().process(new Processor() {
        public void process(Exchange exchange) throws Exception {
            final String outputFile = exchange.getProperty("outputFile", String.class);

            // add new rout to encrypt
            CamelContext context = new DefaultCamelContext();
            context.addRoutes(new RouteBuilder() {
                public void configure() {

            from("file:/tmp/test/output?fileName=" + outputFile).marshal().pgp(keyFileName, keyUserid).to("file:/tmp/test/output?fileName=" + outputFile + ".pgp");
                }
            });

            context.start();
            Thread.sleep(5000);
            context.stop();
        }
    });

    from("direct:response").to("file:/tmp/test/output?fileName=${header.outputFile}&fileExist=Append");

以上路线正在处理将大文件拆分为 block (用于批处理)并生成包含结果的输出文件。一旦生成了我需要加密的输出文件。因此,我在 onCompletion 文件拆分/处理路由上的处理器内添加了新路由。它有效,但我觉得这不是一个好的设计(因为涉及两个上下文并且需要显式关闭上下文)。

有人可以建议我触发加密路由的正确方法吗?

最佳答案

通常(或总是)您建议的嵌套路由可能会被绕过。也许这个简单的路线会满足您的要求:

@Override
public void configure() throws Exception {
    from("file:/tmp/test?include=.*.csv")
       .split().tokenize("\n", 100)
       .setProperty("outputFile", simple("${header.CamelFileName}.${exchangeId}.pgp"))
       .log("The splitted body will be PGP encoded & written to file ${property.outputFile}")
       .marshal().pgp("keyFileName", "keyUserid")
       .to("file:/tmp/test/output?fileName=${property.outputFile}");
    }
}

不会写入临时文件,但分割后的内容会直接加密在内存中。

编辑:

如果你想一个一个地处理文件,那么你的路线将如下所示:

@Override
public void configure() throws Exception {
    from("file:/tmp/test?include=.*.csv")
       .setProperty("outputFile", simple("${header.CamelFileName}.${exchangeId}.pgp"))
       .log("The body will be PGP encoded & written to file ${property.outputFile}")
       .marshal().pgp("keyFileName", "keyUserid")
       .to("file:/tmp/test/output?fileName=${property.outputFile}");
    }
}    

如果您想首先创建一个大文件,然后对该文件进行 PGP 编码,那么您可以使用 aggregator即对内存中所有输入文件的内容进行采样。当然,这只有在您的内存限制允许的情况下才有可能。

关于java - Apache Camel 支持嵌套路由吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22087871/

相关文章:

java - Java 中的高阶函数

java - 正则表达式:如何找到精确的值长度?

JavafirstPosition算法

java - Apache Camel - 如何在 java 中使用属性配置端点

java - Apache Camel - Groovy 脚本

java - 循环条件变量的最佳实践

java - 即使使用更新的 gradle 文件,也无法解析符号 'firebase'

java - Apache Camel : errorHandler vs onException?

java - 轮询后将邮件标记为未读 - Apache Camel

java - Apache Camel : do not trigger route if previous route run is not complete