Dynamically modify SQS queue to an @SqsListener method at runtime?

1.2k Views Asked by At
@SqsListener(value = "foo", deletionPolicy = SqsMessageDeletionPolicy.ON_SUCCESS)
    public void fooMethod(final String message)  {
        processEvents(message);
    }

I am using spring-cloud-starter-aws-messaging - @SqsListener in my app which at startup loads foo queue just fine. But while the app is up and running processing events from the queue, if I want to change the queue to a different queue, say foo-test dynamically, is that possbile ?

Background: I am trying to run a test queue part of my deployment and the deployment would start with the test queue. Once messages from the test queue are processed and the test is successful, I want to dynamically change the queue to the production queue.

Please advise on how to achieve this. If there are alternative approaches, let me know. Thanks!

2

There are 2 best solutions below

0
On

You can intercept DestinationResolver.resolveDestination() and change it:

@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSQS) {
    SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
    factory.setAmazonSqs(amazonSQS);
    factory.setWaitTimeOut(15);
    factory.setDestinationResolver(destinationResolver(amazonSQS));
    return factory;
}

private DestinationResolver<String> destinationResolver(AmazonSQSAsync amazonSQS) {
    return new DestinationResolver<String>() {
        public String resolveDestination(String name) {
            if (name.equals(name1)) {
                return name2;
            }
            return name;
        }
    };
}

resolveDestination is called once, when SqlListener is initialized

0
On

AFAIK, It is not possible to switch the value dynamically. I am guessing that you want to do it so that your application only starts consuming production messages once you are sure that the app code is good enough and logic isn't broken.

if above assumption is correct, this should get verified when your app is promoted to TEST environment, or you can solve it using active/passive PROD deployments, where both deployments use different properties.