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

前端 未结 2 728
清歌不尽
清歌不尽 2021-01-16 19:10

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 a

相关标签:
2条回答
  • 2021-01-16 19:23

    As other guys said, you can't use the same CamelContext across different Jars. Could you explain a little what you want to do?


    IMHO what you want to do is use routes defined in different Jars. So for that you can define a Camel Context and add all the routes from different Jars. Of course your Camel-Context-JAR has to have access to all those jars.

     <camel:camelContext id="camel5">
      <camel:package>org.apache.camel.spring.example</camel:package>
    </camel:camelContext>
    

    Or class by class

      <camelContext id="camel5" xmlns="http://camel.apache.org/schema/spring">
        <routeBuilder ref="myBuilder" />    
      </camelContext>
    
      <bean id="myBuilder" class="org.apache.camel.spring.example.test1.MyRouteBuilder"/>
    

    Or if you are using CDI you can follow this great article https://dzone.com/articles/using-camel-routes-in-java-ee-components

    Reference: http://camel.apache.org/spring.html

    0 讨论(0)
  • 2021-01-16 19:40

    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.

    0 讨论(0)
提交回复
热议问题