如何配置自定义Spring Cloud AWS SimpleMessageListenerContainerFactory使其与@SqsListener一起使用 [英] How to configure custom Spring Cloud AWS SimpleMessageListenerContainerFactory so it keeps working with @SqsListener

查看:316
本文介绍了如何配置自定义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天全站免登陆