Apache Camel中的OnCompletion用于整个文件的完成

时间:2018-03-05 10:29:15

标签: java apache-camel

我有文件处理的路由和多个进程。我想要实现的是,如果我放置一个文件,在输出文件创建后我想在onCompeltion()代码中重命名创建的文件。但是onCompletion代码被触发了所有的路线。但是我想在所有路线的特定文件过程结束时这样做。请帮我实现这个。我也尝试了这个链接OnCompletion() in Apache Camel is called more than once。我想在onCompletion中重命名每个文件文件过程完成

路由器:

@Component
public class MyRouter extends RouteBuilder {

            @Override
            public void configure() throws Exception {

                onCompletion().process((exchange)->{System.out.println("File is processed");});

                from("file:src/main/resources/in?delete=true").routeId("route1").process((exchange) -> {
                    List<Customer> names = new ArrayList<>();
                    names.add(new Customer("first1","last1"));
                    names.add(new Customer("first2","last2"));
                    names.add(new Customer("first3","last3"));
                    System.out.println("total customers:"+names.size());
                    exchange.getOut().setBody(names);
                }).split(simple("${body}")).to("direct:process1");

                from("direct:findAndProcess").routeId("findAndProcess").choice()
                .when().simple("${body.getFirstName} == 'first1'").to("direct:process1")
                .otherwise().to("direct:process2");

                from("direct:process1").routeId("process1").process((exchange) -> {
                    System.out.println("current process1 customer:"+exchange.getIn().getBody(Customer.class).toString());
                    exchange.getOut().setBody(exchange.getIn().getBody(Customer.class).toString());
                }).to("direct:write2file");

                from("direct:process2").routeId("process2").process((exchange) -> {
                    System.out.println("current process2 customer:"+exchange.getIn().getBody(Customer.class).toString());
                    exchange.getOut().setBody(exchange.getIn().getBody(Customer.class).toString());
                }).to("direct:write2file");

                from("direct:write2file").routeId("write2file").to("file:src/main/resources/out?fileName=test_out.txt&fileExist=Append");       
    }
}

当前输出:

total customers:3
current process1 customer:first1:::last1
File is processed
File is processed
current process1 customer:first2:::last2
File is processed
File is processed
current process1 customer:first3:::last3
File is processed
File is processed
File is processed

预期:

total customers:3
current process1 customer:first1:::last1
current process1 customer:first2:::last2
current process1 customer:first3:::last3
File is processed

更新了代码段工作:

from("file:src/main/resources/in?delete=true").routeId("route1").process((exchange) -> {
    List<Customer> names = new ArrayList<>();
    names.add(new Customer("first1", "last1"));
    names.add(new Customer("first2", "last2"));
    names.add(new Customer("first3", "last3"));
    System.out.println("total customers:" + names.size());
    exchange.getOut().setBody(names);
}).split(simple("${body}")).to("direct:findAndProcess").end().process((exchange) -> {
    System.out.println("File is processed");
});

1 个答案:

答案 0 :(得分:1)

只需在拆分器

之后添加它
from
   split
     to direct:1
   end // end of splitter
   process // here you can rename the file, but you can also just use the move option on the file endpoint

你也看到你可以配置文件端点本身在文件完成后重命名,它是move选项。请参阅Camel文档中的更多内容