一般而言,MQI 程序采用第31 页图3-1 所显示的简单的操作流程。
1. 程序必须首先调用MQCONN 连接到队列管理器。
2. 一旦连接成功建立,就可以调用MQOPEN 打开一个或多个对象。
3. 可对每个对象进行任何次数的操作(如获取或放置操作),直到不需要该对象为止。
4. 然后调用MQCLOSE 关闭该对象。
我们完成上述工作之后,还将讨论一下MQI 所能完成的四项基本操作:
将消息放入队列中
从队列中获取消息
在队列上浏览消息
查询和设定对象属性
打开/关闭调用中可以进行任何次数的基本操作。
简单的发送消息程序
import com.ibm.mq.*;
public class Typesetter {
public static void main(String args[]) {
try
{
String hostName = "ITSOG" ;
String channel = "JAVA.CLIENT.CHNL" ;
String qManager = "ITSOG.QMGR1" ;
String qName = "SAMPLE.QUEUE" ;
//Set up the MQEnvironment properties for Client Connections(建立MQEnvironment 属性以便客户机连接)
MQEnvironment.hostname = hostName ;
MQEnvironment.channel = channel ;
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
MQC.TRANSPORT_MQSERIES);
//Connection To the Queue Manager(连接到队列管理器)
MQQueueManager qMgr = new MQQueueManager(qManager) ;
/* Set up the open options to open the queue for out put and(建立打开选项以便打开用于输出的队列,)
additionally we have set the option to fail if the queue manager is quiescing.(进一步而言,如果队列管理器是
停顿的话,我们也已设置了选项去应对不成功情况。)
*/
int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING ;
//Open the queue(打开队列)
MQQueue queue = qMgr.accessQueue(qName,
openOptions,
null,
null,
null);
// Set the put message options , we will use the default setting.(设置放置消息选项我们将使用默认设置。)
MQPutMessageOptions pmo = new MQPutMessageOptions();
/* Next we Build a message The MQMessage class encapsulates the data buffer
that contains the actual message data, together with all the MQMD
parameters
that describe the message. (下一步我们建立消息,MQMessage 类压缩了包含实际消息数据的数据缓冲区,
和描述消息的所有MQMD 参数)
To Build a new message, create a new instance of MQMessage class and use
writxxx (we will be using writeString method). The put() method of
MQQueue also
takes an instance of the MQPutMessageOptions class as a parameter.(欲建立新消息,创建MQMessage 类新
实例以及使用writxxx<我们将使用writeString 方法。 > 。MQQueue 的put()方法也可作为参数
MQPutMessageOptions 类的实例。)
*/
MQMessage outMsg = new MQMessage(); //Create The message buffer(创建消息缓冲区)
outMsg.format = MQC.MQFMT_STRING ; // Set the MQMD format field.(设置MQMD 格式字段)
//Prepare message with user data(准备用户数据消息)
String msgString = "Test Message from PtpSender program ";
outMsg.writeString(msgString);
// Now we put The message on the Queue(现在我们在队列上放置消息)
queue.put(outMsg, pmo);
//Commit the transaction.(提交事务处理)
qMgr.commit();
System.out.println(" The message has been Sussesfully put\n\n#########");
// Close the the Queue and Queue manager objects.(关闭队列和队列管理器对象)
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();
}
}
}
简单的消息接收器应用程序
我们的下一个点到点客户机程序是消息接收器应用程序,它获取PtpSender 应用程序所
发送的消息并在控制台上将消息打印出来。
有关步骤如下:
调入MQSeries Java API package;
为客户机连接设置环境属性;
连接到队列管理器;
为打开MQSeries 队列设置选项;
为获取消息打开应用程序;
设置选项,从应用程序队列获取消息;
创建消息缓冲区;
从队列获取消息到消息缓冲区;
从消息缓冲区读取用户数据并在控制台上显示。
import com.ibm.mq.* ;
public class PtpReceiver {
public static void main(String args[]) {
try
{
String hostName = "ITSOG" ;
String channel = "JAVA.CLIENT.CHNL" ;
String qManager = "ITSOG.QMGR1" ;
String qName = "SAMPLE.QUEUE" ;
//建立MQEnvironment 属性以便客户机连接
MQEnvironment.hostname = hostName ;
MQEnvironment.channel = channel ;
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
MQC.TRANSPORT_MQSERIES);
//Connection To the Queue Manager(连接到队列管理器)
MQQueueManager qMgr = new MQQueueManager(qManager) ;
/* Set up the open options to open the queue for out put and
additionally we have set the option to fail if the queue manager is
quiescing.(建立打开选项以便打开用于输出的队列,进一步而言,如果队列管理器是停顿的话,我们也
已设置了选项去应对不成功情况。)
*/
int openOptions = MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING ;
//Open the queue(打开队列)
MQQueue queue = qMgr.accessQueue(qName,
openOptions,
null,
null,
null);
// 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 = 3000 ; // Sets the time limit for the wait.(设置等待的时间限制)
/* Next we Build a message The MQMessage class encapsulates the data buffer
that contains the actual message data, together with all the MQMD parameters
that describe the message.(下一步我们建立消息,MQMessage 类压缩了包含实际消息数据的数据缓冲区,
和描述消息的所有MQMD 参数)
*/
MQMessage inMsg = new MQMessage(); //Create the message buffer(创建消息缓冲区)
// Get the message from the queue on to the message buffer.(从队列到消息缓冲区获取消息)
queue.get(inMsg, gmo) ;
// Read the User data from the message.(从消息读取用户数据)
String msgString = inMsg.readString(inMsg.getMessageLength());
System.out.println(" The Message from the Queue is : " + msgString);
//Commit the transaction.(提交事务处理)
qMgr.commit();
// Close the the Queue and Queue manager objects.(关闭队列和队列管理器对象)
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();
}
}
}
