I get an Exception while connecting to MQQueueManager. The Exception reads - MQException: MQJE001: An MQException occurred: Completion Code 2, Reason 2009 MQJE016: MQ queue manager closed channel immediately during connect Closure reason = 2009. Below is my code to push a message to queue.
@SuppressWarnings("unchecked")
public boolean postMessage(String mqMessage, String correlationId) throws TradeException, MQException {
String logStr = TradeUtil.getLoggerPrefix(this.getClass().getSimpleName(),
Thread.currentThread().getStackTrace()[1].getMethodName(), "");
boolean result = false;
MQQueueManager queueManager = null;
try {
MQEnvironment.hostname = ConfigFileLoader.getProperty("MQ_HOSTNAME");
MQEnvironment.channel = ConfigFileLoader.getProperty("MQ_CHANNEL");
MQEnvironment.port = Integer.parseInt(ConfigFileLoader.getProperty("MQ_PORT"));
tradeLogger.info(logStr + "MQ HOST NAME : " + MQEnvironment.hostname + " MQ CHANNEL : "
+ MQEnvironment.channel + " MQ PORT : " + MQEnvironment.port);
try {
queueManager = new MQQueueManager(ConfigFileLoader.getProperty("MQ_QUEUE_MANAGER"));
} catch (MQException e) {
tradeLogger.info("queueManager is Busy, try after some time");
Thread.sleep(1000 * 60);
if(maxCount < 3) {
maxCount += 1;
postMessage(mqMessage, correlationId);
}
}
tradeLogger.info(logStr + "MQhostname : " + queueManager + ConfigFileLoader.getProperty("MQ_QUEUE_MANAGER"));
int openOptions = MQC.MQOO_OUTPUT + MQC.MQOO_INPUT_AS_Q_DEF;
MQQueue queue = queueManager.accessQueue(ConfigFileLoader.getProperty("MQ_INPUT_QUEUE_NAME"), openOptions);
tradeLogger.info("INPUT QUEUE NAME : " + queue + ConfigFileLoader.getProperty("MQ_INPUT_QUEUE_NAME"));
if (queueManager.isConnected()) {
tradeLogger.info("queue manager is connected!");
MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();
MQMessage message = new MQMessage();
message.format = MQC.MQFMT_STRING;
message.correlationId = correlationId.getBytes();
message.writeString(mqMessage);
queue.put(message, mqPutMessageOptions);
tradeLogger.info(logStr + "POSTED MESSAGE : " + mqMessage);
result = true;
queue.close();
tradeLogger.info("queue is closed.");
} else {
tradeLogger.error("unable to connect to Queue");
throw new TradeException("Unable to connect to Queue");
}
} catch (MQException e) {
tradeLogger.error(logStr + " MQException : " + e.getMessage());
JSONObject errorJSON = new JSONObject();
errorJSON.put("errorDesc", e);
throw new TradeException(ResponseStatus.failure, 404,
"Unable to post message in MQ. Please try after sometime.", errorJSON);
} catch (Exception e) {
tradeLogger.error(logStr + " Exception : " + e.getMessage());
JSONObject errorJSON = new JSONObject();
errorJSON.put("errorDesc", e);
throw new TradeException(ResponseStatus.failure, 404,
"Unable to post message in MQ due to some techical error. Please contact administrator.",
errorJSON);
} finally {
if (null != queueManager) {
queueManager.disconnect();
tradeLogger.info("queueManager is disconnected!");
}
}
return result;
}
This issue gets resolved after manually restarting the Queue Manager. Also, I read somewhere that MQQueueManager() is synchronous. Is it possible that other Threads might be accessing this queue manager to establish connection which results in this exception?