完成时骆驼停止上下文

时间:2018-07-18 13:51:26

标签: java apache apache-camel

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

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

3 个答案:

答案 0 :(得分:2)

您正在寻找的是前面的答案org.apache.camel.main.Main类中提到的。此类中的run()方法将启动另一个线程,并且将被激活,除非您手动终止其执行。对于应该运行较长时间的独立集成,请在apache骆驼网站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

答案 1 :(得分:0)

所以您只想等到路由执行完成,对吗?一种方法是使用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

答案 2 :(得分:0)

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

相关问题