2

I need to implement a custom PollingConsumerPollStrategy implementation on a route inside a RouteBuilder. The examples I found use spring to create a bean, but i am not using Spring in my project.

How do i add MyPollStrategy to registry and use it as pollStrategy=#myPoll ?

public class MyFtpServiceBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        // Want to add to below route &pollStrategy=#myPoll

        from("sftp://tmpserver.example.com:22//tmp/testfolder?password=xxxxxx&username=tmpuser")
            .routeId("testRoute")
            .to("file:C:/tmp/testfolder")

    }

    private class MyPollStrategy implements PollingConsumerPollStrategy {
        public boolean begin(Consumer consumer, Endpoint endpoint) {
            return true;
        }
        public void commit(Consumer consumer, Endpoint endpoint, int polledMessages) {
            if (polledMessages > maxPolls) {
                maxPolls = polledMessages;
            }
            latch.countDown();
        }
        public boolean rollback(Consumer consumer, Endpoint endpoint, int retryCounter, Exception cause) throws Exception {
            return false;
        }
    }
}
Xstian
  • 8,184
  • 10
  • 42
  • 72
Sumeet Kashyap
  • 143
  • 4
  • 15

2 Answers2

3

You can create an instance of SimpleRegistry where you can add your custom bean. And then pass in the simple registry instance to where you create CamelContext with the new DefaultCamelContext(myRegistry) constructor.

If you have a copy of Camel in Action book, see the beans chapter, it explain all about this in more details.

On the web site there is a little details at: http://camel.apache.org/registry.html

Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
  • Thanks for the answer Claus. I am using `MainSupport` for starting the route. How do i create an instance of `SimpleRegistry` and add to the `MainSupport` ? – Sumeet Kashyap Dec 24 '15 at 04:20
  • I am stuck on the same thing. I have to create a new route builder within the route builder in order to attach the registry. – Chad Van De Hey Feb 24 '17 at 00:02
-1

As i was using the org.apache.camel.main.Main, I could not figure out a way to create a SimpleRegistry as Claus mentioned and pass it to the main object.

Just found out that there is a method bind in Main class to pass bean name and bean object.

Main main = new Main();
main.addRouteBuilder(new MyTestRouteBuilder());
main.enableHangupSupport();
main.bind("myPoll", new MyPollStrategy());
main.run();
Sumeet Kashyap
  • 143
  • 4
  • 15