如何配置自定义Spring Cloud AWS SimpleMessageListenerContainerFactory以便它继续使用@SqsListener

时间:2016-10-17 17:40:31

标签: spring-boot spring-cloud amazon-sqs

我试图让SpringCloud AWS SQS使用自定义SimpleMessageListenerContainerFactory,这样我就可以设置超时和最大数量的消息。没有自定义SimpleMessageListenerContainerFactory方法,这些方法使用SQS中的@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);
    }
}

1 个答案:

答案 0 :(得分:3)

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

@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 {{1}}。