Question

I am writing my first camel application. it is a standalone application with a main method. As starting point i used the maven camel java archetype. It provides a simple main method that calls main.run().

Now i re-factored it a little bit and pulled the main.run out in a new class (and method) that will be my main-control of all camel stuff. Now i want to create the "opposite" method of run(). At the moment i want to implement tests for single routs that start (run()) the context then wait (at the moment i am unsure how to wait 'til a route is finished) and the stop the context.

But now i discovered many method that could start and stop stuff all in Main class. The Jvadoc didn't help - that some methods are inherited doesn't make it easier ;-). So someone please tell me the exact meaning (or use case) for:

Main.run()
Main.start()
Main.stop()
Main.suspend()
Main.resume()

Thanks in advance.

Was it helpful?

Solution

See this page about the lifecycle of the various Camel services

And for waiting until a route is finished, then you can check the inflight registry if there is any current in-flight exchanges to know if a route is finished.

OTHER TIPS

We must separate the methods into 2 groups. The first is the one described in the life cycle http://camel.apache.org/lifecycle The second is composed of run and shutdown.

run runs indefinitely and can be stopped when invoking shutdown, the latter must be invoked in a different thread and sent before the run invocation.

Example:

import org.apache.camel.main.Main;

public class ShutdownTest {

    public static void main(String[] args) throws Exception {

       Main camel = new Main();

       camel.addRouteBuilder( new MyRouteBuilder() );

       // In this case the thread will wait a certain time and then invoke shutdown.
       MyRunnable r = new MyRunnable(5000, camel);

       r.excecute();

       camel.run();
    }
}

Simple Runnable class

public class MyRunnable implements Runnable {

    long waitingFor = -1;
    Main camel;

    public MyRunnable(long waitingFor, Main camel){
        this.waitingFor = waitingFor;
        this.camel = camel;
    }

    public void excecute(){

        Thread thread = new Thread(this);

        thread.start();
    }

    @Override
    public void run() {

        try {
            synchronized (this) {
                    this.wait( waitingFor );
            }
        } catch (InterruptedException e) {
        }

        try {
            System.out.println("camel.shutdown()");
            camel.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top