0

I have created 2 different application and started the camel context in one of them. How do I use this already started context in the second application ?

I tried getting the context by using lookUpByname() and binding camel context with jndi context but could on load the existing context.

Also tried by setting NameStrategy in context in application 1 and getting the same in application 2 but looks like camel auto generates name and prefix in DefaultCamelContextNameStrategy.

code snippet:  

Application 1 :  

public static void main(String[] args)  
{  
CamelContext ctx = new DefaultCamelContext();  
String camelContextId= "sample";  
ctx.setNameStrategy(new DefaultCamelContextNameStrategy(
                camelContextId));  
ctx.start();  

}  

Application 2:  
public static void main(String[] args)  
{  
sampleRouter testobj = new sampleRouter();  
testobj.test();  
}  

public class sampleRouter extends RouteBuilder  
{  

public static CamelContext camelContext;  
public void test()  
try  
{  

camelContext = getContext();  
    try {
        camelContext.stop();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Please guide me to get the already started context in different applications as I want to avoid creating a new context every time.

Sat
  • 3
  • 2

1 Answers1

0

Why do you want to avoid having multiple CamelContexts? What goal are you trying to accomplish?
Without a clear requirement it's not easy to help you, however I'll try and suggest a couple of ideas.

Looking at your code you are using two different JVMs, since you have 2 main methods.
If your applications run in different JVMs, use a JMS Message Broker like ActiveMQ as communication layer.

If you deploy 2 wars / applications in the same JVM, you can use two CamelContexts and have them communicate through VM endpoints, like seda-vm and direct-vm.

Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64
  • Thanks Alessandro. We are using Apache camel as an implementation component for batch jobs to read and write files.We are using Rundeck as a scheduler, multiple jobs will be sceduled to run in a day, we do not want to create a new context for every job rather use the already started context.File Reader will be generic; it will use Camel as implementation component for file processing; conversely Camel will not be aware of running jobs but will act as implementation mechanism only When Rundeck triggers job.File Reader will get CamelContext and start Camel route for job passing metadata. – Sat Oct 11 '17 at 06:43