Camel - 消费目录不存在时停止路由

时间:2016-09-17 16:47:18

标签: apache-camel

我有一个SFTP路由(在Spring XML中),它的路径在每日更改目录(即/ yyyyMMdd)中结束,当autoCreate=true或目录存在时,一切都运行良好路线开始了。但是如果不存在则不允许我创建目录!

当dir存在时,路径获取文件并自行终止。

当目录不存在时,路线将永久轮询并发出警告(即org.apache.camel.component.file.GenericFileOperationFailedException: Cannot change directory to: 20160917)并永不停止。

如何避免此行为(例如,将警告转换为空消息或异常或......)?我已经使用startingDirectoryMustExist,consumer.bridgeErrorHandler以及其他许多方法进行了实验,但没有取得任何成功。

简化路线(在开始之前,用实际日期填充elmu.sftp.importDir属性):

    <from
        uri="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"/>
    <choice>
        <when>
            <simple>${body} != null</simple>
            ... a lot of stuff ...
            <to uri="bean:shutdownRoute" />
        </when>
        <otherwise>
            <to uri="bean:shutdownRoute" />
        </otherwise>
    </choice>

使用directoryMustExist=true and startingDirectoryMustExist=true结果是一个无限循环(轮询),并带有此警告:

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

它不适用于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

更新(根据@ ruffp&#39;答案):

我试图设置自定义PollingConsumerPollStrategy,但我无法阻止它的路由。只有第三个(注释行)停止路线,但我有几条路线,我不知道实际路线的名称。我怎么能得到它?

    @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;
    }

3 个答案:

答案 0 :(得分:2)

最后我用consumer.exceptionHandler解决了这个问题。但似乎这个选项不在可用选项列表中http://camel.apache.org/file2.html我一次又一次地阅读)刚刚提到的一个例子在巨大的底部页。不幸的是,它是如此“隐藏”,以至于我错过了它。

我设置了一个新类并实现了handleExceptions方法:

public class DirNotExistsExHandler implements ExceptionHandler

获取异常并决定该怎么做。在上下文中,我做了一个bean定义:

<bean id="dirNotExistsExHandler" class="hu.dbit.eleo.DirNotExistsExHandler" />

在使用者中,将bean传递给处理程序:

consumer.exceptionHandler=#dirNotExistsExHandler

非常感谢你的帮助!

答案 1 :(得分:1)

在我看来,最好的方法是:

  1. throwExceptionOnConnectFailed端点上启用选项sftp,如下所示:

    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
    

    这可以帮助管理异常,并通过驼峰路由onException来捕获它。但是我不确定在你的情况下是否真的有必要。

  2. 对您的连接例外进行特殊处理

    // 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");
    

    或另一种方式:

    // 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();
    
  3. 更新: 我发现this post显然没有其他方法可以实现您自己的PollingConsumerPollStrategy,因为GenericFileOperationFailedException显然是在默认实现中处理的。

答案 2 :(得分:0)

使用doCatch块处理异常。如果没有抛出异常,则使用您的代码存在检查文件并手动抛出异常。

http://camel.apache.org/try-catch-finally.html