Search code examples
apache-camelcamel-ftp

Reuse Camel FTP route


I have a globally defined FTP Camel route. It is started from another route:

exchange.getContext().startRoute("downloadRoute");

and stopped in another thread, as described here:

.process(new Processor() {
    Thread stop;
    @Override
    public void process(final Exchange exchange) throws Exception {
        stop = new Thread() {
            @Override
            public void run() {
                try {
                    log.info("Stopping route");
                    exchange.getContext().stopRoute("downloadRoute");
                } catch (Exception e) {
                } finally {
                    latch.countDown();
                }
            }
        };
        stop.start();
    }
})

It works fine. Now I want to start this route again. Say I added new files to the folder and want to download them again, or re-download downloaded files.
So I start this route again with the same command, but it doesn't download files - it simply doesn't see them, since they marked as downloaded somewhere in the route. Whereas if I remove this route from camel context then add it again and start - it works - it downloads files again.

So the question is how to reuse (start-stop-start) an existing route so that it starts as a new one?


Solution

  • I think generally, this is a misuse of Apache Camel. Routes are not meant to be started or stopped over and over within an application. Routes in Camel are generally meant to be defined, then started on application startup, and then not stopped until the application stops.

    If you want to re-download files on demand using a file consumer based route (file, FTP, SFTP, etc), I think the correct approach would be to move the files from the .camel directory (where downloaded files are placed by default) back to the directory which is monitored by the file consume. You could do this programatically quite easily if needed.

    This should cause Camel to re-download the files. Camel would then place them back in the .camel directory when finished. You can also use a different directory than .camel using the move file component flag (.camel is the default) if you want.