j-money
j-money

Reputation: 529

Camel stop context when it is finished

If my understanding is correct, then camel routes do not have a 'complete' state therefore it doesn't make sense to say something like

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

and in most examples I've seen it is written something like

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

I have a data transformation of somewhere in the ballpark of 25Gb and I have no idea how long this will take. So what would be best practice here? (I was thinking maybe grossly overestimate the time to complete, and then try and fine tune from there using the logging messages from my route)

the route:

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

Upvotes: 3

Views: 2118

Answers (3)

Prakhar
Prakhar

Reputation: 1075

As mentioned in the earlier answer org.apache.camel.main.Main class is what you are looking for. The run() method in this class would start another thread and will be active unless you manually terminate its execution. For standalone integrations that should run for longer durations, have a look at this reference in the apache camel website long-running-camel-integrations

In short, you would end up doing something like this.

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!");}
}

}

You can have a look at working example here - apache-camel-kafka

Upvotes: 3

Luca Burgazzoli
Luca Burgazzoli

Reputation: 1236

camel spring boot has a property named camel.springboot.duration-max-messages that may help.

Upvotes: 0

hgrey
hgrey

Reputation: 3093

So you just want to wait until execution of the route is complete, right? One way is to use one of org.apache.camel.main.MainSupport implementations, for example org.apache.camel.spring.javaconfig.Main. The code could be something like:

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

Upvotes: 0

Related Questions