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