Camel - Stop route when the consuming directory not exists - apache-camel

I've an SFTP route (in Spring XML), and its from path ends in a daily changing directory (ie. /yyyyMMdd), and everything is working well when autoCreate=true or the directory exists when the route starts. But it is not permitted to me to create the directory if not exists!
When the dir exists, the route get files and terminates itself.
When the dir not exists, the route is polling permanently with a warning (ie. org.apache.camel.component.file.GenericFileOperationFailedException: Cannot change directory to: 20160917) and never stops.
How can I avoid this behaviour (eg. convert the warning to an empty message or exception or ...)? I've done experiments with startingDirectoryMustExist, consumer.bridgeErrorHandler and many others without any success.
The simplified route (before start, fill the elmu.sftp.importDir property with the actual date):
<from
uri="sftp://{{elmu.sftp.host}}:{{elmu.sftp.port}}{{elmu.sftp.importDir}}?username={{elmu.sftp.userName}}&password={{elmu.sftp.password}}&
autoCreate=false&preferredAuthentications=password&binary=true&include={{elmu.importMask}}&initialDelay=100&
noop=true&sortBy=file:name&sendEmptyMessageWhenIdle=true"/>
<choice>
<when>
<simple>${body} != null</simple>
... a lot of stuff ...
<to uri="bean:shutdownRoute" />
</when>
<otherwise>
<to uri="bean:shutdownRoute" />
</otherwise>
</choice>
With directoryMustExist=true and startingDirectoryMustExist=true the result is an endless loop (poll) with this warning:
08:30:14,658 WARN SftpConsumer - Consumer Consumer[sftp://xxx.xxx.xx:22/DBHtest/ELMUteszt/Kiadott_adatok/20160918?autoCreate=false&binary=true&directoryMustExist=true&include=%5E.*%24&initialDelay=100&noop=true&password=xxxxxx&preferredAuthentications=password&sendEmptyMessageWhenIdle=true&sortBy=file%3Aname&startingDirectoryMustExist=true&username=xxx] failed polling endpoint: Endpoint[sftp://xxx:22/DBHtest/ELMUteszt/Kiadott_adatok/20160918?autoCreate=false&binary=true&directoryMustExist=true&include=%5E.*%24&initialDelay=100&noop=true&password=xxxxxx&preferredAuthentications=password&sendEmptyMessageWhenIdle=true&sortBy=file%3Aname&startingDirectoryMustExist=true&username=xxx]. Will try again at next poll. Caused by: [org.apache.camel.component.file.GenericFileOperationFailedException - Cannot change directory to: 20160918]
org.apache.camel.component.file.GenericFileOperationFailedException: Cannot change directory to: 20160918
at org.apache.camel.component.file.remote.SftpOperations.doChangeDirectory(SftpOperations.java:576)
at org.apache.camel.component.file.remote.SftpOperations.changeCurrentDirectory(SftpOperations.java:564)
at org.apache.camel.component.file.remote.SftpConsumer.doPollDirectory(SftpConsumer.java:107)
at org.apache.camel.component.file.remote.SftpConsumer.pollDirectory(SftpConsumer.java:79)
at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:131)
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:175)
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:102)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: 2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)
at com.jcraft.jsch.ChannelSftp._realpath(ChannelSftp.java:2340)
at com.jcraft.jsch.ChannelSftp.cd(ChannelSftp.java:342)
at org.apache.camel.component.file.remote.SftpOperations.doChangeDirectory(SftpOperations.java:574)
... 13 more
It doesn't work with stepwise=false:
11:52:19,210 WARN SftpConsumer - Consumer Consumer[sftp://xxx:22/DBHtest/ELMUteszt/Kiadott_adatok/20160918?autoCreate=false&binary=true&directoryMustExist=true&include=%5E.*%24&initialDelay=100&noop=true&password=xxxxxx&preferredAuthentications=password&sendEmptyMessageWhenIdle=true&sortBy=file%3Aname&startingDirectoryMustExist=true&stepwise=false&username=xxx] failed polling endpoint: Endpoint[sftp://xxx:22/DBHtest/ELMUteszt/Kiadott_adatok/20160918?autoCreate=false&binary=true&directoryMustExist=true&include=%5E.*%24&initialDelay=100&noop=true&password=xxxxxx&preferredAuthentications=password&sendEmptyMessageWhenIdle=true&sortBy=file%3Aname&startingDirectoryMustExist=true&stepwise=false&username=xxx]. Will try again at next poll. Caused by: [org.apache.camel.component.file.GenericFileOperationFailedException - Cannot list directory: DBHtest/ELMUteszt/Kiadott_adatok/20160918]
org.apache.camel.component.file.GenericFileOperationFailedException: Cannot list directory: DBHtest/ELMUteszt/Kiadott_adatok/20160918
UPDATE (according to #ruffp's answer):
I've tried to set up a custom PollingConsumerPollStrategy, but I cannot stop the route from it. Only the third (commented row) stops the route, but I've several routes and I cannot know the name of the actual route. How can I get it?
#Override
public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception cause) throws Exception {
consumer.getEndpoint().stop(); // 1
consumer.stop(); // 2
consumer.getEndpoint().getCamelContext().stopRoute(route???); // 3
return false;
}

Finally I've solved it with consumer.exceptionHandler. But it seems, that this option is not in list of the usable options (http://camel.apache.org/file2.html which I've been reading again and again) just mentioned once with an example at the bottom of the huge page. Unfortunately, it was so "hidden" that I missed it up to now.
I set up a new class and implemented the handleExceptions methods:
public class DirNotExistsExHandler implements ExceptionHandler
which get the exceptions and decide what to do. In the context, I did a bean definition:
<bean id="dirNotExistsExHandler" class="hu.dbit.eleo.DirNotExistsExHandler" />
And in the consumer, passed the bean to the handler:
consumer.exceptionHandler=#dirNotExistsExHandler
Many thanks for your help!

