小编典典

如何配置自定义Spring Cloud AWS SimpleMessageListenerContainerFactory,使其与@SqsListener一起使用

spring-boot

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

@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行为?

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

阅读 714

收藏
2020-05-30

共1个答案

小编典典

不确定您错过了什么,但是针对这种用例进行了准确的测试:

@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

2020-05-30