package cn.edu.itec.mqclient;
?
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
?
import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueueManager;
?
/**
?* @author Administrator
?*
?* TODO To change the template for this generated type comment go to Window -
?* Preferences - Java - Code Style - Code Templates
?*/
public class MQGet {
?????? private static String HOST_URL = "192.168.1.116";
?
?????? private static String MQ_CHANNEL = "EXAMPLE.CHANNEL";
?
?????? private static String MQ_MANAGER = "QMGR";
?
?????? private static String MQ_QUEUE = "EXAMPLE.SENDQUEUE";
?
?????? private static int MQ_PORT = 4001;
?
?????? public static void main(String[] args) {
????????????? MQGet.getMessage();
?????? }
?
?????? public static void getMessage() {
????????????? try {
???????????????????? /*
設(shè)置
MQEnvironment
屬性以便客戶機(jī)連接
*/
???????????????????? MQEnvironment.hostname = HOST_URL;
???????????????????? MQEnvironment.channel = MQ_CHANNEL;
???????????????????? MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
?????????????????????????????????? MQC.TRANSPORT_MQSERIES);
???????????????????? MQEnvironment.CCSID = 1381;
???????????????????? MQEnvironment.port = MQ_PORT;
?
???????????????????? /*
連接到隊(duì)列管理器
*/
???????????????????? MQQueueManager qMgr = new MQQueueManager(MQ_MANAGER);
???????????????????? System.out.println("queue manager is connected!");
?
???????????????????? /*
????????????????????
?*
設(shè)置打開選項(xiàng)以便打開用于輸出的隊(duì)列,如果隊(duì)列管理器停止,我們也
已設(shè)置了選項(xiàng)去應(yīng)對不成功情況
????????????????????
?*/
???????????????????? int openOptions = MQC.MQOO_INPUT_SHARED
?????????????????????????????????? | MQC.MQOO_FAIL_IF_QUIESCING;
?
???????????????????? /*
打開隊(duì)列
*/
???????????????????? com.ibm.mq.MQQueue queue = qMgr.accessQueue(MQ_QUEUE, openOptions);
???????????????????? System.out.println("
隊(duì)列連接成功
");
???????????????????? /*
設(shè)置放置消息選項(xiàng)
*/
???????????????????? MQGetMessageOptions gmo = new MQGetMessageOptions();
?
???????????????????? /*
在同步點(diǎn)控制下獲取消息
*/
???????????????????? gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;
?
???????????????????? /*
如果在隊(duì)列上沒有消息則等待
*/
???????????????????? gmo.options = gmo.options + MQC.MQGMO_WAIT;
?
???????????????????? /*
如果隊(duì)列管理器停頓則失敗
*/
???????????????????? gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;
?
???????????????????? /*
設(shè)置等待的時間限制
*/
???????????????????? gmo.waitInterval = MQC.MQWI_UNLIMITED;
???????????????????? /* create the message buffer store */
???????????????????? MQMessage inMessage = new MQMessage();
???????????????????? /* set the selector */
???????????????????? inMessage.correlationId = "clinet_B_receive".getBytes();
???????????????????? /* get the message */
???????????????????? queue.get(inMessage, gmo);
???????????????????? System.out.println("get message success");
????????????????????
???????????????????? /*
讀出消息對象
*/
???????????????????? Hashtable messageObject = (Hashtable) inMessage.readObject();
???????????????????? System.out.println(messageObject);
???????????????????? /*
讀出消息內(nèi)容
*/
???????????????????? byte[] content = (byte[]) messageObject.get("content");
???????????????????? /* save file */
???????????????????? FileOutputStream output = new FileOutputStream(
?????????????????????????????????? "f:/exampleReceive.jar");
???????????????????? output.write(content);
???????????????????? output.close();
?
???????????????????? System.out.println(messageObject.get("FileName"));
???????????????????? /*
提交事務(wù)
,
相當(dāng)于確認(rèn)消息已經(jīng)接收,服務(wù)器會刪除該消息
*/
???????????????????? qMgr.commit();
?
????????????? } catch (MQException e) {
???????????????????? e.printStackTrace();
????????????? } catch (IOException e) {
???????????????????? // TODO Auto-generated catch block
???????????????????? e.printStackTrace();
????????????? } catch (ClassNotFoundException e) {
???????????????????? // TODO Auto-generated catch block
???????????????????? e.printStackTrace();
????????????? }
?????? }
}
|