简体   繁体   中英

spring cloud aws multiple sqs listener

There are 2 sqs listener in my project. I want one of them to have the same setting and one of them different setting. The only value I want to change is maxNumberOfMessages.

What is the most practical way to do this ? ı want set different maxNumberOfMessages value for one of listener.

this is my config ;

@Bean
public AWSCredentialsProvider awsCredentialsProvider(@Value("${cloud.aws.profile}") String profile,
                                                     @Value("${cloud.aws.region.static}") String region,
                                                     @Value("${cloud.aws.roleArn}") String role,
                                                     @Value("${cloud.aws.user}") String user) {
    ...

    return new AWSStaticCredentialsProvider(sessionCredentials);
}

@Bean
@Primary
@Qualifier("amazonSQSAsync")
public AmazonSQSAsync amazonSQSAsync(@Value("${cloud.aws.region.static}") String region, AWSCredentialsProvider awsCredentialsProvider) {
    return AmazonSQSAsyncClientBuilder.standard()
            .withCredentials(awsCredentialsProvider)
            .withRegion(region)
            .build();
}

@Bean
@Primary
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs) {
    SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
    factory.setAmazonSqs(amazonSqs);
    factory.setMaxNumberOfMessages(1);
    factory.setWaitTimeOut(10);
    factory.setQueueMessageHandler(new SqsQueueMessageHandler());
    return factory;
}

This is listener;

@SqsListener(value = "${messaging.queue.blabla.source}", deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void listen(Message message, Acknowledgment acknowledgment, @Header("MessageId") String messageId) {
    log.info("Message Received");

    try {
        ....
        acknowledgment.acknowledge().get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage());
    }
}

Following hack worked for me (if each listener listens to different queue)

@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs) {

    return new SimpleMessageListenerContainerFactory() {
        @Override
        public SimpleMessageListenerContainer createSimpleMessageListenerContainer() {
            SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer() {
                @Override
                protected void startQueue(String queueName, QueueAttributes queueAttributes) {
                    
                    // A place to configure queue based maxNumberOfMessages
                    
                    try {
                        if (queueName.endsWith(".fifo")) {
                            FieldUtils.writeField(queueAttributes, "maxNumberOfMessages", 1, true);
                        }
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                    super.startQueue(queueName, queueAttributes);
                }
            };
            simpleMessageListenerContainer.setAmazonSqs(amazonSqs);
            return simpleMessageListenerContainer;
        }
    };
}

ı found the solution and share on example repo on github. github link

if ı add @EnableAsync annotation on listener class and @Async annotation to handler method my problem is solving :)

Unfortunately, the solution from Sushant didn't compile for me in Kotlin(because QueueAttributes is static protected class), but I used it to write following:

    @Bean
fun simpleMessageListenerContainerFactory(sqs: AmazonSQSAsync): SimpleMessageListenerContainerFactory =
    object : SimpleMessageListenerContainerFactory() {
        override fun createSimpleMessageListenerContainer(): SimpleMessageListenerContainer {
            val container = object : SimpleMessageListenerContainer() {
                override fun afterPropertiesSet() {
                    super.afterPropertiesSet()
                    registeredQueues.forEach { (queue, attributes) ->
                        if (queue.contains(QUEUE_NAME)) {
                            FieldUtils.writeField(
                                attributes,
                                "maxNumberOfMessages",
                                NEW_MAX_NUMBER_OF_MESSAGES,
                                true
                            )
                        }
                    }
                }
            }

            container.setWaitTimeOut(waitTimeOut)
            container.setMaxNumberOfMessages(maxNumberOfMessages)
            container.setAmazonSqs(sqs)
            return container
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM