防止 spring-cloud-aws-messaging 尝试停止队列 [英] Prevent spring-cloud-aws-messaging from trying to stop the queue

查看:79
本文介绍了防止 spring-cloud-aws-messaging 尝试停止队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring Boot 项目中使用 spring-cloud-aws-messaging.
我在 AWS 中手动创建了 SQS 队列.

I'm using spring-cloud-aws-messaging in a Spring Boot project.
I have SQS queue created manually in AWS.

它的用法如下:

@SqsListener("${sqs.name.incoming}")
public void listen(String message) {
    ...
}

它工作正常.但是当我在 IDE 中停止我的应用程序时,或者当 Spring Boot 测试完成时,它会尝试停止队列.它无法阻止它并最终超时.它抛出这个异常:

It works fine. But when I stop my application in IDE, or when the Spring Boot tests finish, it tries to stop the queue. It can't stop it and eventually times out. It throws this exception:

2019-10-29 15:40:07.949  WARN 10378 --- [       Thread-2] s.c.a.m.l.SimpleMessageListenerContainer : An exception occurred while stopping queue 'my-awesome-queue-name'

java.util.concurrent.TimeoutException: null
    at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:204) ~[na:na]
    at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.waitForRunningQueuesToStop(SimpleMessageListenerContainer.java:161) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.doStop(SimpleMessageListenerContainer.java:140) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.cloud.aws.messaging.listener.AbstractMessageListenerContainer.stop(AbstractMessageListenerContainer.java:351) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.stop(SimpleMessageListenerContainer.java:45) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.cloud.aws.messaging.listener.AbstractMessageListenerContainer.stop(AbstractMessageListenerContainer.java:239) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.cloud.aws.messaging.listener.SimpleMessageListenerContainer.stop(SimpleMessageListenerContainer.java:45) ~[spring-cloud-aws-messaging-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.doStop(DefaultLifecycleProcessor.java:238) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.access$300(DefaultLifecycleProcessor.java:53) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.stop(DefaultLifecycleProcessor.java:377) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.stopBeans(DefaultLifecycleProcessor.java:210) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.DefaultLifecycleProcessor.onClose(DefaultLifecycleProcessor.java:128) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:1018) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext$1.run(AbstractApplicationContext.java:945) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]

这种等待会减慢应用程序或测试关闭.

This waiting slows down application or tests shutdown.

我如何告诉 spring-cloud-aws-messaging 它是一个手动创建的队列并且不应该尝试关闭它?

How do I tell spring-cloud-aws-messaging that it is a manually created queue and it should not try to shut it down?

推荐答案

实际上并不是关闭 SQS 队列,而是关闭正在轮询队列的正在运行的任务.如SimpleMessageListenerContainer.queueStopTimeout.

It is not actually shutting down the the SQS queue rather it is shutting down the running task that is polling the queue. The default timeout to shut this down is 10 secs as defined in SimpleMessageListenerContainer.queueStopTimeout.

当任务轮询队列时,它会按照 SimpleMessageListenerContainerFactory.waitTimeOut 中的设置等待 1 到 20 秒的消息.如果此轮询恰好大于 10 秒,则上述关闭将超时,导致 TimeoutException.

When the task is polling the queue, it waits for messages from 1 to 20 secs as set in SimpleMessageListenerContainerFactory.waitTimeOut. If this poll happens to be greater than 10 secs, then the above shutdown will timeout causing the TimeoutException.

所以要解决这个问题,要么将 queueStopTimeout 增加到大于 20

So to fix this, either you increase the queueStopTimeout to be greater than 20

@PostConstruct
public void setUpListenerContainer() {
    listenerContainer.setQueueStopTimeout(25000);
}

或将轮询 waitTimeOut 减少到小于 10.

or decrease the poll waitTimeOut to be less than 10.

@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs) {
    SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
    factory.setWaitTimeOut(5);
    return factory;
}

这篇关于防止 spring-cloud-aws-messaging 尝试停止队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