Showing posts with label apache camel. Show all posts
Showing posts with label apache camel. Show all posts

June 25, 2012

JMS Configuration in Spring's ApplicationContext


Recently I faced some issue to configure JMS queue in Spring's application context. This queue is exposed on Sonic ESB and I have to connect to it using Apache Camel. First I wrote 
a JMS Producer class which puts message on client's sonic JMS queue. It was something like this:
Hashtable properties = new Hashtable();

properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.test.factory");
properties.put("com.domain", "DevelopmentDomain");
properties.put(Context.PROVIDER_URL, "tcp://10.00.0.00:0506");
properties.put(Context.SECURITY_PRINCIPAL, "aaa");
properties.put(Context.SECURITY_CREDENTIALS, "aaa");

javax.naming.Context context = new javax.naming.InitialContext(properties);
ConnectionFactory factory = (ConnectionFactory) context.lookup("ImpactPocQueueConnectionFactory");

Connection connection = factory.createConnection();

connection.start();

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination queue = session.createQueue("test.producer");
Now above JMS queue setup in applicationContext xml using Camel route.Here I am routing message to sonic queue through a seda queue.

     
          
             com.test.factory
             DevelopmentDomain
             tcp://10.00.0.00:0506
             aaa
             aaa
          
     


     
     

 
     



    
    

May 18, 2012

Read Apache Camel Configuration from a Property file


Requirement is to read camel configuration like route uri value from a property file. If the property file is available in camel context then it can be used to dynamic routes, endpoint, uri values etc.
Camel provides propertyPlaceHolder to load property file from classpath.

   
Here ${env} is declared as a -D option in server config as -Denv=preprod. Property file preprod.properties has a URI entry for bean endpoint as endpoint.bean = bean:testBean. Camel context could access this property with following syntax:

Using Camel with Spring is a common practice and property file can be loaded with the help of Spring's propertyPlaceHolder but using it with is tricky. Spring's property placeHolder to load the file:

  

There are two ways to use it with camel.But first I would change the property value to endpoint.bean=testBean and do mention endpoint type in route.

Another way to achieve it, is.


  
  

May 10, 2012

Apache Camel: Send Message to SEDA Queue


Objective is to send the input to a bean method via SEDA queue. In this way, send a message to the seda queue and route the message to a bean method.
Take the following steps to achieve it:

  1. SEDA is a in-memory queue provided by Camel Framwork. To know more about it refer to SEDA. Camel automatically creates the SEDA component. There is no need to create it expilicitly like other supported components.First declare SEDA queue in camel context:
    
     
    
    When application starts with above context, camel automatically creates a seda queue with the name of "inputSEDAQueue".
  2. As per the requirement, message from seda queue will be input to a bean method. Add a bean route to the camel context
    
     
     
    
    
    Bean definition in application context will be same as any other bean in spring application context.
    
    
  3. To send a message to seda queue, we need camel context in java class. There are two ways to get it:
    1. Add the camel context as a property to spring bean and initialize in class with its getter and setter. Code will look like this:
      
         
      
      
      Java Class needs to include:
      public static CamelContext camelContext;
       
      @Override
      public CamelContext getCamelContext() {
             return camelContext;
      }
      
      @Override
      public void setCamelContext(CamelContext camelContext) {
       this.camelContext = camelContext;
      }
      
    2. Easier way to access camelContext in java class is to implement CamelContextAware interface and implement the required methods. Change implemented method according to your camel context variable name and make sure that your camel context variable name should be same as the camel context id in the applicationContext xml. Now with the implementation of camelContextAware interface your class will look like:
      private static CamelContext camelContext;
       
       @Override
       public CamelContext getCamelContext() {
        return camelContext;
       }
      
       @Override
       public void setCamelContext(CamelContext camelContext) {
        this.camelContext = camelContext;
       }
      
      And the bean definition will not be changed :
      
      
  4. Last step would be to send the input to the seda queue. It needs just a one line code as
    getCamelContext.createProducerTemplate.sendBody("seda:inputSEDAQueue",inputValue)
    Here inputValue is the input method "processRequest" of the bean.

This should work fine. Getting the right camelContext would be tricky sometime. Having a Spring background makes it easy to debug.