SimpleMessageListenerContainer Amazon SQS Pollinterval
Posted
技术标签:
【中文标题】SimpleMessageListenerContainer Amazon SQS Pollinterval【英文标题】: 【发布时间】:2018-10-08 14:51:35 【问题描述】:我正在使用 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;
【问题讨论】:
【参考方案1】:调用包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;
【讨论】:
谢谢@Raj。这与在 AWS 控制台中作为 SQS 配置的一部分设置的长轮询间隔有什么区别? 好问题。帮助说“长轮询接收呼叫在返回空响应之前等待消息可用的最长时间。”。我的理解是,如果你在控制台中设置 15 秒为最大值,你可以在 Java 客户端中选择 1 到 15 秒。 谢谢,我去测试一下。 @PunterVicky 根据 AWS 文档,轮询请求中的正值胜过队列属性:对于 ReceiveMessage 操作的 WaitTimeSeconds 参数,设置在 1 到 20 之间的值优先于为队列设置的任何值属性 ReceiveMessageWaitTimeSeconds(见docs.aws.amazon.com/AWSSimpleQueueService/latest/…) @Raj 你建议扩展 AbstractMessageListenerContainer 吗?还是通过 Spring 应用上下文访问 SimpleMessageListenerContainer 并修改设置?以上是关于SimpleMessageListenerContainer Amazon SQS Pollinterval的主要内容,如果未能解决你的问题,请参考以下文章