0

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();

Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
  • Depends on if you enable reconnect on the cf or not. – JoshMc Aug 29 '19 at 12:46
  • Check the bottom of my answer https://stackoverflow.com/questions/56370312/ibm-mq-how-to-connect-to-queue-manager-with-multiple-connections-names-one-i/56370695#56370695 – JoshMc Aug 29 '19 at 13:25
  • @JoshMc It seems to only work for multi host setup. I've a context made with single host. Can't seem to find a way to recover connection without reconstructing context? – Gayan Weerakutti Aug 30 '19 at 06:25
  • Does "disconnected my connection" mean you pull the network connection? For how long? – JoshMc Aug 31 '19 at 17:52
  • What exactly does "keep failing" mean. What error and stack trace do you get? – JoshMc Aug 31 '19 at 17:57

2 Answers2

0

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 
    
Daniel Steinmann
  • 2,119
  • 2
  • 15
  • 25
0

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

Awan Biru
  • 373
  • 2
  • 10