Hi You can also add that stop route stuff as an on-completion to the current exchange.
There is API on Exchange for that. And you can implemented Ordered and get it an ordering number to be executed LAST. And you can also try to add a delay in the code that stops the route to give the on completion time to complete first. And btw do you want to stop the route always after it has processed some files? Why dont you want to keep the route running? Or are you looking for running this only once or something? On Sun, Jan 13, 2019 at 1:17 PM Davide Rossi <davrossim...@gmail.com> wrote: > > Hi everyone! > I have a connection problem when stopping a ftp route. My route is > something like this: > > > > > > > > > > > > > > *<routes xmlns="http://camel.apache.org/schema/spring > <http://camel.apache.org/schema/spring>"> <route id="jobIdFtp_1"> > <from > uri="ftp://admin@localhost:2121/testFolder?move=.done&moveFailed=.error&delay=3000000&recursive=true&antInclude=**&password=admin&download=true&binary=true&passiveMode=true&sendEmptyMessageWhenIdle=true&maxMessagesPerPoll=1000&readLockDeleteOrphanLockFiles=false&autoCreate=false&localWorkDirectory=C:\Users\Test\Desktop\testAcquisition&throwExceptionOnConnectFailed=true&consumer.bridgeErrorHandler=true"/> > <to uri="bean:myProcessor"/> <choice> > <when> > <simple>${exchangeProperty.CamelBatchComplete}</simple> <to > uri="bean:stopRouteProcessor"/> </when> </choice> > </route></routes>* > where *stopRouteProcessor *is a prcessor that stops the route just as > reported in the documentation > public void process(final Exchange exchange) throws Exception { > Thread stop = new Thread() { > @Override > public void run() { > String routeId = exchange.getFromRouteId(); > try { > SpringCamelContext exchangeContext = > (SpringCamelContext) exchange.getContext(); > RouteDefinition routeDefinition = > exchangeContext.getRouteDefinition(routeId); > > exchangeContext.stopRoute(routeId); > > exchangeContext.removeRoute(routeId); > > exchangeContext.removeRouteDefinition(routeDefinition); > > } catch (Exception e) { > .... > } > } > }; > // start the thread that stops this route > stop.start(); > } > > The route is correctly stopped, but I receive a connection problem when > Camel tries to move on the server the last acquired file, because the > connection is already closed, just because of the route stop (in fact, if I > remove the stopRouteProcessor everything works fine). > 2019-01-11 16:53:24,303 | lMediaProcessorQueue | WARN | > GenericFileOnCompletion | Error during commit. > Exchange[ID-User-NBK-50955-1547221992014-0-6][Message: [Body is file based: > C:\Users\Test\Desktop\testAcquisition\mioFile.txt]]. Caused by: > [org.apache.camel.component.file.GenericFileOperationFailedException - File > operation failed: null Connection is not open. Code: 221] > org.apache.camel.component.file.GenericFileOperationFailedException: File > operation failed: null Connection is not open. Code: 221 > at > org.apache.camel.component.file.remote.FtpOperations.buildDirectory(FtpOperations.java:304) > at > org.apache.camel.component.file.strategy.GenericFileProcessStrategySupport.renameFile(GenericFileProcessStrategySupport.java:106) > at > org.apache.camel.component.file.strategy.GenericFileRenameProcessStrategy.commit(GenericFileRenameProcessStrategy.java:88) > at > org.apache.camel.component.file.GenericFileOnCompletion.processStrategyCommit(GenericFileOnCompletion.java:127) > at > org.apache.camel.component.file.GenericFileOnCompletion.onCompletion(GenericFileOnCompletion.java:83) > at > org.apache.camel.component.file.GenericFileOnCompletion.onComplete(GenericFileOnCompletion.java:57) > at > org.apache.camel.util.UnitOfWorkHelper.doneSynchronizations(UnitOfWorkHelper.java:104) > at > org.apache.camel.impl.DefaultUnitOfWork.done(DefaultUnitOfWork.java:229) > at > org.apache.camel.util.UnitOfWorkHelper.doneUow(UnitOfWorkHelper.java:65) > at > org.apache.camel.processor.CamelInternalProcessor$UnitOfWorkProcessorAdvice.after(CamelInternalProcessor.java:653) > at > org.apache.camel.processor.CamelInternalProcessor$UnitOfWorkProcessorAdvice.after(CamelInternalProcessor.java:608) > at > org.apache.camel.processor.CamelInternalProcessor$InternalCallback.done(CamelInternalProcessor.java:239) > at > org.apache.camel.processor.CamelInternalProcessor$InternalCallback.done(CamelInternalProcessor.java:250) > at > org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:491) > at > org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) > at > org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190) > at > org.apache.camel.component.seda.SedaConsumer.sendToConsumers(SedaConsumer.java:298) > at > org.apache.camel.component.seda.SedaConsumer.doRun(SedaConsumer.java:207) > at > org.apache.camel.component.seda.SedaConsumer.run(SedaConsumer.java:154) > at > java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) > at > java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) > at java.lang.Thread.run(Thread.java:748) > Caused by: java.io.IOException: Connection is not open > at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:472) > at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:537) > at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:586) > at org.apache.commons.net.ftp.FTP.pwd(FTP.java:1381) > at > org.apache.commons.net.ftp.FTPClient.printWorkingDirectory(FTPClient.java:1990) > at > org.apache.camel.component.file.remote.FtpOperations.buildDirectory(FtpOperations.java:281) > ... 21 common frames omitted > > Now, I know that onCompletion is executed in a separated thread, but is > there a way to know if/when onCompletion is executed for the last exchange, > so I can stop the route only after that? If this is not possible, is there > a different approach/solution to be sure that even the last file will be > correctly moved? > Thank you > Regards > Davide -- Claus Ibsen ----------------- http://davsclaus.com @davsclaus Camel in Action 2: https://www.manning.com/ibsen2