Apache Camel:将 bean 添加到注册表以用于自定义轮询策略

Apache Camel: Add bean to registry for custom poll strategy

我需要在 RouteBuilder 内的路由上实现自定义 PollingConsumerPollStrategy 实现。我发现的示例使用 spring 创建一个 bean,但我没有在我的项目中使用 Spring。

如何将 MyPollStrategy 添加到注册表并将其用作 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;
        }
    }
}

您可以创建 SimpleRegistry 的实例,您可以在其中添加自定义 bean。然后将简单的注册表实例传递到您使用 new DefaultCamelContext(myRegistry) 构造函数创建 CamelContext 的位置。

如果你有一本 Camel in Action 书,请参阅 beans 章节,它对此进行了更详细的解释。

网站上有一些详细信息:http://camel.apache.org/registry.html

当我使用 org.apache.camel.main.Main 时,我想不出一种方法来创建 Claus 提到的 SimpleRegistry 并将其传递给主要对象。

刚发现Mainclass里面有个方法bind可以传bean名和bean对象

Main main = new Main();
main.addRouteBuilder(new MyTestRouteBuilder());
main.enableHangupSupport();
main.bind("myPoll", new MyPollStrategy());
main.run();