本文執行個體是基于 WebSphere MQ中将消息發送至遠端隊列的配置的基礎上的,且如果要能正常運作并發送、接收消息,還需要在兩個隊列管理器(QM_ORANGE和QM_APPLE)上做如下配置或修改。
1.建立名稱為DC.SVRCONN的伺服器連接配接通道

2.将隊列管理器的通道認證記錄設定為“已禁用”
該程式實作了發送消息與讀取消息的功能,見其中的send***與get***方法。這隻适合于測試,因為環境中的程式還需要對此有稍微的更改,在真實的環境中肯定是在while(true){...}的無限循環中去調用其中的get方法,如果有值,那就執行對消息的處理操作,如果沒有值就繼續循環,在get方法中有等待的時間。
這個程式就其本身來說還是比較了解的:
1.首先設定一些相關的環境變量
2.再連接配接隊列管理器
3.再次操作隊列管理器中的指定隊列
4.往指定隊列中發消息或者是從指定對列中取消息
5.關閉隊列
真實環境中的MQ,個人覺得至少都應該有兩個本地隊列加一個遠端隊列,因為消息的互動肯定是互相的,有收消息,肯定也有發消息。一個本地隊列用于接收外部發過來的消息,用法為正常;另一個本地隊例用于傳輸,用于做于遠端隊例的傳輸隊列,将消息發送給遠端主機的本地隊列。要使消息能夠成功的傳送到遠端隊列,還需要配置通道,通道中需要指定遠端通道的IP位址及端口、本地傳輸隊例的名稱、以及本地的通信位址,這樣才能夠往遠端主機發送消息。
執行個體代碼如下:
SendMessage.java
package com.bijian.study;
import java.io.IOException;
import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
public class SendMessage {
//定義隊列管理器和隊列的名稱
private static String qmName;
private static String qName;
private static MQQueueManager qMgr;
static{
//設定環境:
//MQEnvironment中包含控制MQQueueManager對象中的環境的構成的靜态變量,MQEnvironment的值的設定會在MQQueueManager的構造函數加載的時候起作用,
//是以必須在建立MQQueueManager對象之前設定MQEnvironment中的值.
MQEnvironment.hostname="bijian-PC"; //MQ伺服器的IP位址
MQEnvironment.channel="DC.SVRCONN"; //伺服器連接配接的通道
MQEnvironment.CCSID=1381; //伺服器MQ服務使用的編碼1381代表GBK、1208代表UTF(Coded Character Set Identifier:CCSID)
MQEnvironment.port=1415;//MQ端口
qmName = "QM_ORANGE";//MQ的隊列管理器名稱
qName = "Q1";//MQ遠端隊列的名稱
try {
//定義并初始化隊列管理器對象并連接配接
//MQQueueManager可以被多線程共享,但是從MQ擷取資訊的時候是同步的,任何時候隻有一個線程可以和MQ通信。
qMgr = new MQQueueManager(qmName);
} catch (MQException e) {
// TODO Auto-generated catch block
System.out.println("初使化MQ出錯");
e.printStackTrace();
}
}
public static int sendMessage(String message){
int result=0;
try{
//設定将要連接配接的隊列屬性
// Note. The MQC interface defines all the constants used by the WebSphere MQ Java programming interface
//(except for completion code constants and error code constants).
//MQOO_INPUT_AS_Q_DEF:Open the queue to get messages using the queue-defined default.
//MQOO_OUTPUT:Open the queue to put messages.
//int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
//連接配接隊列
//MQQueue provides inquire, set, put and get operations for WebSphere MQ queues.
//The inquire and set capabilities are inherited from MQManagedObject.
if(qMgr==null || !qMgr.isConnected()){
qMgr = new MQQueueManager(qmName);
}
MQQueue queue = qMgr.accessQueue(qName, openOptions);
//定義一個簡單的消息
MQMessage putMessage = new MQMessage();
//将資料放入消息緩沖區
putMessage.writeUTF(message);
//設定寫入消息的屬性(預設屬性)
MQPutMessageOptions pmo = new MQPutMessageOptions();
//将消息寫入隊列
queue.put(putMessage,pmo);
queue.close();
}catch (MQException ex) {
System.out.println("A WebSphere MQ error occurred : Completion code "
+ ex.completionCode + " Reason code " + ex.reasonCode);
ex.printStackTrace();
}catch (IOException ex) {
System.out.println("An error occurred whilst writing to the message buffer: " + ex);
}catch(Exception ex){
ex.printStackTrace();
}finally{
try {
qMgr.disconnect();
} catch (MQException e) {
e.printStackTrace();
}
}
return result;
}
public static void main(String args[]) {
sendMessage("this is a test");
}
}
運作結果,java用戶端将消息發送到QM_ORANGE隊列管理器的遠端Q1隊列,QM_ORANGE隊列管理器又将消息發送到QM_APPLE隊列管理器的本地Q1隊列。
GetMessage.java
package com.bijian.study;
import java.io.IOException;
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.MQQueue;
import com.ibm.mq.MQQueueManager;
public class GetMessage {
//定義隊列管理器和隊列的名稱
private static String qmName;
private static String qName;
private static MQQueueManager qMgr;
static{
//設定環境:
//MQEnvironment中包含控制MQQueueManager對象中的環境的構成的靜态變量,MQEnvironment的值的設定會在MQQueueManager的構造函數加載的時候起作用,
//是以必須在建立MQQueueManager對象之前設定MQEnvironment中的值.
MQEnvironment.hostname="bijian-PC"; //MQ伺服器的IP位址
MQEnvironment.channel="DC.SVRCONN"; //伺服器連接配接的通道
MQEnvironment.CCSID=1381; //伺服器MQ服務使用的編碼1381代表GBK、1208代表UTF(Coded Character Set Identifier:CCSID)
MQEnvironment.port=1416;//MQ端口
qmName = "QM_APPLE";//MQ的隊列管理器名稱
qName = "Q1";//MQ遠端隊列的名稱
try {
//定義并初始化隊列管理器對象并連接配接
//MQQueueManager可以被多線程共享,但是從MQ擷取資訊的時候是同步的,任何時候隻有一個線程可以和MQ通信。
qMgr = new MQQueueManager(qmName);
} catch (MQException e) {
// TODO Auto-generated catch block
System.out.println("初使化MQ出錯");
e.printStackTrace();
}
}
public static String getMessage() {
String message=null;
try{
//設定将要連接配接的隊列屬性
// Note. The MQC interface defines all the constants used by the WebSphere MQ Java programming interface
//(except for completion code constants and error code constants).
//MQOO_INPUT_AS_Q_DEF:Open the queue to get messages using the queue-defined default.
//MQOO_OUTPUT:Open the queue to put messages.
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT;
MQMessage retrieve = new MQMessage();
//設定取出消息的屬性(預設屬性)
//Set the put message options.(設定放置消息選項)
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;//Get messages under sync point control(在同步點控制下擷取消息)
gmo.options = gmo.options + MQC.MQGMO_WAIT; // Wait if no messages on the Queue(如果在隊列上沒有消息則等待)
gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;// Fail if Qeue Manager Quiescing(如果隊列管理器停頓則失敗)
gmo.waitInterval = 1000 ; // Sets the time limit for the wait.(設定等待的毫秒時間限制)
if(qMgr==null || !qMgr.isConnected()){
qMgr = new MQQueueManager(qmName);
}
MQQueue queue = qMgr.accessQueue(qName, openOptions);
// 從隊列中取出消息
queue.get(retrieve, gmo);
message = retrieve.readUTF();
System.out.println("The message is: " + message);
queue.close();
}catch (MQException ex) {
System.out.println("A WebSphere MQ error occurred : Completion code "
+ ex.completionCode + " Reason code " + ex.reasonCode);
}catch (IOException ex) {
System.out.println("An error occurred whilst writing to the message buffer: " + ex);
}catch(Exception ex){
ex.printStackTrace();
}finally{
try {
qMgr.disconnect();
} catch (MQException e) {
e.printStackTrace();
}
}
return message;
}
public static void main(String args[]) {
getMessage();
}
}
運作結果,控制台輸出
The message is: this is a test
此時,QM_APPLE隊列管理器中的Q1隊列中的消息已被取出。