0

我对骆驼相当陌生。

我一直在尝试让 Camel 仅按需通过 FTP 获取单个文件。我无法让它工作。这是我尝试过的。让我知道什么是最好的方法以及我的代码有什么问题。

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

 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

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);得到了这个:

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

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

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 。我只给我的路线打了一次电话:

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 DEBUG [correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:152) TelechargementMetadonneesRouteBuilder- Not connected/logged in, connecting to: ftp://pierre@localhost:21 2015-08-05 08:55:28,943 INFO [correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:156) TelechargementMetadonneesRouteBuilder- Connected and logged in to: ftp://pierre@localhost:21 2015-08-05 08:55:28,945 DEBUG [correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.GenericFileConsumer:130) TelechargementMetadonneesRouteBuilder- Took 0.002 seconds to poll: 2015-08-05 08:55:28,945 DEBUG [correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:121) TelechargementMetadonneesRouteBuilder- Disconnecting from: ftp://pierre@localhost:21 2015-08-05 08:55:29,446 DEBUG [correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:152) TelechargementMetadonneesRouteBuilder- Not connected/logged in, connecting to: ftp://pierre@localhost:21 2015-08-05 08:55:29,447 INFO [correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:156) TelechargementMetadonneesRouteBuilder- Connected and logged in to: ftp://pierre@localhost:21 2015-08-05 08:55:29,449 DEBUG [correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.GenericFileConsumer:130) TelechargementMetadonneesRouteBuilder- Took 0.002 seconds to poll: 2015-08-05 08:55:29,449 DEBUG [correlation-ids:faad163b-f68e-4e91-b5fd-e5e2f86d42bd,user:] [(camelContext)-1] (org.apache.camel.component.file.remote.RemoteFileConsumer:121) TelechargementMetadonneesRouteBuilder- Disconnecting from: ftp://pierre@localhost:21

任何帮助将不胜感激。

4

2 回答 2

0

所以我找到的解决方案是使用场景#1,但停止并重新启动路线。

exchange.getContext().stopRoute(routeId);
exchange.getContext().startRoute(routeId);
于 2015-08-07T16:06:55.297 回答
-1

您可以使用 CountDownLatch 从此处指定的路线停止路线 - http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html。它对我来说就像魅力一样..

于 2015-08-06T11:33:02.137 回答