Home » Java » Enterprise Java » AmazonSQS and Spring for messaging queue

About Idan Fridman

Idan Fridman
Idan is Software engineer with experience in Server side technologies. Idan is responsible for various infrastructure models in the software industry(Telecommunications, Finance).

AmazonSQS and Spring for messaging queue

The next post will demonstrate how to use Spring JMS templates and DLMC’S together with AmazonSQS API in order to place message queue.

Why would I use Amazon SQS?

  1. Easy to configure
  2. Cross-platfom support
  3. Earn from your self redundant, conjunction and scaling worries.

 
 
 
 
Why I wouldn’t use Amazon SQS?

  1. If the latency requirement demands less than  ~20 MS
  2. Costs ~0.00005$ per message

I came across a nice open source project called: Nevado which wrapping the Amazon SQS API in a very neat way.

Add this to your maven dependency:

<dependency>
 <groupId>org.skyscreamer</groupId>
 <artifactId>nevado-jms</artifactId>
 <version>1.2.4</version>
 </dependency>

Now let’s configure Spring beans to integrate nicely with AmazonSQS:

1. Connection factory:

<bean id="sqsConnectorFactory" class="org.skyscreamer.nevado.jms.connector.amazonaws.AmazonAwsSQSConnectorFactory"/>

Pay attention that within this wrapper we need to set aws.accessKey and aws.secretKey. We get those keys from AmazonSQS account portal:

<bean id="connectionFactory" class="org.skyscreamer.nevado.jms.NevadoConnectionFactory">
 <property name="sqsConnectorFactory" ref="sqsConnectorFactory"/>
 <property name="awsAccessKey" value="${aws.accessKey}"/>
 <property name="awsSecretKey" value="${aws.secretKey}"/>

</bean>

2. Create the queue

<bean id="myQueue" class="org.skyscreamer.nevado.jms.destination.NevadoQueue">
 <constructor-arg value="${aws.sqs.queue.name}"/>
 </bean>

3. Create Jms template (which will be injected later in the code to send messages):

<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
 <property name="defaultDestinationName" value="${aws.sqs.queue.name}"/>
 <property name="connectionFactory" ref="cachedConnectionFactory"/>
 </bean>

4. Add listeners:

  • 4.a I am using SimpleMessageListenerContainer which has the ability to cache connections, run concurrent consumers, set error listeners and more.
    <bean id="simpleMessageListenerContainer" class="org.springframework.jms.listener.SimpleMessageListenerContainer">
     <property name="connectionFactory" ref="connectionFactory"/>
     <property name="messageListener" ref="listener"/>
     <property name="destination" ref="myQueue"/>
     <property name="errorHandler" ref="amazonMessageListener"/>
     <property name="concurrency" value="20"/>
     <property name="taskExecutor" ref="listenerThreadPoolTaskExecutor"/>
     </bean>
  • 4.b Let’s add Thread pool for the listener’s executors:
    <bean id="listenerThreadPoolTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
     <property name="corePoolSize" value="70"/>
     <property name="maxPoolSize" value="70"/>
     <property name="daemon" value="true"/>
     <property name="keepAliveSeconds" value="60"/>
     </bean>
  • 4.c Add Caching connections support:
    <bean id="cachedConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
     <property name="targetConnectionFactory" ref="connectionFactory"/>
     <property name="sessionCacheSize" value="10"/>
     </bean>
  • 4.d Create MessageAdapter to hook everything up and to set pojo as our message listener(amazonMessageListener):
    <bean id="listener" class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
     <property name="delegate" ref="amazonMessageListener"/>
     <property name="defaultListenerMethod" value="onMessage"/>
     <property name="defaultResponseDestination" ref="myQueue"/>
     </bean>

 

Reference: AmazonSQS and Spring for messaging queue from our JCG partner Idan Fridman at the IdanFridman.com blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

One comment

  1. Thanks for the post
    I tired using nevado as suggested. The only issue is that the periodic test runs both SQS and SNS listTopics which failes we do not have SNS service configured or authorised for this user.
    Is there an alternative to Nevado? or better yet a way to configure it to not run SNS test?

    SEVERE: Connection test failed: Status Code: 403, AWS Service: AmazonSNS, AWS Request ID: d66653ac-ffb6-5eaf-b8d7-1e9d529dd6b7, AWS Error Code: AuthorizationError, AWS Error Message: User: arn:aws:iam::***************:user/********** is not authorized to perform: SNS:ListTopics on resource: arn:aws:sns:**************:###############:*
    com.amazonaws.services.sns.model.AuthorizationErrorException: Status Code: 403, AWS Service: AmazonSNS, AWS Request ID: d66653ac-ffb6-5eaf-b8d7-1e9d529dd6b7, AWS Error Code: AuthorizationError, AWS Error Message: User: arn:aws:iam::##################:user/********* is not authorized to perform: SNS:ListTopics on resource: arn:aws:sns:*********:###############:*
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:767)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:414)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:228)
    at com.amazonaws.services.sns.AmazonSNSClient.invoke(AmazonSNSClient.java:2242)
    at com.amazonaws.services.sns.AmazonSNSClient.listTopics(AmazonSNSClient.java:849)
    at com.amazonaws.services.sns.AmazonSNSClient.listTopics(AmazonSNSClient.java:1413)
    at org.skyscreamer.nevado.jms.connector.amazonaws.AmazonAwsSQSConnector.test(AmazonAwsSQSConnector.java:115)

Leave a Reply

Your email address will not be published. Required fields are marked *

*


5 − = three

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close