我对Camel来说相当新鲜。
我一直试图让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
任何帮助将不胜感激。
答案 0 :(得分:0)
所以我找到的解决方案是使用方案#1,但停止并重新启动路由。
exchange.getContext().stopRoute(routeId);
exchange.getContext().startRoute(routeId);
答案 1 :(得分:-1)
您可以使用CountDownLatch来停止此处指定的路线 - http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html。它对我来说就像魅力一样......