我试图让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);
}
}
答案 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}}。