0

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) ?
kds
  • 28,155
  • 9
  • 38
  • 55
  • a "jar" is not an executable artifact, you can't "share a camelcontext between jar". But you can share an instance in a war between servlets or filters. You should clarify what you really want. – Jérémie B Mar 09 '16 at 11:59
  • @jérémie-b well, basically, I have three jars which has different type of routes. So I have a war which has added about three jars as maven artifacts and different ejb services. What I need is to access routes whenever I activate those ejb services. So How do I handle this without adding those into a one CamelContext ? – kds Mar 09 '16 at 12:05
  • @jérémie-b - As a matter of fact We can "Share a camelContext" between jar" . I'm in the process of implementing and testing on this. Subsequently, Will publish the outcome. – kds Mar 21 '16 at 10:25

2 Answers2

0

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

Chubutin
  • 165
  • 2
  • 9
  • #Chubutin, well basically i'm trying out with the Camel CDI with 2.16.2 . I have already went through the links which you have mentioned. I was able to deploy a camel CDI implementation in a single war and adding routers to the same camel context and that working perfectly. However, as I explained in the previous comments what I need know is that if I have a one camel context in a separate war or a jar, and if my routers are in another jar or war, how can I connect to those routers from the initial war ? How do I start those routes and connect to those routers? (I have edited the question) – kds Mar 10 '16 at 08:49
  • Oh, sorry, but I haven't done it with CDI. You might use the next release 2.17.x and use this annotations : http://camel.apache.org/cdi.html#CDI-CameleventstoCDIevents. Although, I don't know if this is available yet so I can't say which is its behavior – Chubutin Mar 14 '16 at 17:41
  • Have you read about the component [direct-vm](http://camel.apache.org/direct-vm.html)? It might be useful for you. You can have multiples camel context and route messages between them through this component. – Chubutin Mar 15 '16 at 12:15
  • Well I dont think its a good idea to go for direct-vm for these type of scenarios. Infact i'm not looking at OSGI implementation as well. There should be a way to handle with Camel CDI. Will do a bit of research on this. – kds Mar 21 '16 at 10:26
0

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.

kds
  • 28,155
  • 9
  • 38
  • 55
  • I'm getting the same issue with wildfly 10.1.0. This patch is not compatible with that and this is not resolved with 4.8.0 patch in there. So, how to do this with 10.1.0 Final. – ironwood Oct 03 '17 at 07:00