package com.watson.mq;
import java.io.IOException;
import java.util.Hashtable;
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 MQSample{
//定义队列管理器和远程队列的名称
private static String qmName = "IB9QMGR";
private static String qName = "RQ_0000";
private static MQQueueManager qMgr;
private static Hashtable properties = new Hashtable();
public static void main(String args[]) {
try {
properties.put("hostname", "192.168.2.49");
properties.put("port", new Integer(2414));
properties.put("channel", "JMS_RECEIVE");
// Create a connection to the queue manager
qMgr = new MQQueueManager(qmName,properties);
// Set up the options on the queue we wish to open...
int openOptions = 16;
// Now specify the queue that we wish to open,
// and the open options...
MQQueue remoteQ = qMgr.accessQueue(qName, openOptions);
// Define a simple WebSphere MQ message, and write some text in UTF format..
MQMessage putMessage = new MQMessage();
putMessage.writeUTF("Test");
// specify the message options...
MQPutMessageOptions pmo = new MQPutMessageOptions();
// accept the defaults, same as MQPMO_DEFAULT
// put the message on the queue
remoteQ.put(putMessage,pmo);
System.out.println("Message has been input into the Remote Queue");
// Close the queue...
remoteQ.close();
// Disconnect from the queue manager
qMgr.disconnect();
}catch (MQException ex) {
// If an error has occurred in the above, try to identify what went wrong
// Was it a WebSphere MQ error?
System.out.println("A WebSphere MQ error occurred : Completion code " + ex.completionCode +
" Reason code " + ex.reasonCode);
}catch (IOException ex) {
// Was it a Java buffer space error?
System.out.println("An error occurred whilst writing to the message buffer: " + ex);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
运行以上程序需导入ibm mq的jar包.