Listen to multiple IBM MQ using single spring boot application
Asked Answered
R

1

7

I need to listen to multiple queues (exists in same Queue Manager). I have the working spring boot application code for listening to single queue. But is there any way I can connect to multiple queues from the single spring boot application?

Also is there anyway if I can switch listeners from one queue to another queue at runtime?

I have the code to read from single queue which is as follows:

public class ConnectionConfiguration {

  private static final Logger logger = LogManager.getLogger(ConnectionConfiguration.class);

  @Value("${LDR_LOCAL_MQ_READ_FACTORYNAME}")
  String connectionFactory;

  @Value("${LDR_LOCAL_MQ_QUEUENAME}")
  String localQueue;

  @Value("${jmsConcurrency}")
  String concurrency;

  @Value("${servers.mq.host}")
  private String host;

  @Value("${servers.mq.port}")
  private Integer port;

  @Value("${servers.mq.queue-manager}")
  private String queueManager;

  @Value("${servers.mq.channel}")
  private String channel;

  @Value("${servers.mq.queue}")
  private String queue;

  @Value("${servers.mq.timeout}")
  private long timeout;

  @Bean
  public ConnectionFactory connectionFactory() {

    JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
    jndiObjectFactoryBean.setResourceRef(true);
    jndiObjectFactoryBean.setJndiName(connectionFactory);
    try {
      jndiObjectFactoryBean.afterPropertiesSet();
    } catch (IllegalArgumentException e) {
      logger.error(e.getMessage(), e);
      e.printStackTrace();
    } catch (NamingException e) {
      logger.error(e.getMessage(), e);
      e.printStackTrace();
    }
    return (ConnectionFactory) jndiObjectFactoryBean.getObject();
  }

  @Bean
  public MQQueueConnectionFactory mqQueueConnectionFactory() {
    MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
    try {
      mqQueueConnectionFactory.setHostName(host);
      mqQueueConnectionFactory.setQueueManager(queueManager);
      mqQueueConnectionFactory.setPort(port);
      mqQueueConnectionFactory.setChannel(channel);
      mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
      mqQueueConnectionFactory.setCCSID(1208);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return mqQueueConnectionFactory;
  }

  @Bean
  public SimpleMessageListenerContainer queueContainer(MQQueueConnectionFactory mqQueueConnectionFactory) {

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(mqQueueConnectionFactory);
    container.setDestinationName(queue);
    container.setMessageListener(getListenerWrapper());
    container.start();
    return container;
  }

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

  @Bean
  public JmsTemplate getJmsTemplate() {
    try {
      return new JmsTemplate(mqQueueConnectionFactory());
    } catch (Exception exp) {
      logger.error(exp.getMessage(), exp);
    }
    return null;
  }

}
Rhynchocephalian answered 19/4, 2018 at 0:40 Comment(1)
This is a nice example how to accomplish that: github.com/Aracki/IBM_MQ_Spring_Boot_JMSIronsides
C
3

Just add a listener container bean for each queue.

To change the queue, call stop(), then shutdown(), change the destination, then initialize(), then start().

Corporate answered 19/4, 2018 at 16:32 Comment(1)
Thanks @Gary for this suggestion, I manipulated queue Container to not to listen to any specific queue.. Created a component and a method annotated with JmsListener. Also I used stop(), shutdown(), initialize() and start() method to dynamically switch queuesRhynchocephalian

© 2022 - 2024 — McMap. All rights reserved.