Disable SqsListener on specific instances

667 views Asked by At

I have a spring boot application where i am using SQS for some async tasks. I want to configure 2 types of instances in this application:

  1. Server instance: responsible for pushing message to SQS queue.
  2. Worker instance: responsible for listening messages from SQS queue.

For listening messages I am using @SqsListener annotation like this:

    @SqsListener("queue_name")
    public void loadMessagesFromQueue(Object message) {
        log.info("inside loadMessagesFromQueue");
        log.info("Queue Messages: " + message);
    }

The ask here is that i want to disable this SqsListener on the server instance and just want it to run on the worker instance. How do I achieve this? I have tried searching if there is a way to disable it using some config variable but didn't find anything

1

There are 1 answers

0
akhil On

I have figured out a way to do it. I have created 2 separate YAML files for server and worker instances and configured a variable cloud.aws.sqs.autoStart in both the YAML files. I have kept the value of autoStart as True on the worker instance and as False on the server instance.

While creating the SimpleMessageListenerContainerFactory I am using this variable like this:

    @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs) {
        SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
        factory.setAmazonSqs(amazonSqs);
        factory.setAutoStartup(autoStart);
        factory.setMaxNumberOfMessages(5);


        return factory;
    }