In my opinion the best way to do it would be:
Enable the option throwExceptionOnConnectFailed on the sftp endpoint like this:
sftp://{{elmu.sftp.host}}:{{elmu.sftp.port}}{{elmu.sftp.importDir}}?username={{elmu.sftp.userName}}&password={{elmu.sftp.password}}&
autoCreate=false&preferredAuthentications=password&binary=true&include={{elmu.importMask}}&initialDelay=100&
noop=true&sortBy=file:name&sendEmptyMessageWhenIdle=true&throwExceptionOnConnectFailed=true
This could help to manage with the exception and to be catch it by the camel routes onException. However I am not sure it is really necessary in your case.
Make a special processing for your connection exception
// 2a) Solution to redirect an empty body to a destination
onException(GenericFileOperationFailedException.class)
.handled(true)
.log(LoggingLevel.INFO, "Source directory not present: send empty body to shutdown route...")
.setBody(null)
.to("bean:shutdownRoute");
Or another way:
// 2b)Solution to just stop the processing without log in warn or error
onException(GenericFileOperationFailedException.class)
.handled(true)
.log(LoggingLevel.INFO, "Source directory not present: stop the process and wait until next time...")
.stop();
UPDATE:
I found this post and apparently there is not another way than implementing your own PollingConsumerPollStrategy because the GenericFileOperationFailedException is apparently handled inside the default implementation.

Handle the exception using doCatch block. If no exception is thrown then check file exists using your code and throw exception manually.
http://camel.apache.org/try-catch-finally.html

Otto Csatari: To get Route Id of failed route in rollback method use below code snipped
CamelContext context = endpoint.getCamelContext();
List<Route> routes = context.getRoutes();
SftpEndpoint sftpEndpoint = (SftpEndpoint)endpoint;
Route failedRoute = routes.stream().filter(route -> ((SftpEndpoint) route.getEndpoint())
.getConfiguration().getDirectoryName().equals(sftpEndpoint.getConfiguration().getDirectoryName()))
.findAny().orElse(null);

Related

Camel appears to ignore onException instruction - what am I doing wrong?

