How to configure custom Spring Cloud AWS SimpleMessageListenerContainerFactory so it keeps working with @SqsListener

亡梦爱人 提交于 2019-12-23 16:50:32

问题


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;
}

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);
    }
}

回答1:


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;
    }

}

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



来源:https://stackoverflow.com/questions/40092518/how-to-configure-custom-spring-cloud-aws-simplemessagelistenercontainerfactory-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!