Reusing JMSContext in IBM MQ

859 Views Asked by At

I'm trying to reuse a JMSContext to send multiple messages using the same context as shown in this IBM MQ tutorial.

context = cf.createContext();
destination = context.createQueue(QUEUE_NAME);
producer = context.createProducer();

for (int i = 1; i <= 5000; i++) {
  try {
     TextMessage message = context.createTextMessage("Message " + i + ".\n");
     producer.send(destination, message);
  } catch (Exception ignore) {}
}
context.close();

Say the connection is dropped at some point. Will the context auto recovers or will I need to reconstruct the context again?


UPDATE --

This is how the current connection factory is being constructed:

JmsFactoryFactory ff = JmsFactoryFactory.getInstance(JmsConstants.WMQ_PROVIDER);
JmsConnectionFactory cf = ff.createConnectionFactory();

cf.setStringProperty    (CommonConstants.WMQ_HOST_NAME, config.getHost());
cf.setIntProperty       (CommonConstants.WMQ_PORT, config.getPort());
cf.setStringProperty    (CommonConstants.WMQ_CHANNEL, config.getChannel());
cf.setIntProperty       (CommonConstants.WMQ_CONNECTION_MODE, CommonConstants.WMQ_CM_CLIENT);
cf.setStringProperty    (CommonConstants.WMQ_QUEUE_MANAGER, config.getQueueManager());
cf.setBooleanProperty   (JmsConstants.USER_AUTHENTICATION_MQCSP, false);
cf.setIntProperty       (JmsConstants.PRIORITY, 0);

return cf.createContext();

2

There are 2 best solutions below

0
On

Have u tried with creating JMSContext from existing one?

JMSContext#createContext(int sessionMode)

It will create new JMSContext but reuse the same connection.

Reference:

https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_9.1.0/com.ibm.mq.pro.doc/intro_jms_model.htm https://docs.oracle.com/javaee/7/api/javax/jms/JMSContext.html

4
On

Reconnect works like this (see also comment of @JoshMc):

  1. On the client, set the reconnect option like this:

    cf.setIntProperty(CommonConstants.WMQ_CLIENT_RECONNECT_OPTIONS, CommonConstants.WMQConstants.WMQ_CLIENT_RECONNECT);
    
  2. On the server, stop the queue manager like this:

    endmqm -r