apache-camelapache-camel-cdi

How to use same CamelContext in multiple jar on the same war


I'm using the camel 2.16.2 and I need to use the one CamelContext across multiple jars as I need to have all the Camel Routers in to one CamelContext. So my war will have all those jars as maven artifacts.

Please let me know how do I handle above scenario?

Edit

Just to elaborate more on above question. In my war myApp.war, I have initialized the CamelContext. There are three jars myApp1.jar, myApp2.jar and myApp3.jar. Each jar has it own routers defined separately.

  1. How do I start the routers in each jar ?
  2. Can I use the same CamelContext injected to each routers?
  3. If I cannot handle through jars, is it possible to implement with multiple war (myApp1.war, myApp2.war and myApp3.war) and each war having different camelContext and communicate to those routers from the main war (myApp.war) ?

Solution

  • After doing some research found a way to implement this. Infact we can use the same CamelContext across different jars as all jars are in the same war (Web Container).

    We can implement easily with Apache Camel 2.16.2 with camel CDI. If you're using wildfly to deploy your war then you may need to add the camel patch. Download the the wildfly 9.0.2 pach

    Steps are Given Below.

    In your war create a servlet or restService and Inject the camelContext.

    @Inject
    @ContextName("cdi-context")
    private CamelContext camelctx;
    

    Create a router in the jar with below annotation.

    @Startup
    @ApplicationScoped
    public class MyJRouteBuilder extends RouteBuilder {
    

    In Configure method add

    @Override
    public void configure() throws Exception {
        from("direct:startTwo").routeId("MyJRouteBuilder")
        .bean(new SomeBeanThree());
    }
    

    Create a BootStrap Class in your jar and add the Router

    @Singleton
    @Startup
    public class BootStrap {
    
    private CamelContext camelctx;
    
    @PostConstruct
    public void init() throws Exception {   
        camelctx.addRoutes(new MyJRouteBuilder());
    }
    

    Add your jar as a artifact in the war pom.xml. Once you deploy the war you can see MyJRouteBuilder is Registred in the cdi-context CamelContext. So now you can access your Router anywhere you want.

    Hope this would useful anyone who has the same issue what I had.