2.13. Apache Camel Standalone 실행

독립 실행형 애플리케이션으로 camel을 실행하면 애플리케이션을 실행하고 JVM이 종료될 때까지 계속 실행할 수 있는 Main 클래스를 제공합니다. org.apache.camel.main Java 패키지 내에서 MainListener 클래스를 찾을 수 있습니다.

다음은 Main 클래스의 구성 요소입니다.

  • org.apache.camel.Main 클래스의 Camel-core JAR
  • org.apache.camel.spring.Main 클래스의 Camel- spring JAR

다음 예제에서는 Camel에서 Main 클래스를 생성하고 사용하는 방법을 보여줍니다.

public class MainExample {

    private Main main;

    public static void main(String[] args) throws Exception {
        MainExample example = new MainExample();
        example.boot();
    }

    public void boot() throws Exception {
        // create a Main instance
        main = new Main();
        // bind MyBean into the registry
        main.bind("foo", new MyBean());
        // add routes
        main.addRouteBuilder(new MyRouteBuilder());
        // add event listener
        main.addMainListener(new Events());
        // set the properties from a file
        main.setPropertyPlaceholderLocations("example.properties");
        // run until you terminate the JVM
        System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
        main.run();
    }

    private static class MyRouteBuilder extends RouteBuilder {
        @Override
        public void configure() throws Exception {
            from("timer:foo?delay={{millisecs}}")
                .process(new Processor() {
                    public void process(Exchange exchange) throws Exception {
                        System.out.println("Invoked timer at " + new Date());
                    }
                })
                .bean("foo");
        }
    }

    public static class MyBean {
        public void callMe() {
            System.out.println("MyBean.callMe method has been called");
        }
    }

    public static class Events extends MainListenerSupport {

        @Override
        public void afterStart(MainSupport main) {
            System.out.println("MainExample with Camel is now started!");
        }

        @Override
        public void beforeStop(MainSupport main) {
            System.out.println("MainExample with Camel is now being stopped!");
        }
    }
}