Enterprise Java

Creating a Message Driven Bean with AWS SQS in Spring

In my previous post I showed a simple example how to use AWS SQS with Spring Framework to put messages on a queue and to read them from the queue. In this post I go one step further and use Spring to create a ‘Message Driven Bean’ so each message that is put on the queue is picked up and processed ‘automatically’. This is called the asynchronous way by AWS on their documentation page. To do this I will define a MessageListener in Spring and configure it to listen to my queue as described here. To see the initial project setup please see my previous post as I won’t show it again here.

The Spring application context will define the Message Listener (and corresponding objects) like this:

<bean id="amazonMessageListener" class="net.pascalalma.aws.sqs.SpringMessageDrivenBean" />

  <bean id="messageListener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
    <property name="delegate" ref="amazonMessageListener"/>
    <property name="defaultListenerMethod" value="onMessage"/>
    <property name="messageConverter">
      <null/>
    </property>
  </bean>

  <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
    <property name="connectionFactory" ref="connectionFactory"/>
    <property name="destinationName" ref="queueName" />
    <property name="messageListener" ref="messageListener" />
  </bean>

First I have defined my MDB (MessageDrivenBean) bean and called it ‘amazonMessageListener’. Next I use this MDB as the ‘delegate’ for the ‘messageListener’ Adapter. This ‘adapter’ bean can also take care of converting the message payload (ignored here) and calling the correct method in the delegated listener.

In the ‘jmsContainer’ bean the ‘adapter’ is linked with the used JMS connection Factory and destination to listen to.

All that is left is the source code of the MDB itself:

package net.pascalalma.aws.sqs;

import org.apache.log4j.Logger;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;

public class SpringMessageDrivenBean {

    final static Logger logger = Logger.getLogger(SpringMessageDrivenBean.class);

    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            try {
                logger.info(String.format("MDB received: %s ", ((TextMessage) message).getText()));
            }
            catch (JMSException ex) {
                throw new RuntimeException(ex);
            }
        }
        else {
            throw new IllegalArgumentException("Message must be of type TextMessage");
        }
    }
}

This is fairly straightforward I would think. The method ‘onMessage’ gets called for each message that is put on the queue and it simply prints the text content of the message in this case. To see it working I use the following ‘main’ class:

package net.pascalalma.aws.sqs;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringMdbMain {

    public static void main(String[] args) {
        //Build application context by reading spring-config.xml
        ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"application-context.xml"});

        //Get an instance of ProviderService class;
        MyMessageProvider prdSvc = (MyMessageProvider) ctx.getBean("myMessageProviderService");

        //Call getProduct method of ProductService
        prdSvc.sendMessage("This is a test A");
        prdSvc.sendMessage("This is a test B");
        prdSvc.sendMessage("This is a test C");
        prdSvc.sendMessage("This is a test D");
    }
}

This results in the following output:

2015-04-11 13:17:20 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(23) - Sending message with txt: This is a test A
2015-04-11 13:17:26 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(36) - Message sent 
2015-04-11 13:17:26 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(23) - Sending message with txt: This is a test B
2015-04-11 13:17:26 INFO  net.pascalalma.aws.sqs.SpringMessageDrivenBean(16) - MDB received: This is a test A 
2015-04-11 13:17:26 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(36) - Message sent 
2015-04-11 13:17:26 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(23) - Sending message with txt: This is a test C
2015-04-11 13:17:26 INFO  net.pascalalma.aws.sqs.SpringMessageDrivenBean(16) - MDB received: This is a test B 
2015-04-11 13:17:27 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(36) - Message sent 
2015-04-11 13:17:27 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(23) - Sending message with txt: This is a test D
2015-04-11 13:17:27 INFO  net.pascalalma.aws.sqs.SpringMessageDrivenBean(16) - MDB received: This is a test C 
2015-04-11 13:17:27 DEBUG net.pascalalma.aws.sqs.MyMessageProvider(36) - Message sent 
2015-04-11 13:17:27 INFO  net.pascalalma.aws.sqs.SpringMessageDrivenBean(16) - MDB received: This is a test D

Pascal Alma

Pascal is a senior JEE Developer and Architect at 4Synergy in The Netherlands. Pascal has been designing and building J2EE applications since 2001. He is particularly interested in Open Source toolstack (Mule, Spring Framework, JBoss) and technologies like Web Services, SOA and Cloud technologies. Specialties: JEE, SOA, Mule ESB, Maven, Cloud Technology, Amazon AWS.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button