我在连接到MQQueueManager时得到一个异常。异常如下:MQException: MQJE001:一个MQException发生:完成代码2,原因2009MQJE016: MQ队列管理器在连接期间立即关闭通道关闭原因= 2009年。下面是我将消息推送到队列的代码。
@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;
}
此问题在手动重新启动队列管理器后得到解决。另外,我在某处读到MQQueueManager()是同步的。是否有可能其他线程正在访问此队列管理器以建立导致此异常的连接?
这不是2009年的答案。我已经简化了你的代码版本
@SuppressWarnings("unchecked")
public boolean postMessage(String mqMessage, String correlationId) throws MQException {
boolean result = false;
MQQueueManager queueManager = null;
MQQueue queue = null;
MQEnvironment.hostname = "localhost";
MQEnvironment.channel = "SVRCONN_CHN";
MQEnvironment.port = 1414;
try {
queueManager = new MQQueueManager("QM1");
int openOptions = MQC.MQOO_OUTPUT; // Just open for putting message
queue = queueManager.accessQueue("DESTQ", openOptions);
MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();
MQMessage message = new MQMessage();
message.format = MQC.MQFMT_STRING;
message.correlationId = correlationId.getBytes();
message.writeString(mqMessage);
queue.put(message, mqPutMessageOptions);
result = true;
} catch (MQException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (queue != null)
queue.close();
if (queueManager != null)
queueManager.disconnect(); // Call disconnect
}
return result;
}
首先,不要使用MQEnvironment类,因为它不是线程安全的。我希望IBM摒弃这个类。您应该将您的连接信息放在一个Hashtable中,并将该Hashtable传递给MQQueueManager对象。
Java示例:
Hashtable<String, Object> mqht = new Hashtable<String, Object>();
mqht.put(CMQC.CHANNEL_PROPERTY, ConfigFileLoader.getProperty("MQ_CHANNEL"));
mqht.put(CMQC.HOST_NAME_PROPERTY, ConfigFileLoader.getProperty("MQ_HOSTNAME"));
mqht.put(CMQC.PORT_PROPERTY, Integer.parseInt(ConfigFileLoader.getProperty("MQ_PORT"));
mqht.put(CMQC.USER_ID_PROPERTY, ConfigFileLoader.getProperty("MQ_USERID")));
mqht.put(CMQC.PASSWORD_PROPERTY, ConfigFileLoader.getProperty("MQ_PASSWORD")));
queueManager = new MQQueueManager(ConfigFileLoader.getProperty("MQ_QUEUE_MANAGER"), mqht);
c# .NET示例:
Hashtable mqht = new Hashtable();
mqht.Add(MQC.CHANNEL_PROPERTY, ConfigFileLoader.getProperty("MQ_CHANNEL"));
mqht.Add(MQC.HOST_NAME_PROPERTY, ConfigFileLoader.getProperty("MQ_HOSTNAME"));
mqht.Add(MQC.PORT_PROPERTY, System.Int32.Parse(ConfigFileLoader.getProperty("MQ_PORT"));
mqht.Add(MQC.USER_ID_PROPERTY, ConfigFileLoader.getProperty("MQ_USERID")));
mqht.Add(MQC.PASSWORD_PROPERTY, ConfigFileLoader.getProperty("MQ_PASSWORD")));
mqht.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
queueManager = new MQQueueManager(ConfigFileLoader.getProperty("MQ_QUEUE_MANAGER"), mqht);
另外,为了适当的安全性,您应该为到队列管理器的连接设置UserId和Password。
最后,确保关闭所有打开的队列,正如Shashi在他的代码示例中所指出的。