Search code examples
spring-bootspring-cloudamazon-sqs

Listen to multiple AWS SQS from single SQSListener


I have a springboot application where sqslistener is configured to listen to one queue as shown below -

@SqsListener(value="${sqs.name}", deletionPolicy=SqsMessageDeletionPolicy.NEVER)
public void listen(Request request, Acknowledgment ack) {
logger.info("Message received {}",request);
    try {
        ack.acknowledge().get();
        logger.info("Deleted message"); 
    } catch (InterruptedException | ExecutionException e) {
        logger.error("Failed to delete message from the queue");
    }
    logger.info("Message processed.");
}

But, I have to change this to listen to 2 different queues. First, listener should listen to first queue, if no messages found then it should listen to second queue.

Is it possible to do it with the same listener ? If not, any idea to achieve this is helpful.


Solution

  • Is it possible to do it with the same listener ? If not, any idea to achieve this is helpful.

    No it is not possible to have the same listener for 2 different queues.

    First, listener should listen to first queue, if no messages found then it should listen to second queue.

    If there aren't messages in the Queue the listener that is listening to that queue won't receive any "input" so you don't have to worry about this scenario.

    If not, any idea to achieve this is helpful

    You must implement two different listeners to respectively listening to the QueueA and QueueB