amazon-web-servicesspring-bootamazon-sqsspring-jmsregion

Multiple SQS Listeners from different AWS regions


We are currently consuming a single SQS queue to process messages. However as extension of functionality, we need to support multiple regions with same queue name.

Current implementation of jmsListener is tied to a given SQS queue in a given region as below:

SQSListener.java

@Component
public class SQSListener {

    @Override
    @JmsListener(destination = "${QueueName}", concurrency = "${JmsThreadCount}")
    public void onMessage(Message message) throws JMSException {

    }

SQSConfiguration.java

@Component
@EnableJms
public class SQSConfig {
    @Bean
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
         DefaultJmsListenerContainerFactory factory = null;
         try {
             factory = new DefaultJmsListenerContainerFactory();
             factory.setConnectionFactory(getSqsConnectionFactory()); //passes credentials in local method call
             factory.setDestinationResolver(new DynamicDestinationResolver());
             factory.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
         } catch (Exception e) {
             logger.error(e.getLocalizedMessage());
         }
         return factory;
    }



}

Application.properties

QueueName=xyz
JmsThreadCount=1-5
Regions=us-east-1, us-west-2 #*(newly added region)*

How can I make it generic to create multiple listener class implementations by regions specified in the configurations ?


Solution

  • Though there is no direct way using spring framework's annotations to create listeners to listen to multiple queue's in multiple AWS regions (same queue name in multiple AWS regions), I was able to solve above problem by programmatic configuration of JMS Container Factory beans for each region, JMS Templates for each region, and JMS Listeners for each region.

    I will post sample code in next few days, that may be useful to the community.