如何让 Camel FTP 仅按需获取一次

2022-01-19 00:00:00 java apache-camel camel-ftp

我对 Camel 还很陌生.

I'm fairly new to Camel.

我一直试图让 Camel 根据需要仅通过 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,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:152) TelechargementMetadonneesRouteBuilder- 未连接/登录,连接到:ftp://pierre@localhost:212015-08-05 08:55:28,943 信息 [相关 ID:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,用户:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:156) TelechargementMetadonneesRouteBuilder- 连接并登录到:ftp://pierre@localhost:212015-08-05 08:55:28,945 调试 [相关 ID:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,用户:] [(camelContext)-1] (org.apache.camel.component.file.GenericFileConsumer:130) TelechargementMetadonneesRouteBuilder- 轮询耗时 0.002 秒:2015-08-05 08:55:28,945 调试 [相关 ID:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,用户:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:121) TelechargementMetadonneesRouteBuilder- 断开连接:ftp://pierre@localhost:212015-08-05 08:55:29,446 调试 [相关 ID:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,用户:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:152) TelechargementMetadonneesRouteBuilder-未连接/登录,连接到:ftp://pierre@localhost:212015-08-05 08:55:29,447 信息 [相关 ID:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,用户:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:156) TelechargementMetadonneesRouteBuilder- 连接并登录到:ftp://pierre@localhost:212015-08-05 08:55:29,449 调试 [相关 ID:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,用户:] [(camelContext)-1] (org.apache.camel.component.file.GenericFileConsumer:130) TelechargementMetadonneesRouteBuilder- 轮询耗时 0.002 秒:2015-08-05 08:55:29,449 调试 [相关 ID:faad163b-f68e-4e91-b5fd-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);

相关文章