gpt4 book ai didi

java - spring cloud aws多个sqs监听器

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:45:17 25 4
gpt4 key购买 nike

我的项目中有 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;
}
};
}

关于java - spring cloud aws多个sqs监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55163339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com