嵌入到另一个应用程序中时,Camel路由的行为会有所不同

时间:2017-10-18 09:14:36

标签: java maven apache-camel activemq

我是Apache Camel的新手,所以请耐心等待。我正在尝试将下面显示的简单camel路由集成到我的消息传递应用程序中,该应用程序从ActiveMQ服务器读取消息,处理它并将其发送回服务器。问题是,当下面的路由作为独立的camel应用程序执行时,它们工作正常(如预期的那样)。但是当嵌入我的应用程序时,它们的行为会有所不同。首先,即使某些路由不应自动启动,所有路由也会在启动驼峰上下文时启动。其次,不会调用camel上下文的startRoute和stopRoute方法。在我的pom文件中,我只有activemq-camel 5.6.0依赖。

public static void main(String[] args) {
    CamelContext context = new DefaultCamelContext();
    context.addComponent("activemq", ActiveMQComponent.activeMQComponent("tcp://127.0.0.1:61616"));
    try {
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("activemq:queue:oldEvents")
                .routeId("sn")
                .process(new AddSequenceNumber())
                .to("activemq:queue:queue-P1-in");

                from("activemq:queue:queue-P1-in")
                .routeId("order")
                .resequence(header("seqnum"))
                .process(new Untapped())
                .stream(new StreamResequencerConfig(5000, 5000L))
                .to("activemq:queue:newEvents");

                from("activemq:queue:queue-P1-in")
                .routeId("tapOrder")
                .noAutoStartup()
                .wireTap("direct:tapped-messages")
                .resequence(header("seqnum"))
                .stream(new StreamResequencerConfig(5000, 5000L))
                .to("activemq:queue:newEvents");

                from("direct:tapped-messages")
                .routeId("checker")
                .noAutoStartup()
                .process(new SourceEventsChecker());    
            }               
        });
        context.start();
    }catch(Exception e) {
        e.printStackTrace();
    }

    try {
        TimeUnit.SECONDS.sleep(10);
        context.stopRoute("order");
        TimeUnit.SECONDS.sleep(10);
        context.startRoute("checker");
        TimeUnit.SECONDS.sleep(10);
        context.startRoute("tapOrder");

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

我哪里出错了?我错过了什么?

在我的应用程序中,路由是在' getCamelContext'中实现的。返回camel上下文的方法。然后,从另一个类中调用该方法并使用上下文动态停止和启动路由,如下所示。

CamelContext context = MessageHandler.getCamelContext();
    try {
        context.stopRoute("order");
        context.startRoute("checker");
        context.startRoute("tap");
    } catch (Exception e`enter code here`) {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

如果您使用上面代码中显示的main类启动应用程序,请注意start方法是非阻塞调用。在Camel网站上阅读更多关于如何以更好的方式运行Camel standalone的信息。

例如,使用Apache Camel中的Main类并使用其run方法来保持JVM的运行。