如何配置自定义 Spring Cloud AWS SimpleMessageListenerContainerFactory 以便它继续与 @SqsListener 一起工作 [英] How to configure custom Spring Cloud AWS SimpleMessageListenerContainerFactory so it keeps working with @SqsListener

查看:54
本文介绍了如何配置自定义 Spring Cloud AWS SimpleMessageListenerContainerFactory 以便它继续与 @SqsListener 一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让 SpringCloud AWS SQS 与自定义 SimpleMessageListenerContainerFactory 一起工作,以便我可以设置超时和最大消息数.如果没有使用 @SqsListener 注释的自定义 SimpleMessageListenerContainerFactory 方法,可以很好地拾取 SQS 中的消息.但是当我尝试配置自定义 SimpleMessageListenerContainerFactory 时,注释停止工作.

I am trying to get SpringCloud AWS SQS working with a custom SimpleMessageListenerContainerFactory so i can set timeouts and maxnumber of messages. Without the custom SimpleMessageListenerContainerFactory methods that are annotated with the @SqsListener nicely pickup messages that are in SQS. But when i try to configure a custom SimpleMessageListenerContainerFactory the annotation stops working.

@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory(AmazonSQSAsync amazonSqs) {
    SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
    factory.setAmazonSqs(amazonSqs);
    factory.setAutoStartup(true);
    factory.setMaxNumberOfMessages(10);
    factory.setWaitTimeOut(2000);
    return factory;
}

如何在定义自定义 SimpleMessageListenerContainerFactory 时获得正常的 @SqsListener 行为?

How can i get the normal @SqsListener behaviour when defining a custom SimpleMessageListenerContainerFactory?

@Component
public class SqsMessageConsumer {
    @SqsListener("incoming-data")
    private void doSomething(String payload) {
        System.out.println("data = " + payload);
    }
}

推荐答案

不确定您错过了什么,但有针对此类用例的测试:

Not sure what you have missed but there is a test exactly for such a use-case:

@EnableSqs
@Configuration
public static class ConfigurationWithCustomContainerFactory {


    @Bean
    public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory() {
        SimpleMessageListenerContainerFactory factory = new SimpleMessageListenerContainerFactory();
        factory.setAmazonSqs(amazonSQS());
        ...
        return factory;
    }

    @Bean
    public AmazonSQSAsync amazonSQS() {
        return AMAZON_SQS;
    }

}

所以,@EnaqbleSqs 仍然存在,SqsConfiguration@Autowired 和您的自定义 SimpleMessageListenerContainerFactory @Bean.

So, @EnaqbleSqs is still here and SqsConfiguration is @Autowired with your custom SimpleMessageListenerContainerFactory @Bean.

这篇关于如何配置自定义 Spring Cloud AWS SimpleMessageListenerContainerFactory 以便它继续与 @SqsListener 一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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