package com.hometest.IBMMqTest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.MQConstants;
public class TestMQ {
private MQQueueManager qmanager = null;
private MQQueue queue = null;
private static final String MQ_HOST ="";
private static final Integer MQ_PORT =1414;
private static final String MQ_CHANNEL ="";
private static final String MQ_QUEUEMANAGER_NAME = "MQ_APP";
private static final String MQ_QUEUE_NAME = "Queue1";
public static void main(String[] args) {
// TODO Auto-generated method stub
TestMQ mq = new TestMQ();
try {
mq.send("hello everyone");
// mq.send("WHAT");
System.out.println(mq.receive());
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// mq.receiveAll();
// Integer depth = mq.getQueueDepth();
// System.out.println("The current depth of queue is " + depth);
mq.close();
}
public TestMQ() {
try {
MQEnvironment.hostname = MQ_HOST;
MQEnvironment.port = MQ_PORT;
MQEnvironment.channel = MQ_CHANNEL;
qmanager = new MQQueueManager(MQ_QUEUEMANAGER_NAME);
int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_INQUIRE | MQConstants.MQOO_OUTPUT;
queue = qmanager.accessQueue(MQ_QUEUE_NAME, openOptions);
} catch (MQException e) {
e.printStackTrace();
}
}
public Integer getQueueDepth() {
Integer depth = 0;
try {
if (queue != null && queue.isOpen()) {
depth = queue.getCurrentDepth();
}
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return depth;
}
public void close() {
if (queue != null && queue.isOpen()) {
try {
queue.close();
} catch (MQException e) {
e.printStackTrace();
}
}
if (qmanager != null && qmanager.isConnected()) {
try {
qmanager.disconnect();
} catch (MQException e) {
e.printStackTrace();
}
}
}
public void send(String msg) throws MQException {
try {
MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();
MQMessage mqMessage = new MQMessage();
mqMessage.write(msg.getBytes("utf-8"));
queue.put(mqMessage, mqPutMessageOptions);
System.out.println("Sent a message:" + msg);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String receive() throws MQException{
MQMessage mqMsg = new MQMessage();
MQGetMessageOptions mqGetMessageOptions = new MQGetMessageOptions();
try {
queue.get(mqMsg, mqGetMessageOptions);
int len = mqMsg.getDataLength();
byte[] message = new byte[len];
mqMsg.readFully(message, 0, len);
String str = new String(message);
System.out.println("Received a message:" + str);
return str;
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public void receiveAll() {
try {
int depth = queue.getCurrentDepth();
System.out.println("The current depth of queue is " + depth);
System.out.println("Now receive all messages....");
while(depth-- > 0){
MQMessage mqMsg = new MQMessage();
MQGetMessageOptions mqGetMessageOptions = new MQGetMessageOptions();
queue.get(mqMsg, mqGetMessageOptions);
int len = mqMsg.getDataLength();
byte[] message = new byte[len];
mqMsg.readFully(message, 0, len);
System.out.println(new String(message));
}
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
来源:https://www.cnblogs.com/wendedi/p/5453209.html