I have a Camel (2.21.2) route that gets an XML file via FTP and then splits it before processing further, somewhat like...
from("direct-vm:start")
.split()
.xtokenize("*/row", 'w', ns)
.streaming()
...
If an empty file is passed, I am getting a com.ctc.wstx.exc.WstxEOFException raised by the xtokenize step I presume, which seems perfectly reasonable.
However, when I try to put in an onException clause, it doesn't seem to be catching the exception.
I have:
onException(com.ctc.wstx.exc.WstxEOFException.class)
.handled(true)
.log(org.apache.camel.LoggingLevel.INFO,
"myLogger",
"File ${header.CamelFileName} is empty or invalid XML");
and yet the exception is resulting in the route failing with the exception, and my handler log message is never produced.
However, if I change the onException clause to java.lang.Exception.class, it does handle the exception.
I don't want this to handle any exception, just the one caused by an empty or incomplete file.
What I am doing wrong here? How do I get onException to handle only the exception I am interested in?
Thanks for looking!

Apache Camel NotifyBuilder not working as expected

I am trying to test an error handling route. The NotificationBuilder does not work as expected (it always returns false).
Created a main route and a test route to test the main route. I used Spring Boot - all other tests work fine, so there is no problem with the setup I guess.
errorHandler(deadLetterChannel("seda:errorQueue").maximumRedeliveries(5).redeliveryDelay(1000));
from("file://{{inputFolder}}?delay=10s&noop=true")
.routeId("InputFolderToTestSedaRoute")
.setHeader("myHeader", constant("MY_HEADER_CONSTANT_VALUE"))
.to("seda://testSeda")
.log(LoggingLevel.DEBUG, "**** Input File Pushed To Output Folder *****");
There is an error route too.
from("seda:errorQueue")
.routeId("ErrorHandlingRoute")
.log("***** error body: ${body} *****")
.log("***** Exception Caught: ${exception} *****");
I then used adviceWith() to throw an Exception in the main route.
Then created the NotifyBuilder.
NotifyBuilder errorRouteNotifier = new NotifyBuilder(camelContext)
// .wereSentTo("seda:errorQueue")
.fromRoute("ErrorHandlingRoute*")
.whenReceived(1)
.create();
Then I sent message using ProducerTemplate. But when I test the match condition, it always fails.
boolean done = errorRouteNotifier.matches(5, TimeUnit.SECONDS);
assertTrue("Should have thrown Exception and caught at errorQueue", done);
But the message is routed to the ErrorHandlingRoute as I can see the messages (body and exception) I asked to print there.
Please let me know what is the issue here. Thanks in advance.

Apache Camel route with no "to" endpoint

