I wrote some code to download a file from an FTP site looks like below:
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
public void configure() {
from( uri ).to( localBox );
}
});
context.start();
// a tedious sleepy thread !?
// TODO: work around for a better solution
Thread.sleep(20000);
context.stop();
I am looking for a better solution instead of Thread.sleep method. Is it also possible to get how much time has left for finishing the transfer?
Thanks
Main Answer
The syntax below will get you what you want without using Thread.sleep:
Other Suggestions
This page has a few other examples of ways to shutdown camel. However, if you’re using these approaches, chances are that you’re not using camel as intended. As @Petter said, Camel is typically used on a server that continuously runs.
Here, when we want to run a route in a “one-off” way, we usually just use the Producer as in:
You can use a similar technique to send a message to a route who is responsible for shutting things down using DSLs like the
shutdownRouteandshutdownRunningTaskor some kind ofprocessorwho exits the app, gracefully.Once you have a route like that in place, you can send your application the appropriate message that invokes this route and exits cleanly (Alternatively, using an instance of Camel’s Main class and enabling hangup support takes care of this for you when you send Ctrl+C to your running application).