如何为多个队列管理器创建多个 MQQueueConnectionFactory

How to create multiple MQQueueConnectionFactory for multiple Queue Manager

我正在尝试配置一个@JMSListener,它将在我正在侦听 IBM MQ 的 Springboot 应用程序中侦听多个队列管理器。下面是我创建的 2 个 bean,要求是主动收听两个队列:

@Value("${ibm.mq.queueManager.A}")
String jmsMQConnectionFactoryA;

@Value("${ibm.mq.queueManager.B}")
String jmsMQConnectionFactoryB;

@Bean
@Primary
public MQQueueConnectionFactory jmsMQConnectionFactoryB() {
    MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
    URL url = JMSConfiguration.getURL(this.urlFileLocation);
    try {
        mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
        mqQueueConnectionFactory.setClientReconnectOptions(WMQConstants.WMQ_CLIENT_RECONNECT_Q_MGR);
        mqQueueConnectionFactory.setCCDTURL(url);
        mqQueueConnectionFactory.setQueueManager(jmsMQConnectionFactoryB);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mqQueueConnectionFactory;
}

   

    @Bean
    public MQQueueConnectionFactory jmsMQConnectionFactoryB() {
        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
        URL url = JMSConfiguration.getURL(this.urlFileLocation);
        try {
            mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            mqQueueConnectionFactory.setClientReconnectOptions(WMQConstants.WMQ_CLIENT_RECONNECT_Q_MGR);
            mqQueueConnectionFactory.setCCDTURL(url);
            mqQueueConnectionFactory.setQueueManager(jmsMQConnectionFactoryA);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mqQueueConnectionFactory;
    }

编辑 1:

使用上述连接添加了 2 个 containerFactory 和 2 个 JMSTemplate(因为我也需要发布消息)下面是更新后的工作代码

@Bean
    JmsListenerContainerFactory<?> jmsContainerFactoryA() {
        DefaultJmsListenerContainerFactory factory = new 
        DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(jmsMQConnectionFactoryA());
        factory.setRecoveryInterval((long) 60000);
        factory.setSessionAcknowledgeMode(2);
        factory.setSessionTransacted(true);
        // factory.setMaxMessagesPerTask(concurrencyLimit);
        return factory;
    }

@Bean
JmsListenerContainerFactory<?> jmsContainerFactoryB() {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    factory.setConnectionFactory(jmsMQConnectionFactoryB());
    factory.setRecoveryInterval((long) 60000);
    factory.setSessionAcknowledgeMode(2);
    factory.setSessionTransacted(true);
    // factory.setMaxMessagesPerTask(concurrencyLimit);
    return factory;
}

@Bean
public MQQueue jmsResponseQueue() {
    MQQueue queue = null;
    try {
        queue = new MQQueue("QueueOut");
        queue.setBaseQueueName(wmq_out_base_queue);
    } catch (JMSException e) {
        e.printStackTrace();
    }
    return queue;
}

@Bean
public JmsTemplate jmsTemplateB() {
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(jmsMQConnectionFactoryB());
    template.setDefaultDestination(jmsResponseQueue());
    template.setMessageConverter(oxmMessageConverter());
    return template;
}

@Bean
public JmsTemplate jmsTemplateA() {
    JmsTemplate template = new JmsTemplate();
    template.setConnectionFactory(jmsMQConnectionFactoryA());
    template.setDefaultDestination(jmsResponseQueue());
    template.setMessageConverter(oxmMessageConverter());
    return template;
}

Class2.java

 @Value("${wmq_out_base_queue}")
    String wmq_out_base_queue;

    @Autowired
    JmsTemplate jmsTemplateA;
    
    @Autowired
    JmsTemplate jmsTemplateB;

   @JmsListener(containerFactory="jmsContainerFactoryA",destination = "${wmq_in_base_queue}")
    public void reciveMessageA(Message message) {
      LOGGER.info("Received message is: " + message); 
      this.jmsTemplateA.convertAndSend(wmq_out_base_queue, "Some Message");
   }
 
  @JmsListener(containerFactory="jmsContainerFactoryB",destination = "${wmq_in_base_queue}")
    public void reciveMessageB(Message message) {
      LOGGER.info("Received message is: " + message); 
      this.jmsTemplateA.convertAndSend(wmq_out_base_queue, "Some Message2");
   }

您需要禁用 JMS 自动配置并配置 2 个连接工厂、2 个使用这些连接工厂的容器工厂和 2 个 JmsTemplate(如果您还发布)。

I am trying to configure one @JMSListener which will listen to multiple queue managers

为了让 @JmsListener 执行此操作,您需要向该方法添加 2 个 @JmsListener 注释,每个注释都将 containerFactory 属性 设置为各自的容器工厂。