I am using Apache Camel to assist with capturing message data emitted by a third party software package. In this particular instance, I only need to capture what is produced by the software, there is no receiver on the other end (really no "end" to go to).
So, I tried to set up a route with just the "from" endpoint and no "to" endpoint. Apparently this is incorrect usage as I received the following exception:
[2018-08-15 11:08:03.205] ERROR: string.Launcher:191 - Exception
org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> From[mina:udp://localhost:9877?sync=false] <<< in route: Route(route1)[[From[mina:udp://localhost:9877?sync=false]] -... because of Route route1 has no output processors. You need to add outputs to the route such as to("log:foo").
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1063)
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:196)
at org.apache.camel.impl.DefaultCamelContext.startRoute(DefaultCamelContext.java:974)
at org.apache.camel.impl.DefaultCamelContext.startRouteDefinitions(DefaultCamelContext.java:3301)
at org.apache.camel.impl.DefaultCamelContext.doStartCamel(DefaultCamelContext.java:3024)
at org.apache.camel.impl.DefaultCamelContext.access$000(DefaultCamelContext.java:175)
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2854)
at org.apache.camel.impl.DefaultCamelContext$2.call(DefaultCamelContext.java:2850)
at org.apache.camel.impl.DefaultCamelContext.doWithDefinedClassLoader(DefaultCamelContext.java:2873)
at org.apache.camel.impl.DefaultCamelContext.doStart(DefaultCamelContext.java:2850)
at org.apache.camel.support.ServiceSupport.start(ServiceSupport.java:61)
at org.apache.camel.impl.DefaultCamelContext.start(DefaultCamelContext.java:2819)
at {removed}.Launcher.startCamel(Launcher.java:189)
at {removed}.Launcher.main(Launcher.java:125)
Caused by: java.lang.IllegalArgumentException: Route route1 has no output processors. You need to add outputs to the route such as to("log:foo").
at org.apache.camel.model.RouteDefinition.addRoutes(RouteDefinition.java:1061)
... 13 more
How do I set up a camel route that allows me to intercept (capture) the message traffic coming from the source, and not send it "to" anything? There is no need for a receiver. What would be an appropriate "to" endpoint that just drops everything it receives?
The exception suggestion of to("log:foo"). What does this do?
You can see if the Stub component can help
http://camel.apache.org/stub.html
Example:
from("...")
.to("stub:nowhere");
The exception suggestion of to("log:foo"). What does this do?
It sends your route messages to an endpoint with a component of type log:
(http://camel.apache.org/log.html) - component which basically dumps message contents (body and/or headers and/or properties) to your log file using appropriate log category.
If you just want to drop everything received, it's a good choice:
to("log:com.company.camel.sample?level=TRACE&showAll=true&multiline=true")
Apparently if you're under Linux or Unix, you can also redirect to /dev/null like in this example:
to( "file:/dev?fileName=null")
I am not sure it can be used on Windows but I don't think so.
Note that the syntax: to( "file:/dev/null") does not work as it point to a directory called null but with the fileName option it will work.

java.io.IOException: Authorization loop detected on Conduit in Apache Camel

org.apache.cxf.interceptor.Fault: Could not send Message.
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:514)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:416)
at org.apache.camel.component.cxf.CxfProducer.process(CxfProducer.java:120)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:141)
at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
at org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:163)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)
at org.apache.camel.spring.spi.TransactionErrorHandler.processByErrorHandler(TransactionErrorHandler.java:218)
at org.apache.camel.spring.spi.TransactionErrorHandler.process(TransactionErrorHandler.java:99)
Camel version - 2.16.1.
Caused by: java.io.IOException: Authorization loop detected on Conduit "webserviceURL here..."
trying to call a WS published, from "route id=1" which is available in "routeContext id=1", from "route id=2" which is defined in "routeContext id=2"..
WS - defined using CXF-endpoint..
Both the "routeContext" has "http:conduit" defined..
Tried removing one of it..Not successful..
The solution is,
you have to be more specific of the scope of http:conduit, can be acheived by providing the specific name to the configured conduit, eg: http:conduit name="abcd.http", will be available only to the 'abcd', here abcd~=Webservice

Camel SFTP Route fails to continue onException

I've fairly simple looking route
sftp://hostname:22//incoming/folder/location/?username=username&password=xxxxx
&localWorkDirectory=/tmp&readLock=changed&readLockCheckInterval=2000
&move=processed/$simple{date:now:yyyy}/$simple{date:now:MM}/$simple{date:now:dd}${file:name}
&consumer.delay=450000&stepwise=false&streamDownload=true&disconnect=true
I also have an onException clause
onException(ValidationException.class)
.handled(true)
.logStackTrace(true)
.filter(header("VALIDATION_ERROR").isEqualTo(true))
.choice()
.when(header("CamelFileName").contains("Param1"))
.to(sftp://hostname:22//One/error/folder?password=xxxxxx&username=username)
.when(header("CamelFileName").contains("Param2"))
.to(sftp://hostname:22//Two/error/folder?password=xxxxxx&username=username)
.endChoice();
When I have single file, the route seems to work as expected. When more than one file and exception occurs, I get many different exceptions like
org.apache.camel.component.file.GenericFileOperationFailedException: Cannot list directory: incoming/folder/location
Caused by: java.lang.IndexOutOfBoundsException
I tried using all the attributes mentioned in the route viz. streamDownload, stepwise, readLock, localWorkDirectory et al. However, the error handling while multiple files is not working. I see the first file getting processed. However, it doesn't move to the processed folder once the exception occurs and then incoming/folder/location becomes non listable. I tried using continued(true) as well instead of handled(true)
The problem was with multiple files being handled in the same exchange. On exception, the route was trying to FTP back the error file on the same server. The solution was to split body into multiple exchanges so that each file has its own exchange and process them separately.
from(sftp://hostname:22//incoming/folder/location/?username=username&password=xxxxx
&localWorkDirectory=/tmp&readLock=changed&disconnect=true&stepwise=false
&move=processed/$simple{date:now:yyyy}/$simple{date:now:MM}/$simple{date:now:dd}${file:name}
&consumer.delay=450000).split(body()).processRef("incomingProcessor").end();

Resources