Camel - Stop route when the consuming directory not exists

心已入冬 提交于 2019-12-05 17:16:19

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:

  1. Enable the option throwExceptionOnConnectFailed on the sftp endpoint like this:

    sftp://{{elmu.sftp.host}}:{{elmu.sftp.port}}{{elmu.sftp.importDir}}?username={{elmu.sftp.userName}}&amp;password={{elmu.sftp.password}}&amp;
    autoCreate=false&amp;preferredAuthentications=password&amp;binary=true&amp;include={{elmu.importMask}}&amp;initialDelay=100&amp;
    noop=true&amp;sortBy=file:name&amp;sendEmptyMessageWhenIdle=true&amp;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.

  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!