如何让Camel FTP按需仅获取一次 [英] How to get Camel FTP to fetch only once on demand

查看:176
本文介绍了如何让Camel FTP按需仅获取一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚来骆驼。

我一直在尝试让骆驼按需通过FTP仅一次获取单个文件。我无法正常工作。这是我尝试过的。让我知道什么是最好的方法,我的代码有什么问题。

I've been trying to get Camel to fetch a single file via FTP only once, on demand. I couldn't get it to work. Here is what I've tried. Let me know what is the best approach and what is wrong with my code.

1-读取文件后发送空消息,并在收到空消息后停止路由。

1- Send an empty message after the file was read and when the empty message is received, stop the route.

 from("direct:myStart")
    .routeId("routeA")
    .pollEnrich("ftp:...disconnect=true&sendEmptyMessageWhenIdle=true")
    .choice()
       .when(body().isNull()).process(RouteStopper("routeA"))
       .otherwise().to("validator:file.xsd")
    .to("somewhere")
    .end();

我的RouteStopper

And my RouteStopper

public class RouteStopper implements Processor {

private String routeId;
private Thread stopper;

public RouteStopper(String routeId) {
    this.routeId = routeId;
}
@Override
public void process(Exchange exchange) throws Exception {
    if (stopper == null) {
        stopper = new Thread() {
            @Override
            public void run() {
                try {
                    exchange.getContext().stopRoute(routeId);
                } catch (Exception e) {
                }
            }
        };
    }
    stopper.start();
}

当我第一次调用 producerTemplate时,它实际上可以正常工作.sendBody( direct:myStart,null);

但是我第二次调用 producerTemplate.sendBody( direct:myStart,为null); 我知道了:

But the second time I call producerTemplate.sendBody("direct:myStart", null); I get this:

org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: Endpoint[direct://myStart]. Exchange[Message: [Body is null]]

我想我想做的就是不要停止我的路完全...所以我尝试了另一种方法...

I guess what I want to do is not to stop my road completely... so I tried another way...

2-实施PollingConsumerPollStrategy

public class FtpPollStrategy implements PollingConsumerPollStrategy {

@Override
public boolean begin(Consumer consumer, Endpoint endpoint) {
    return true;
}

@Override
public void commit(Consumer consumer, Endpoint endpoint, int polledMessages) {
    try {
        consumer.stop();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception cause) throws Exception {
    return false;
}

}

在我的路线上:

from("ftp:....disconnect=true&pollStrategy=#ftpPollStrategy")

我得到的是

java.lang.IllegalArgumentException: Could not find a suitable setter for property: pollStrategy as there isn't a setter method with same type: java.lang.String nor type conversion possible: No type converter available to convert from type: java.lang.String to the required type: org.apache.camel.spi.PollingConsumerPollStrategy with value #ftpPollStrategy

3-如果我什么都没停止(如下面的注释中所建议),则表明该路由永远不会停止轮询我的FTP 找不到文件 >。我只给路线打过一次:

3 - If I don't stop anything (as suggested in the comments below) it looks like the route never stops polling my FTP when no file is found. I called my route only once :

producerTemplate.sendBody("direct:myStart", null);

和路线

 from("direct:myStart")
.routeId("routeA")
.pollEnrich("ftp:...disconnect=true&sendEmptyMessageWhenIdle=true")
.to("validator:file.xsd")
.to("somewhere")
.end();

查看日志:

2015-08-05 08:55:28,942调试[correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,用户:] [(camelContext)-1](org.apache.camel.component.file .remote.RemoteFileConsumer:152)TelechargementMetadonneesRouteBuilder-未连接/已登录,正在连接至:ftp:// pierre @ localhost:21
2015-08-05 08:55:28,943 INFO [[correlation-ids:faad163b-f68e -4e91-b5fd-e5e2f86d42bd,用户:] [(camelContext)-1](org.apache.camel.component.file.remote.RemoteFileConsumer:156)TelechargementMetadonneesRouteBuilder-已连接并登录到:ftp:// pierre @ localhost: 21
2015-08-05 08:55:28,945调试[correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,用户:] [[camelContext)-1](org.apache.camel.component。 file.GenericFileConsumer:130)TelechargementMetadonneesRouteBuilder-花费0.002秒进行轮询:
2015-08-05 08:55:28,945 DEBUG [correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext )-1](org.apache.camel.co mponent.file.remote.RemoteFileConsumer:121)TelechargementMetadonneesRouteBuilder-断开连接:ftp:// pierre @ localhost:21
2015-08-05 08:55:29,446 DEBUG [correlation-ids:faad163b-f68e-4e91- b5fd-e5e2f86d42bd,用户:] [(camelContext)-1](org.apache.camel.component.file.remote.RemoteFileConsumer:152)TelechargementMetadonneesRouteBuilder-未连接/登录,连接至:ftp:// pierre @ localhost: 21
2015-08-05 08:55:29,447信息[correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,用户:] [(camelContext)-1](org.apache.camel.component。 file.remote.RemoteFileConsumer:156)TelechargementMetadonneesRouteBuilder-已连接并登录到:ftp:// pierre @ localhost:21
2015-08-05 08:55:29,449 DEBUG [correlation-ids:faad163b-f68e-4e91 -b5fd-e5e2f86d42bd,用户:] [(camelContext)-1](org.apache.camel.component.file.GenericFileConsumer:130)TelechargementMetadonneesRouteBuilder-花费0.002秒进行轮询:
2015-08-05 08:55 :29,449 DEBUG [correlation-ids:faad163b-f68e-4e91-b5f d-e5e2f86d42bd,用户:] [(camelContext)-1](org.apache.camel.component.file.remote.RemoteFileConsumer:121)TelechargementMetadonneesRouteBuilder-断开与:ftp:// pierre @ localhost:21

任何帮助将不胜感激。

推荐答案

我发现的解决方案使用的是方案1,但请停止并重新启动路由。

So the solution I found is using the scenario #1, but stop and restart the route.

exchange.getContext().stopRoute(routeId);
exchange.getContext().startRoute(routeId);

这篇关于如何让Camel FTP按需仅获取一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