ibm linux mq 发送消息_IBM MQ 发送 和 接收

该博客展示了如何使用IBM MQ Java SDK发送和接收消息。`SendDataToMQ`类用于将Push_App对象批量写入MQ,而`ReceiveDataFromMQ`类负责从MQ中读取并处理消息。示例代码中,消息被成功地发送和接收,并进行了相应的日志记录。
摘要由CSDN通过智能技术生成

package com.xiuye.mq;

import java.io.IOException;

import java.util.List;

import org.apache.log4j.Logger;

import com.ibm.mq.MQC;

import com.ibm.mq.MQEnvironment;

import com.ibm.mq.MQException;

import com.ibm.mq.MQMessage;

import com.ibm.mq.MQQueue;

import com.ibm.mq.MQQueueManager;

import com.tienon.message.entity.Push_App;

public class SendDataToMQ {

private static Logger log = Logger.getLogger(SendDataToMQ.class);

static {

MQEnvironment.hostname = "localhost";

MQEnvironment.port = 1417;

MQEnvironment.CCSID = 1381;

MQEnvironment.channel = "Server";//服务器通道

}

private static MQQueueManager manager = null;

private static MQQueue queue = null;

public static void openMQ(String qMgr, String q) throws MQException {

manager = new MQQueueManager(qMgr);

queue = manager.accessQueue(q, MQC.MQOO_OUTPUT);

}

@SuppressWarnings("deprecation")

public static void sendMsg(T data) throws MQException, IOException {

MQMessage msg = new MQMessage();

msg.writeObject(data);

queue.put(msg);

manager.commit();

queue.close();

manager.disconnect();

if (data instanceof Push_App) {

Push_App app = (Push_App) data;

log.info("批次号:" + app.getBatId() + "流水号:" + app.getSerial() + "内容:"

+ app.getMsg() + " 成功插入数据到MQ");

}

}

public static void sendMsgs(List datas) throws MQException,

IOException {

for (T data : datas) {

MQMessage msg = new MQMessage();

msg.writeObject(data);

queue.put(msg);

manager.commit();

if (data instanceof Push_App) {

Push_App app = (Push_App) data;

log.info("批次号:" + app.getBatId() + "流水号:" + app.getSerial()

+ "内容:" + app.getMsg() + " 成功插入数据到MQ");

}

}

queue.close();

manager.disconnect();

}

public static void closeMQ() throws MQException {

//manager.commit();//不要重复commit,否者会报错的

queue.close();

manager.disconnect();

}

}

package com.xiuye.mq;

import java.io.IOException;

import java.io.InvalidClassException;

import java.io.OptionalDataException;

import java.io.StreamCorruptedException;

import java.util.ArrayList;

import java.util.List;

import com.ibm.mq.MQC;

import com.ibm.mq.MQEnvironment;

import com.ibm.mq.MQException;

import com.ibm.mq.MQMessage;

import com.ibm.mq.MQQueue;

import com.ibm.mq.MQQueueManager;

public class ReceiveDataFromMQ {

static {

MQEnvironment.hostname = "localhost";

MQEnvironment.port = 1417;

MQEnvironment.CCSID = 1381;

MQEnvironment.channel = "Server";//服务器通道

}

private static MQQueueManager manager = null;

private static MQQueue queue = null;

public static void openMQ(String qMgr, String q) throws MQException {

manager = new MQQueueManager(qMgr);

//必须要写MQC.MQOO_INQUIRE,否者无法查询队列深度(即消息条数)

queue = manager.accessQueue(q, MQC.MQOO_INPUT_AS_Q_DEF|MQC.MQOO_INQUIRE);

}

public static void closeMQ() throws MQException {

queue.close();

manager.disconnect();

}

public static Object receivedMsg() throws MQException,

StreamCorruptedException, OptionalDataException,

ClassNotFoundException, IOException {

MQMessage msg = new MQMessage();

queue.get(msg);

Object obj = msg.readObject();

manager.commit();

return obj;

}

public static List receiveAllMsgs() throws MQException,

StreamCorruptedException, OptionalDataException,

ClassNotFoundException, IOException {

List datas = new ArrayList();

while ( queue.getCurrentDepth() > 0) {

MQMessage msg = new MQMessage();

queue.get(msg);

datas.add(msg.readObject());

manager.commit();

}

return datas;

}

public static List receivePartMsgs(int sum) throws MQException,

InvalidClassException, StreamCorruptedException,

OptionalDataException, ClassNotFoundException, IOException {

List datas = new ArrayList();

while (sum > 0) {

MQMessage msg = new MQMessage();

queue.get(msg);

datas.add(msg.readObject());

manager.commit();

sum--;

}

return datas;

}

}

@Test

public void sendMsg() {

List list = appDao.findTenDatas();

System.out.println(list.size());

for (Push_App app : list) {

System.out.println(app.getSerial() + ":" + app.getMsg());

}

try {

SendDataToMQ.openMQ("AppMsg", "SendMsg");

SendDataToMQ.sendMsgs(list);

//SendDataToMQ.sendMsg(list.get(0));

SendDataToMQ.closeMQ();

} catch (MQException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

@Test

public void receiveMsg(){

try {

ReceiveDataFromMQ.openMQ("AppMsg", "SendMsg");

List list = ReceiveDataFromMQ.receiveAllMsgs();

//List list = ReceiveDataFromMQ.receivePartMsgs(10);

for(Object data : list){

if(data instanceof Push_App){

Push_App app = (Push_App) data;

System.out.println("序列号:"+app.getSerial()+" 发送的消息是:" + app.getMsg());

}

}

ReceiveDataFromMQ.closeMQ();

} catch (MQException e) {

e.printStackTrace();

} catch (StreamCorruptedException e) {

e.printStackTrace();

} catch (OptionalDataException e) {

e.printStackTrace();

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

10

23:123456

2:123456

30:123456

22:123456

888:123456

21:123456

24:123456

5:123456

4:123456

28:123456

序列号:23 发送的消息是:123456

序列号:2 发送的消息是:123456

序列号:30 发送的消息是:123456

序列号:22 发送的消息是:123456

序列号:888 发送的消息是:123456

序列号:21 发送的消息是:123456

序列号:24 发送的消息是:123456

序列号:5 发送的消息是:123456

序列号:4 发送的消息是:123456

序列号:28 发送的消息是:123456

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值