spring cloud aws multiple sqs listener

孤街浪徒 提交于 2020-07-20 17:00:51

问题


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());
    }
}

回答1:


ı 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 :)




回答2:


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;
        }
    };
}


来源:https://stackoverflow.com/questions/55163339/spring-cloud-aws-multiple-sqs-listener

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!