ganquan的博客

欢迎来到ganquan的博客>>   | 首页 资源中心 | WebSphere MQ | Oracle | DB2 | TXSeries/CICS & CTG | 工作经历和心得 | Informix | Sybase | tuxedo | WebSphere Application Server | ITPUB论坛

WebSphere MQ For JAVA编程实例----请求/回复---样例

发表人:ganquan | 发表时间: 2007年五月29日, 16:04

Requester.java源码:

import com.ibm.mq.*;

public class Requester {
public static void main(String args[]) {
try {
String hostName = "127.0.0.1";
String channel = "CHAN1";
String qManager = "QM1";
String requestQueue = "QL1";
String replyToQueue = "REPLYQ";
String replyToQueueManager = "QM1";
// Set up the MQEnvironment properties for Client Connections
MQEnvironment.hostname = hostName;
MQEnvironment.channel = channel;
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
MQC.TRANSPORT_MQSERIES);
MQEnvironment.CCSID = 1381;

// Connection To the Queue Manager
MQQueueManager qMgr = new MQQueueManager(qManager);

int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;

// Open the queue
MQQueue queue = qMgr.accessQueue(requestQueue, openOptions, null,
null, null);
// Set the put message options , we will use the default setting.
MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.options = pmo.options + MQC.MQPMO_NEW_MSG_ID;
pmo.options = pmo.options + MQC.MQPMO_SYNCPOINT;
MQMessage outMsg = new MQMessage();
// Create the message buffer
outMsg.format = MQC.MQFMT_STRING;

// Set the MQMD format field.
outMsg.messageFlags = MQC.MQMT_REQUEST;
outMsg.replyToQueueName = replyToQueue;
outMsg.replyToQueueManagerName = replyToQueueManager;
// Prepare message with user data
String msgString = "Test Request Message from Requester 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 putnn#########");
// Close the the Request Queue

queue.close();
// Set openOption for response queue
openOptions = MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING;
MQQueue respQueue = qMgr.accessQueue(replyToQueue, openOptions,
null, null, null);
MQMessage respMessage = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;
// Get messages under syncpoint control
gmo.options = gmo.options + MQC.MQGMO_WAIT;
// Wait for Response Message
gmo.matchOptions = MQC.MQMO_MATCH_CORREL_ID;
gmo.waitInterval = 10000;
respMessage.correlationId = outMsg.messageId;
System.out.println("The response message correlID : " + respMessage.correlationId);
// Get the response message.
respQueue.get(respMessage, gmo);
String response = respMessage.readString(respMessage
.getMessageLength());
System.out.println("The response message is : " + response);
qMgr.commit();
respQueue.close();
qMgr.disconnect();
} catch (MQException ex) {
System.out.println("An MQ Error Occurred: Completion Code is :t"
+ ex.completionCode + "nn The Reason Code is :t"
+ ex.reasonCode);
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Responder.java源码如下:

import com.ibm.mq.*;

public class Responder {
public static void main(String args[]) {
try {
String hostName = "127.0.0.1";
String channel = "CHAN1";
String qManager = "QM1";
String qName = "QL1";
// Set up the MQEnvironment properties for Client
// Connections
MQEnvironment.hostname = hostName;
MQEnvironment.channel = channel;
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
MQC.TRANSPORT_MQSERIES);
MQEnvironment.CCSID = 1381;
// 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 syncpoint 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 QeueManager 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.
* 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 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);
// Check if message if of type request message and reply to the
// request.
if (inMsg.messageFlags == MQC.MQMT_REQUEST) {
System.out.println("Preparing To Reply To the Request ");
String replyQueueName = inMsg.replyToQueueName;
System.out.println("The reply queue: " + replyQueueName);
openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
MQQueue respQueue = qMgr.accessQueue(replyQueueName,
openOptions, inMsg.replyToQueueManagerName, null, null);
MQMessage respMessage = new MQMessage();
respMessage.correlationId = inMsg.messageId;
System.out.println("The response CorrelID " + respMessage.correlationId);
MQPutMessageOptions pmo = new MQPutMessageOptions();
respMessage.format = MQC.MQFMT_STRING;
respMessage.messageFlags = MQC.MQMT_REPLY;
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 + "nn The Reason Code is :t"
+ ex.reasonCode);
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}


polo t shirts [回复]

polo t shirts Portable DVD players poster printing posters printing power leveling power leveling power leveling power leveling power leveling power leveling

polo t shirts | 04/07/2009, 11:52

您好,求教MQ问题 [回复]

求教 makefire斑竹 IBM MQ的问题
package com;

import com.ibm.mq.*;

/*
* 成功的访问mq 的java 类
*/
public class FirstMqTest {
// public static void main(String[] args[]){
// FirstMqTest first = new FirstMqTest();
// first.test();
// }
public static void main(String args[]){
FirstMqTest first = new FirstMqTest();
first.test();
}
public void test(){
String qManager = "P_TD_QM"; //QueueManager name
String qName = "DOWN_FROM_SA_P";//Queue Name
try {
//configure connection parameters
MQEnvironment.hostname="10.64.8.54";//MQ Server name or IP
MQEnvironment.port=1415;//listenr port
MQEnvironment.channel="CH1";//Server-Connection Channel
MQEnvironment.CCSID =819;
// Create a connection to the QueueManager
System.out.println("Connecting to queue manager: "+qManager);
MQQueueManager qMgr = new MQQueueManager(qManager);
// Set up the options on the queue we wish to open
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF │ MQC.MQOO_OUTPUT;
// Now specify the queue that we wish to open and the open options
System.out.println("Accessing queue: "+qName);
MQQueue queue = qMgr.accessQueue(qName, openOptions);
// Define a simple WebSphere MQ Message ...
MQMessage msg = new MQMessage();
// ... and write some text in UTF8 format
msg.writeUTF("Hello, World!");
// Specify the default put message options
MQPutMessageOptions pmo = new MQPutMessageOptions();
// Put the message to the queue
System.out.println("Sending a message...");
/*
* 在此测试一下 mq 的传输次列
*
*/
for(int j=0;j<5 string str="str+j;" str="msg.writeUTF(str);" now get the message back first define a websphere mq message to receive the data mqmessage rcvmessage="new" rcvmessage="MQMessage();" specify default get message options mqgetmessageoptions gmo="new" gmo="MQGetMessageOptions();" get the message off the and display the message string msgtext="rcvMessage.readUTF();" close the queue disconnect from the queuemanager catch catch connecting to queue accessing a websphere mq error occured completion code reason code>

任鹏 | 22/03/2008, 16:56

发表评论

标题

在此添加评论

称呼

邮箱地址(可选)

个人主页(可选)




Valid XHTML 1.0 Strict and CSS. Powered by pLog
Design by Blog.lvwo.com