简体   繁体   English

如何识别SQS-Queue不存在?

[英]How to identify that an SQS-Queue does not exist?

I would expect that if the SQS queue specified in the following annotation does not exist, Spring will throw a NonExistentQueue exception at start time.我希望如果以下注释中指定的 SQS 队列不存在,Spring 将在启动时抛出 NonExistentQueue 异常。

@SqsListener(value = "nonExistingQueue", deletionPolicy = SqsMessageDeletionPolicy.ALWAYS)

But somehow no exception is thrown in my case and the application will be started without the queue.但不知何故,在我的案例中没有抛出异常,应用程序将在没有队列的情况下启动。 Why will be no exception thrown?为什么不会抛出异常? May it be disabled somehow?它可能以某种方式被禁用吗? Sorry, I am pretty new to AWS.抱歉,我是 AWS 的新手。 My goal is that my application couldn't even start without a queue present.我的目标是如果没有队列,我的应用程序甚至无法启动。 If an exception were thrown would it suffice, or maybe I need to create an @PostConstruct method that checks for existence and manually throws an exception?如果抛出异常就足够了,或者我可能需要创建一个 @PostConstruct 方法来检查是否存在并手动抛出异常?

My properties:我的属性:

cloud.aws.credentials.access-key=accesKey
cloud.aws.credentials.secret-key=secretKey
cloud.aws.region.auto=false
cloud.aws.region.static=eu-central-1
cloud.aws.stack.auto=false

When I create the SQS-Queue that SqlListened needs, the application works fine.当我创建 SqlListened 需要的 SQS-Queue 时,应用程序运行良好。 Here is my pom to check out my dependencies:这是我的 pom 来检查我的依赖项:

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
        <version>1.11.792</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-aws-autoconfigure</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-aws-context</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-aws-messaging</artifactId>
        <version>2.2.5.RELEASE</version>
    </dependency>

Thank you very much.非常感谢你。

I've requested this feature in spring-cloud-aws project: https://github.com/awspring/spring-cloud-aws/issues/393 just c/p my workaround here:我已经在spring-cloud-aws项目中请求了这个功能: https://github.com/awspring/spring-cloud-aws/issues/393只是 c/p 我的解决方法在这里:

  1. Custom destination resolver that throws RuntimeException that will not be catched by SimpleMessageListenerContainer :抛出SimpleMessageListenerContainer不会捕获的RuntimeException的自定义目标解析器:
public class FailingDestinationResolver<T> implements DestinationResolver<T> {
    
    private final DestinationResolver<T> destinationResolver;

    public FailingDestinationResolver(DestinationResolver<T> destinationResolver) {
        this.destinationResolver = destinationResolver;
    }

    @Override
    public T resolveDestination(String name) {
        try {
            return destinationResolver.resolveDestination(name);
        } catch (DestinationResolutionException e) {
            throw new RuntimeException("Fail to resolve '" + name + "' destination", e);
        }
    }
}
  1. Configure SimpleMessageListenerContainerFactory bean with custom queue resolver:使用自定义队列解析器配置SimpleMessageListenerContainerFactory bean:
@Bean
public SimpleMessageListenerContainerFactory sqsListenerContainerFactory(DestinationResolver<String> queueDestinationResolver) {
    SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
    factory.setDestinationResolver(queueDestinationResolver);
    return factory;
}
    
@Bean
public DestinationResolver<String> queueDestinationResolver(AmazonSQSAsync sqs) {
    return new CachingDestinationResolverProxy<>(new FailingDestinationResolver<>(new DynamicQueueUrlDestinationResolver(sqs, null)));
}

Note that spring cloud.aws.sqs.listener.* properties ignored for this configuration, additional code is required to pick them up from SqsProperties .请注意,此配置忽略了 spring cloud.aws.sqs.listener.*属性,需要额外的代码才能从SqsProperties中获取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM