其他分享
首页 > 其他分享> > 春天-SimpleMessageListenerContainer Amazon SQS Pollinterval

春天-SimpleMessageListenerContainer Amazon SQS Pollinterval

作者:互联网

我正在使用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-boot,amazon-web-services,spring-cloud,amazon-sqs,spring
来源: https://codeday.me/bug/20191109/2011371.html