关闭

IBM MQ 发送 和 接收

3283人阅读 评论(4) 收藏 举报
分类:
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 <T> 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 <T> void sendMsgs(List<T> 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<Object> receiveAllMsgs() throws MQException,
			StreamCorruptedException, OptionalDataException,
			ClassNotFoundException, IOException {

		List<Object> datas = new ArrayList<Object>();
		
		while ( queue.getCurrentDepth() > 0) {

			MQMessage msg = new MQMessage();
			queue.get(msg);
			datas.add(msg.readObject());
			manager.commit();
			
		}
		
		return datas;
	}

	public static List<Object> receivePartMsgs(int sum) throws MQException,
			InvalidClassException, StreamCorruptedException,
			OptionalDataException, ClassNotFoundException, IOException {

		List<Object> datas = new ArrayList<Object>();

		while (sum > 0) {

			MQMessage msg = new MQMessage();
			queue.get(msg);
			datas.add(msg.readObject());
			manager.commit();
			sum--;

		}

		return datas;

	}

}




@Test
	public void sendMsg() {
		List<Push_App> 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<Object> list = ReceiveDataFromMQ.receiveAllMsgs();
			//List<Object> 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





0
1

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:120395次
    • 积分:4279
    • 等级:
    • 排名:第7065名
    • 原创:305篇
    • 转载:94篇
    • 译文:0篇
    • 评论:18条
    文章分类
    最新评论