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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Apache Camel中的OnCompletion用于整个文件的完成相关的知识,希望对你有一定的参考价值。

我有文件处理的路由和多个进程。我想要实现的是,如果我放置一个文件,在输出文件创建后我想在onCompeltion()代码中重命名创建的文件。但是对于所有路由都触发了onCompletion代码但是我想在所有routes的特定文件进程结束时这样做。请帮我实现这个。我也试过这个链接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");
});
答案

只需在拆分器后添加它

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文档中的更多内容

以上是关于Apache Camel中的OnCompletion用于整个文件的完成的主要内容,如果未能解决你的问题,请参考以下文章

Apache Camel中的窃听和多播有啥区别

子文件夹中的 Apache Camel 监视文件

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

在 Apache Camel 中的处理器之间传递值

如何使用 Apache Camel 来监控文件更改?

将更改日志转换为 Apache Camel 中的实际状态