Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to multiple IBM MQ using single spring boot application

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;
  }

}
like image 351
Tarun Shedhani Avatar asked Apr 19 '18 00:04

Tarun Shedhani


1 Answers

Just add a listener container bean for each queue.

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

like image 189
Gary Russell Avatar answered Nov 11 '22 10:11

Gary Russell