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.

2 comments:

  1. Hi,
    This post is very useful to understand how send a message to a seda endpoint.
    My question, is it possible to consume messages from a seda endpoint as well?
    Thanks.

    ReplyDelete
  2. Thanks for the kind words. Yes it is possible to consume a message from a SEDA queue. SEDA queue can definitely be a starting point (<from uri="seda:queueName") of your camel route.

    ReplyDelete