package mq;
import java.io.IOException;
import com.ibm.mq.MQC;
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;
public class MQMain {
public static void main(String[] args) throws Exception {
sendMsg();
getMsg();
}
static void getMsg() throws MQException, IOException {
MQEnvironment.hostname = "172.25.1.69";
MQEnvironment.port = 1414;
MQEnvironment.CCSID = 1381;
MQEnvironment.channel = "ch1";
MQQueueManager qMgr = new MQQueueManager("QM_APPLE");
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;
MQQueue queue = qMgr.accessQueue("Q1_Local", openOptions, null, null, null);
MQMessage msg = new MQMessage();// 要读的队列的消息
MQGetMessageOptions gmo = new MQGetMessageOptions( );
gmo.options = MQC.MQGMO_SYNCPOINT;
int currDepth = queue.getCurrentDepth();
System.out.println(currDepth);
if (currDepth > 0) {
queue.get(msg, gmo);
qMgr.commit();
qMgr.close();
System.out.println(msg.readStringOfCharLength(msg.getDataLength()));
}
}
static void sendMsg() throws MQException, IOException {
MQEnvironment.hostname = "172.25.1.69";
MQEnvironment.port = 1414;
MQEnvironment.CCSID = 1381;
MQEnvironment.channel = "ch1";
MQQueueManager qMgr = new MQQueueManager("QM_APPLE");
int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
MQQueue queue = qMgr.accessQueue("Q1_Local", openOptions, null, null, null);
MQMessage msg = new MQMessage();// 要写入队列的消息
msg.writeString("111111111111111111111111"); //将消息写入消息对象中
MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.options = MQC.MQPMO_SYNCPOINT;
msg.expiry = -1; // 设置消息用不过期
queue.put(msg, pmo);// 将消息放入队列
qMgr.commit();
System.out.println("发送成功");
}
}
java操作MQ
最新推荐文章于 2024-05-10 16:07:25 发布