DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Develop a Spring Boot REST API in AWS: PART 4 (CodePipeline / CI/CD)
  • AWS Spring Boot Tutorial, Part 3: CodeDeploy, Blue/Green Deployment
  • Develop Spring Boot REST API in AWS - Part 2: ECS Cluster
  • Develop Spring Boot REST API in AWS - PART 1/4 (CodeBuild + ECR)

Trending

  • 5 Web3 Trends to Follow in 2023
  • Analyzing Stock Tick Data in SingleStoreDB Using LangChain and OpenAI's Whisper
  • OneStream Fast Data Extracts APIs
  • Five Free AI Tools for Programmers to 10X Their Productivity
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Creating a Message Driven Bean With AWS SQS in Spring

Creating a Message Driven Bean With AWS SQS in Spring

Want to whip up an MDB using AWS SQS in Spring? Here's a neat tutorial.

$$anonymous$$ user avatar by
$$anonymous$$
·
Oct. 08, 15 · Tutorial
Like (2)
Save
Tweet
Share
4.60K Views

Join the DZone community and get the full member experience.

Join For Free

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 
Spring Framework AWS

Published at DZone with permission of $$anonymous$$. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Develop a Spring Boot REST API in AWS: PART 4 (CodePipeline / CI/CD)
  • AWS Spring Boot Tutorial, Part 3: CodeDeploy, Blue/Green Deployment
  • Develop Spring Boot REST API in AWS - Part 2: ECS Cluster
  • Develop Spring Boot REST API in AWS - PART 1/4 (CodeBuild + ECR)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: