import com.ibm.mq.MQGetMessageOptions; //導入依賴的package包/類
/**
* 處理來自請求隊列的請求消息並發送回複到請求應用程序指定的請求隊列上。
*/
public static void responder() {
try {
/* 設置MQEnvironment 屬性以便客戶機連接 */
MQEnvironment.hostname = hostName;
MQEnvironment.channel = channel;
MQEnvironment.port = port;
MQEnvironment.CCSID = CCSID;
// MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,MQC.TRANSPORT_WEBSPHERE
// MQ);
/* 連接到隊列管理器 */
MQQueueManager qMgr = new MQQueueManager(qManager);
/*
* 設置打開選項以便打開用於輸出的隊列,如果隊列管理器停止,我們也 已設置了選項去應對不成功情況
*/
int openOptions = MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING;
/* 打開隊列 */
MQQueue queue = qMgr.accessQueue(qName, openOptions, null, null, null);
/* 設置放置消息選項 */
MQGetMessageOptions gmo = new MQGetMessageOptions();
/* 在同步點控製下取消息 */
gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;
/* 如果隊列上沒有消息則等待 */
gmo.options = gmo.options + MQC.MQGMO_WAIT;
/* 如果隊列管理器停止則失敗 */
gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;
/* 設置等待的時間限製 */
gmo.waitInterval = 3000000;
/* 創建MQMessage 類 */
MQMessage inMsg = new MQMessage();
/* 從隊列到隊列緩衝區獲取消息 */
queue.get(inMsg, gmo);
/* 從消息讀用戶數據 */
String msgString = inMsg.readString(inMsg.getMessageLength());
System.out.println(" The Message from the Queue is : " + msgString);
/* 檢查看消息是否屬於類型請求消息並對該請求回複 */
if (inMsg.messageFlags == MQC.MQMT_REQUEST) {
System.out.println("Preparing To Reply To the Request ");
String replyQueueName = inMsg.replyToQueueName;
openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
MQQueue respQueue = qMgr.accessQueue(replyQueueName, openOptions, inMsg.replyToQueueManagerName, null, null);
MQMessage respMessage = new MQMessage();
MQPutMessageOptions pmo = new MQPutMessageOptions();
respMessage.format = MQC.MQFMT_STRING;
respMessage.messageFlags = MQC.MQMT_REPLY;
//respMessage.correlationId = "123456789".getBytes();
//String response = "Reply from the Responder Program ";
//respMessage.writeString(response);
//respQueue.put(respMessage, pmo);
inMsg.messageId = "12345".getBytes();
respMessage.correlationId = inMsg.messageId;
String response = "Reply from the Responder Program ";
respMessage.writeString(response);
respQueue.put(respMessage, pmo);
System.out.println("The response Successfully send ");
qMgr.commit();
respQueue.close();
}
queue.close();
qMgr.disconnect();
} catch (MQException ex) {
System.out.println("An MQ Error Occurred: Completion Code is :\t" + ex.completionCode + "\n\n The Reason Code is :\t" + ex.reasonCode);
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}