【问题标题】:Camel stop context when it is finished完成时骆驼停止上下文
【发布时间】:2018-12-26 11:29:48
【问题描述】:

如果我的理解是正确的,那么骆驼路线没有“完整”状态,因此说类似的话是没有意义的

camelContext.addRoute(route1);
camelContext.start();
while(0) 
{
    ifComplete(route1)
        break;
}
camelContext.stop();

在我见过的大多数例子中,它是这样写的

camelContext.start();
Thread.sleep(someDeterminedAmountOfTime);
camelContext.stop();

我在25Gb 的某个地方进行了数据转换,但我不知道这需要多长时间。那么这里的最佳实践是什么? (我在想可能严重高估了完成时间,然后尝试使用我路线中的日志消息从那里进行微调)

路线:

CsvDataFormat csv = new CsvDataFormat();
from(file:/path/to/file/?fileName=fileName&noop=true)
.split(body().tokenize("/n")).streaming()
.unmarshall(csv)
.process(new CsvParserProcess())
.marshal(csv)
.to(file:/path/to/new/file/?fileName=out.csv).log("finished").end();

【问题讨论】:

    标签: java apache apache-camel


    【解决方案1】:

    正如前面的答案中提到的那样,org.apache.camel.main.Main 类是您正在寻找的。此类中的run() 方法将启动另一个线程并且将处于活动状态,除非您手动终止它的执行。对于应该运行更长时间的独立集成,请查看 apache camel 网站long-running-camel-integrations中的此参考资料@

    简而言之,你最终会做这样的事情。

    public final class Application {
    
    private static Logger logger = LoggerFactory.getLogger(Application.class);
    
    public static void main(String[] args) {
        // This is the org.apache.camel.main.Main method
        final Main main = new Main();
    
        // Add lifecycle hooks to the main
        main.addMainListener(new Events());
    
        // Add routes to the camel context
        main.addRouteBuilder(new InvoiceGenerator());
        main.addRouteBuilder(new CustomerInvoicePayment());
        main.addRouteBuilder(new BankProcessPayment());
        main.addRouteBuilder(new CustomerNotificationProcessor());
        main.addRouteBuilder(new InvoiceNotificationProcessor());
    
        try {
            // Run the main method
            main.run();
        } catch (Exception e) {
            logger.error("Error starting Camel Application ", e);
            e.printStackTrace();
        }
    }
    
    // This class provides a few lifecycle hooks. Use them if required
    private static class Events extends MainListenerSupport {
        private static Logger logger = LoggerFactory.getLogger(Events.class);
    
        @Override
        public void afterStart(final MainSupport main) {logger.info("Camel app is now started!");}
    
        @Override
        public void beforeStop(final MainSupport main) {logger.info("Camel app is shutting down!");}
    }
    
    }
    

    您可以在这里查看工作示例 - apache-camel-kafka

    【讨论】:

      【解决方案2】:

      所以你只想等到路由执行完成,对吧?一种方法是使用org.apache.camel.main.MainSupport 实现之一,例如org.apache.camel.spring.javaconfig.Main。代码可能是这样的:

      Main main = new org.apache.camel.spring.javaconfig.Main();
      var springContext = createSpringContext();
      main.setApplicationContext(springContext);
      RouteBuilder route = //create route here 
      main.setRouteBuilders(Collections.singletonList(route));
      main.run();//this will block until the route completes
      

      【讨论】:

        【解决方案3】:

        camel spring boot 有一个名为 camel.springboot.duration-max-messages 的属性可能会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-11-10
          • 1970-01-01
          • 2014-10-27
          • 1970-01-01
          • 2018-08-15
          • 1970-01-01
          • 1970-01-01
          • 2017-08-15
          相关资源
          最近更新 更多