使用spring jmsListener监听多个队列

c6ubokkw  于 2022-11-29  发布在  Spring
关注(0)|答案(1)|浏览(197)

在这篇文章中,Garry Russell解释了如何通过编程方式创建多个KafkaListener来监听多个主题..[这个设置实际上对我来说很成功] Kafka Spring: How to create Listeners dynamically or in a loop?
现在,我希望对JMSListener也有一个类似的设置--其中我可以有一个包含一个@JMSListener的类,并且可以通过编程方式创建该JMSListener的多个示例,每个示例都注入了自己的queueName。
我找到了这个帖子Spring JMS start listening to jms queues on request
在这篇文章的最后,Gary发表了类似的评论,
如果您希望动态地创建许多容器,则只需以编程方式创建容器,调用afterPropertiesSet(),然后调用start()
我使用了上面第一篇文章中的设置(与KafkaListeners相关),JMS侦听器的多个示例正在启动,但没有使用任何消息。
基本上我不明白我在哪里做这个
然后以编程方式创建容器,调用afterPropertiesSet(),再调用start()
我对容器这个词感到困惑,我知道有JMSListener和JmsListenerContainerFactory,在这个上下文中什么是容器-我猜是JMSListener?
我已经确认了队列中有消息。而且,当我没有以编程方式创建侦听器,而只是有一个带有硬编码队列的侦听器时,它会很好地使用消息。
当我以编程方式创建多个JMS侦听器时,基本上没有侦听器使用消息

@SpringBootApplication
@EnableJms
public class MqProdConsumerApplication {
    private static Logger logger = LogManager.getLogger(MqProdConsumerApplication.class.getName());
    private static Consumers consumersStatic;

    @Autowired
    Consumers consumers;

    @PostConstruct
    public void init() {
        consumersStatic = this.consumers;
    }

    @Bean
    public Gson gson() {
        return new Gson();
    }

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(MqProdConsumerApplication.class, args);
        List<QueueInformation> queueInformationList = consumersStatic.getQueueInformationList();
        Assert.notEmpty(queueInformationList, "queueInformationList cannot be empty");
        logger.debug("queueInformationList ************" + queueInformationList.toString());
        for (QueueInformation queueInformation : queueInformationList) {
            AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
            child.setParent(context);
            child.register(MQConfig.class);
            Properties props = new Properties();
            props.setProperty("mqQueueName", queueInformation.getMqQueueName());
            //
            PropertiesPropertySource pps = new PropertiesPropertySource("listenerProps", props);
            child.getEnvironment().getPropertySources().addLast(pps);
            child.refresh();
        }
    }
}

下面是包含listenerContainerFactory的MQConfig

@Configuration
public class MQConfig {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${ibm.mq.user}")
    private String mqUserName;

    @Bean
    public MQListener listener() {
        return new MQListener();
    }

    @PostConstruct
    public void afterConstruct() {
        logger.debug("************* initialized MQ Config successfully for user =" + mqUserName);
    }

    @Bean
    public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
                                                    DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);

        // Put the MQ username in the PCF environment.
        // Otherwise, the connection is identified by PCF's default user, "VCAP"
        System.setProperty("user.name", mqUserName);
        return factory;
    }
}

然后是MQListener,它具有实际的@JMSListener

public class MQListener {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${mqQueueName}")
    private String mqQueueName;

    @PostConstruct
    public void afteConstruct() {
        logger.debug("************* initialized MQ Listener successfully, will read from =" + mqQueueName);

    }

    @JmsListener(destination = "${mqQueueName}", containerFactory = "myFactory")
    public void receiveMessage(String receivedMessage) throws JAXBException, ExecutionException, InterruptedException {
        logger.debug("***********************************************receivedMessage:" + receivedMessage);
    }
}

这是我的申请表.yml

ibm.mq.queueManager: ABCTOD01
ibm.mq.channel: QMD00.SERVER
ibm.mq.connName: mqdv1.devfg.ABC.com
ibm.mq.user: pmd0app1
ibm.mq.password:
consumers:
  queueInformationList:
  -
    mqQueueName: QMD00.D.SRF.PERSON.LITE.PHONE.LOAD
  -
    mqQueueName: QMD00.D.SRF.PERSON.PHONE.LOAD
y4ekin9u

y4ekin9u1#

好的,我找到了另一个帖子,加里已经回答了我正在寻找的Adding Dynamic Number of Listeners(Spring JMS)
基本上这是我的工作解决方案。伟大的工作@GaryRussell -我现在是一个球迷:)

@Configuration
@EnableJms
public class AppConfig implements JmsListenerConfigurer {

    @Override
    public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
        List<QueueInformation> queueInformationList = consumersStatic.getQueueInformationList();
        int i = 0;
        for (QueueInformation queueInformation :
                queueInformationList) {
            SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
            endpoint.setId("myJmsEndpoint-" + i++);
            endpoint.setDestination(queueInformation.getMqQueueName());
            endpoint.setMessageListener(message -> {
                logger.debug("***********************************************receivedMessage:" + message);
            });
            registrar.registerEndpoint(endpoint);
            logger.debug("registered the endpoint for queue" + queueInformation.getMqQueueName());
  }
}

另请参阅https://docs.spring.io/spring/docs/current/spring-framework-reference/integration.html#jms-annotated-programmatic-registration

相关问题