繁体   English   中英

spring cloud aws 多个 sqs 侦听器

[英]spring cloud aws multiple sqs listener

我的项目中有 2 个 sqs 侦听器。 我希望其中一个具有相同的设置,而其中一个具有不同的设置。 我想更改的唯一值是 maxNumberOfMessages。

最实用的方法是什么? 我想为监听器之一设置不同的 maxNumberOfMessages 值。

这是我的配置;

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

这是听众;

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

以下 hack 对我有用(如果每个听众收听不同的队列)

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

我找到了解决方案并在 github 上的示例 repo 上分享。 github链接

如果我在侦听器类上添加@EnableAsync 注释并将@Async 注释添加到处理程序方法我的问题正在解决:)

不幸的是,Sushant 的解决方案没有在 Kotlin 中为我编译(因为 QueueAttributes 是静态保护类),但我用它写了以下内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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