jmq java_Java MQGetMessageOptions類代碼示例

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值