gpt4 book ai didi

spring - SimpleMessageListenerContainer Amazon SQS 轮询间隔

转载 作者:行者123 更新时间:2023-12-02 03:39:45 27 4
gpt4 key购买 nike

我正在使用 Spring Cloud 库来轮询 SQS。如何设置轮询间隔?

@Bean
@Primary
public AmazonSQSAsync amazonSQSAsync() {
return AmazonSQSAsyncClientBuilder.standard().
withCredentials(awsCredentialsProvider()).
withClientConfiguration(clientConfiguration()).
build();
}


@Bean
@ConfigurationProperties(prefix = "aws.queue")
public SimpleMessageListenerContainer simpleMessageListenerContainer(AmazonSQSAsync amazonSQSAsync) {
SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
simpleMessageListenerContainer.setAmazonSqs(amazonSQSAsync);
simpleMessageListenerContainer.setMessageHandler(queueMessageHandler());
simpleMessageListenerContainer.setMaxNumberOfMessages(10);
simpleMessageListenerContainer.setTaskExecutor(threadPoolTaskExecutor());
return simpleMessageListenerContainer;
}


@Bean
public QueueMessageHandler queueMessageHandler() {
QueueMessageHandlerFactory queueMessageHandlerFactory = new QueueMessageHandlerFactory();
queueMessageHandlerFactory.setAmazonSqs(amazonSQSAsync());
QueueMessageHandler queueMessageHandler = queueMessageHandlerFactory.createQueueMessageHandler();
return queueMessageHandler;
}


@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(10);
executor.setThreadNamePrefix("oaoQueueExecutor");
executor.initialize();
return executor;
}

最佳答案

调用org.springframework.cloud.aws.messaging.listener中基类AbstractMessageListenerContainer的setWaitTimeOut(N)函数。 N 是长轮询超时(以秒为单位)。例如,如果您想在返回之前等待 5 秒,请在queueMessageHandler() 函数中使用以下代码行。如果不调用此函数,则默认为 1 秒。最大长轮询超时为 20 秒,因此您可以为此函数指定的最大值为 20,这意味着“等待 20 秒”

simpleMessageListenerContainer.setWaitTimeOut (5);

源代码在这里:https://github.com/spring-cloud/spring-cloud-aws/blob/master/spring-cloud-aws-messaging/src/main/java/org/springframework/cloud/aws/messaging/listener/AbstractMessageListenerContainer.java

/**
* Configures the wait timeout that the poll request will wait for new message to arrive if the are currently no
* messages on the queue. Higher values will reduce poll request to the system significantly.
*
* @param waitTimeOut
* - the wait time out in seconds
*/
public void setWaitTimeOut(Integer waitTimeOut) {
this.waitTimeOut = waitTimeOut;
}

关于spring - SimpleMessageListenerContainer Amazon SQS 轮询间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50067821/

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