java操作MQ




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("发送成功");
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值