有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我有一个文件处理的路由和多个进程。我试图实现的是,如果我放置了一个文件,在创建输出文件后,我想用oncompetion()代码重命名创建的文件。但所有路由都会触发onCompletion代码。但我想在所有路由的特定文件进程结束时这样做。请帮助我实现这一目标。我也尝试了这个链接。我想在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) 个答案

  1. # 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选项。请参阅骆驼文档中的更多内容