| « | 五月 2007 | » | ||||
|---|---|---|---|---|---|---|
| 一 | 二 | 三 | 四 | 五 | 六 | 日 |
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 | |||
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();
}
}
}
MQTrigger.java源码如下:
import java.io.*;
import java.lang.*;
import com.ibm.mq.*;
class MQTrigger
{
private String structId;
private String version;
private String qName;
private String processName;
private String triggerData;
private String applType;
private String applId;
private String envData;
private String userData;
private String qMgrName;
/******************************************************/
/* Constructor to parse the MQTMC2 stucture and set */
/* the class attributes. */
/* Values derived from field definitions given for */
/* MQTMC2 in the WebSphere Application Programming */
/* Reference. */
/******************************************************/
public MQTrigger(String tmcStruct) throws StringIndexOutOfBoundsException
{
structId = tmcStruct.substring(0,3).trim();
version = tmcStruct.substring(4,8).trim();
qName = tmcStruct.substring(8,55).trim();
processName = tmcStruct.substring(56,103).trim();
triggerData = tmcStruct.substring(104,167).trim();
applType = tmcStruct.substring(168,171).trim();
applId = tmcStruct.substring(172,427).trim();
envData = tmcStruct.substring(428,555).trim();
userData = tmcStruct.substring(556,683).trim();
qMgrName = tmcStruct.substring(684,730).trim();
}
public String getStructId()
{
return(structId);
}
public String getVersion()
{
return(version);
}
public String getQueueName()
{
return(qName);
}
public String getProcessName()
{
return(processName);
}
public String getTriggerData()
{
return(triggerData);
}
public String getApplicationType()
{
return(applType);
}
public String getApplicationId()
{
return(applId);
}
public String getEnvironmentData()
{
return(envData);
}
public String getUserData()
{
return(userData);
}
public String getQueueManagerName()
{
return(qMgrName);
}
}
JavaTrigger.java源码如下:
import java.io.IOException;
import com.ibm.mq.MQC;
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 JavaTrigger
{
private MQQueueManager qMgr;
public static void main (String args[]) throws IOException
{
if (args.length < 1)
{
System.out.println("This must be a triggered application");
}
else
{
JavaTrigger jt = new JavaTrigger();
jt.start(args);
}
System.exit(0);
}
public void start(String args[])
{
try
{
MQException.log = null;
/******************************************************/
/* Create a MQTrigger class object to read the MQTMC2 */
/* structure into the correct attribute. */
/******************************************************/
MQTrigger tmc = new MQTrigger(args[0]);
/******************************************************/
/* Connect to the queue manager identified by the */
/* trigger. */
/******************************************************/
qMgr = new MQQueueManager(tmc.getQueueManagerName());
/******************************************************/
/* Open the queue identified by the trigger. */
/******************************************************/
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF
| MQC.MQOO_FAIL_IF_QUIESCING;
MQQueue triggerQueue = qMgr.accessQueue(tmc.getQueueName(),
openOptions,
null, null, null);
/******************************************************/
/* Set up our options to get the first message */
/* Wait 5 seconds to be cetain all messages are */
/* processed. */
/******************************************************/
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = MQC.MQGMO_WAIT | MQC.MQGMO_CONVERT;
gmo.waitInterval = 5000;
MQMessage triggerMessage = new MQMessage();
/*****************************************************/
/* Read each message from the queue until there are */
/* no more messages to get. */
/*****************************************************/
long rc = 0;
do
{
rc = 0;
try
{
/***********************************************/
/* Set the messageId and correlationId to none */
/* to get all messages with no message */
/* selection. */
/***********************************************/
triggerMessage.clearMessage();
triggerMessage.correlationId = MQC.MQCI_NONE;
triggerMessage.messageId = MQC.MQMI_NONE;
triggerQueue.get(triggerMessage, gmo);
String msg = triggerMessage.readString(triggerMessage.getMessageLength());
/***********************************************/
/* Insert business logic for the message here. */
/* For this sample, echo the first 20 */
/* characters of the message. */
/***********************************************/
if (msg.length() > 20)
{
System.out.println("Message: " + msg.substring(0,20));
}
else
{
System.out.println("Message: " + msg);
}
}
catch (MQException mqEx)
{
rc = mqEx.reasonCode;
if (rc != MQException.MQRC_NO_MSG_AVAILABLE)
{
System.out.println(" PUT Message failed with rc = "
+ rc);
}
}
catch (Exception ex)
{
System.out.println("Generic exception: " + ex);
rc = 1;
}
} while (rc == 0);
/**********************************************************/
/* Cleanup MQ resources prior to exiting. */
/**********************************************************/
triggerQueue.close();
qMgr.disconnect();
}
catch (MQException mqEx)
{
System.out.println("MQ failed with completion code = "
+ mqEx.completionCode
+ " and reason code = " + mqEx.reasonCode);
}
}
}
首先做如下配置定义:
Use runmqsc to create the following objects:
1. Queue to be triggered.
DEFINE QLOCAL('JAVA.TRIGGER.QUEUE') REPLACE +
DESCR('Application queue to test triggering') +
SHARE +
TRIGGER +
TRIGTYPE (FIRST) +
INITQ('JAVA.INIT.QUEUE') +
PROCESS('JAVA.PROCESS')
2. Initiation queue.
DEFINE QLOCAL('JAVA.INIT.QUEUE') REPLACE +
DESCR('Initiation queue to test triggering')
3. Process. **
DEFINE PROCESS('JAVA.PROCESS') REPLACE +
DESCR('Process to test triggering') +
APPLICID('java -cp .;c:mqmjavalib;c:mqmjavalibcom.ibm.mq.jar;c:mqmjavalibconnector.jar;c:mqmjavalibjta.jar JavaTrigger')
4. Start the trigger monitor with
runmqtrm -m QMGRNAME -q JAVA.INIT.QUEUE
** Note: It must be started in the directory where JavaTrigger.class
resides so that JavaTrigger.class can be loaded from the
current directory and resolved by "." in the classpath.
If your java code is included in a package, then the jar may
be added to the classpath and the location where runmqtrm is
started does not matter.
5. Put a message on JAVA.TRIGGER.QUEUE. The trigger information along
with the first 20 characters of the message is displayed in the
window where runmqtrm is active.
源码:
import java.util.Hashtable;
import javax.jms.*;
import javax.naming.*;
import javax.naming.directory.*;
public class sample {
protected QueueConnectionFactory factory=null;
protected QueueConnection connection;
protected QueueSession queueSession;
protected TextMessage outMessage;
protected QueueSender queueSender;
protected QueueReceiver queueReceiver;
public static final String qcfLookup="QCFC";
public static final String qLookup="Q1";
public static final String icf = "com.sun.jndi.fscontext.RefFSContextFactory";
public String url ="file:/d:/testmq/ctx/";
public void sampleInit() throws Exception {
Hashtable environment = new Hashtable();
environment.put(Context.INITIAL_CONTEXT_FACTORY, icf);
environment.put(Context.PROVIDER_URL, url);
environment.put(Context.REFERRAL, "throw");
Context ctx=new InitialDirContext(environment);
factory = (QueueConnectionFactory)ctx.lookup(qcfLookup);
Queue q1=null;
q1=(Queue)ctx.lookup(qLookup);
connection = factory.createQueueConnection();
queueSession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queueSender = queueSession.createSender(q1);
queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
outMessage = queueSession.createTextMessage();
queueReceiver = queueSession.createReceiver(q1);
connection.start();
}
public void sendMessageOut(String message) throws JMSException {
outMessage.setText(message);
queueSender.send(outMessage);
}
public String receiveMessage() throws Exception{
return ((TextMessage)queueReceiver.receive()).getText();
}
public void sampleClose() throws JMSException {
queueSession.close();
connection.close();
}
public static void main(String[] args){
String rec;
sample sp = new sample();
try {
sp.sampleInit();
sp.sendMessageOut("This is a test!");
java.lang.Thread.sleep(4000);
rec=sp.receiveMessage();
System.out.println("Receive text is : "+rec);
sp.sampleClose();
}catch(Exception e) {
e.printStackTrace();
}
}
}








