In recent times, we faced a lingering issue in production while posting messages to a remote queue (MQV5.3) using MQ import binding (connections management via server settings) where the connections from client are remained alive/active despite connections are broken from remote side. We applied several settings suggested in Technote but it didn't help. We also checked firewall and TCP settings but no luck.
PMR suggested that its due to older version of MQ which is no longer supported by IBM and we were advised to upgrade. Since MQ is remote and we have no control on it we end up making/managing connections using Java code, which is performing really well as of this writing. Here is the code snippet:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Date;
import java.util.Properties;
import javax.jms.JMSException;
import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;
public static void postMessage(String msg) {
Properties p = new Properties(System.getProperties());
//Setting below properties based on Link1 and Link2
p.put("com.ibm.mq.cfg.TCP.Connect_Timeout", "30");
p.put("com.ibm.mq.cfg.MQRCVBLKTO", "30");
System.setProperties(p);
MQQueueSession session = null;
MQQueueSender sender = null;
MQQueueConnection connection = null;
try {
FileReader fr = new FileReader(
"C:\\Harish\\Msg.txt");
BufferedReader br = new BufferedReader(fr);
String s;
StringBuffer str = new StringBuffer();
while ((s = br.readLine()) != null)
str.append(s);
fr.close();
msg = str.toString();
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
cf.setHostName(HOST);
cf.setPort(PORT);
cf.setTransportType(1);
cf.setQueueManager(QM);
cf.setChannel(CHANNEL);
connection = (MQQueueConnection) cf.createQueueConnection();
session = (MQQueueSession) connection.createQueueSession(false, 1);
MQQueue queue = (MQQueue) session
.createQueue("queue:///QUEUE");
sender = (MQQueueSender) session.createSender(queue);
JMSTextMessage message = (JMSTextMessage) session
.createTextMessage(msg);
connection.start();
sender.send(message);
System.out.println("Sent message");
} catch (JMSException jme) {
System.out.println("JMSException occurred");
jme.printStackTrace();
} catch (Exception e) {
System.out.println("Exception occurred");
e.printStackTrace();
}
finally {
try {
sender.close();
System.out.println("sender closed");
} catch (Exception ex) {
System.err.println("sender() : " + ex.getLocalizedMessage());
}
try {
session.close();
System.out.println("session closed");
} catch (Exception ex) {
System.err.println("session.close() : "
+ ex.getLocalizedMessage());
}
try {
connection.close();
System.out.println("connection closed");
} catch (Exception ex) {
System.err.println("connection.close() : "
+ ex.getLocalizedMessage());
}
}
}
PMR suggested that its due to older version of MQ which is no longer supported by IBM and we were advised to upgrade. Since MQ is remote and we have no control on it we end up making/managing connections using Java code, which is performing really well as of this writing. Here is the code snippet:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Date;
import java.util.Properties;
import javax.jms.JMSException;
import com.ibm.jms.JMSTextMessage;
import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;
public static void postMessage(String msg) {
Properties p = new Properties(System.getProperties());
//Setting below properties based on Link1 and Link2
p.put("com.ibm.mq.cfg.TCP.Connect_Timeout", "30");
p.put("com.ibm.mq.cfg.MQRCVBLKTO", "30");
System.setProperties(p);
MQQueueSession session = null;
MQQueueSender sender = null;
MQQueueConnection connection = null;
try {
FileReader fr = new FileReader(
"C:\\Harish\\Msg.txt");
BufferedReader br = new BufferedReader(fr);
String s;
StringBuffer str = new StringBuffer();
while ((s = br.readLine()) != null)
str.append(s);
fr.close();
msg = str.toString();
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
cf.setHostName(HOST);
cf.setPort(PORT);
cf.setTransportType(1);
cf.setQueueManager(QM);
cf.setChannel(CHANNEL);
connection = (MQQueueConnection) cf.createQueueConnection();
session = (MQQueueSession) connection.createQueueSession(false, 1);
MQQueue queue = (MQQueue) session
.createQueue("queue:///QUEUE");
sender = (MQQueueSender) session.createSender(queue);
JMSTextMessage message = (JMSTextMessage) session
.createTextMessage(msg);
connection.start();
sender.send(message);
System.out.println("Sent message");
} catch (JMSException jme) {
System.out.println("JMSException occurred");
jme.printStackTrace();
} catch (Exception e) {
System.out.println("Exception occurred");
e.printStackTrace();
}
finally {
try {
sender.close();
System.out.println("sender closed");
} catch (Exception ex) {
System.err.println("sender() : " + ex.getLocalizedMessage());
}
try {
session.close();
System.out.println("session closed");
} catch (Exception ex) {
System.err.println("session.close() : "
+ ex.getLocalizedMessage());
}
try {
connection.close();
System.out.println("connection closed");
} catch (Exception ex) {
System.err.println("connection.close() : "
+ ex.getLocalizedMessage());
}
}
}