How to get Camel FTP to fetch only once on demand

2019-03-06 15:11发布

问题:

I'm fairly new to Camel.

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- 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();

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();
}

It actually works fine the first time I call 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- Implement a 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;
}

}

And in my route:

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

And what I get is

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

And the Route

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

See the logs:

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

Any help will be appreciated.

回答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);


回答2:

You can use a CountDownLatch to stop the route from a route as specified here - http://camel.apache.org/how-can-i-stop-a-route-from-a-route.html. It works like charm for me..