Reputation: 10953
I have routes and multiple processes for a file processing.What I am trying to achieve is if I place a file, after output file creation I want to rename the created file in onCompeltion() code.But onCompletion code is triggered for all the routes.But I want to do that at the end of a specific file process of all routes.Please help me to achieve this.I tried this link OnCompletion() in Apache Camel is called more than once too.I want to rename the file in onCompletion each of the file process is completed
Router:
@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");
}
}
Current Output:
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
Expected:
total customers:3
current process1 customer:first1:::last1
current process1 customer:first2:::last2
current process1 customer:first3:::last3
File is processed
Updated Code Snippet working:
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");
});
Upvotes: 0
Views: 1578
Reputation: 55555
Just add it after the splitter
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
Also have you seen you can configure the file endpoint itself to rename the file after its done, its the move
option. See more in the Camel docs
Upvotes: 1