Java Apache Camel是否支持嵌套路由?

Java Apache Camel是否支持嵌套路由?,java,apache-camel,Java,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().getHead

阿帕奇骆驼路线:

    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");
上面的路径是将大文件拆分成块(用于批处理)并生成带有结果的输出文件。生成输出文件后,我需要加密。因此,我在onCompletion file split/process route上的处理器内添加了新路由。它可以工作,但我觉得它不是一个好的设计(因为它涉及两个上下文,需要显式关闭上下文)

你能告诉我正确的加密方法吗

您建议的嵌套路由可能会被绕过。也许这条简单的路线可以满足您的要求:

@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编码,那么可以使用一个对内存中所有输入文件的内容进行采样的方法。当然,这只有在内存限制允许的情况下才可能实现。

我通过您的反馈增强了初始设计。但加密路由是将原始文件作为输入,是否有任何机制将生成的输出文件重定向为加密输入

    from("file:/tmp/test/input?include=.*.csv&noop=true")
        .setProperty("outputFile", simple("${header.CamelFileName}.${exchangeId}"))
        .onCompletion()
            .split().tokenize("\n", 100)
            .log("splitted body processed & written to file ${property.outputFile}.csv")
            .process(new RequestProcessor())
            .to("file:/tmp/test/temp?fileName=${property.outputFile}.csv&fileExist=Append")
        .end()
        .marshal().pgp(keyFileName, keyUserid)
        .log("PGP encrypted written to file ${property.outputFile}.pgp")
        .to("file:/tmp/test/output?fileName=${property.outputFile}.pgp");
注: 请求处理器:

public class RequestProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
String body = exchange.getIn().getBody(String.class);
String[] split = body.split("\n");
StringBuilder output = new StringBuilder();

// TODO begin trx

for (String input : split) {
    if (input.startsWith("InputHeader")) {
        output.append("OutputHeader").append(input.substring(11) + ",");
    } else {
        // TODO process here
        output.append("\n").append(input).append(",DONE");
    }
}

// TODO commit trx

DefaultMessage message = new DefaultMessage();
message.setBody(output.toString());

exchange.setOut(message);

}

我仍然有一个问题,使用上述解决方案,它将根据拆分大小创建多个文件。我怎样才能避免这种情况,并将其加密到单个文件中。如果答案对您有帮助,您可以接受答案并进行投票,这样每个人都会看到问题已经得到了回答。