3

我正在尝试为独立队列实例化多个SimpleMessageListenerContainer并定义自定义,此设置的原因是我不想创建一个共享线程池配置在所有消息侦听器队列之间共享的位置。TaskExecutorMessageListenerSimpleMessageListenerContainer

你能提供一个建议吗?我尝试遵循一些 spring-messaging-aws 测试用例方法,但似乎不起作用?

@Bean
public SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory() {
    SimpleMessageListenerContainerFactory msgListenerContainerFactory = new SimpleMessageListenerContainerFactory();
    msgListenerContainerFactory.setAmazonSqs(amazonSQSClient());
    return msgListenerContainerFactory;
}

@Bean
public SimpleMessageListenerContainer uplinkMessageListenerContainer() throws Exception {
    SimpleMessageListenerContainer msgListenerContainer = simpleMessageListenerContainerFactory().createSimpleMessageListenerContainer();
    msgListenerContainer.setAmazonSqs(amazonSQSClient());
    QueueMessageHandler messageHandler = new QueueMessageHandler();
    msgListenerContainer.setMessageHandler(messageHandler);
    StaticApplicationContext applicationContext = new StaticApplicationContext();
    applicationContext.registerSingleton("uplinkMessageListener", UplinkMessageListener.class);

    messageHandler.setApplicationContext(applicationContext);
    msgListenerContainer.setBeanName("testContainerName");
    messageHandler.afterPropertiesSet();
    msgListenerContainer.setMaxNumberOfMessages(maxNumberOfmessages);
    msgListenerContainer.setWaitTimeOut(waitTimeout);
    msgListenerContainer.setBackOffTime(backOfTime);
    msgListenerContainer.setAutoStartup(sqsAutoStartup);
    msgListenerContainer.setVisibilityTimeout(visibilityTimeout);
    msgListenerContainer.setTaskExecutor(createDefaultTaskExecutor("MPSQSUplinkMessageListener", 2, 10));
    msgListenerContainer.afterPropertiesSet();
    msgListenerContainer.start();
    return msgListenerContainer;
}
4

1 回答 1

0

我找不到比使用调度程序创建服务更好的解决方案。

@Component
public class AmazonService {

  @Scheduled("0 */5 * * * *")
  public void checkSqs() {
     // Check sqs
  }
}

SQS 将通过惰性机制或通过服务中的构造函数进行实例化。

于 2021-07-18T09:12:46.567 回答