coverl 发表于 2017-5-24 12:23:47

IBM MQ 简单例子

package com.founder.gome.bg.service.mq;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.ibm.mq.MQException;
import com.ibm.mq.MQQueueManager;
/**
* @author HP
*
*/
public class MQCommon {
public static String MQ_MANAGER = null;//队列管理器名称
public static MQQueueManager qMgr = null;
public static Properties props = null;
public static MQQueueManager getMQQueueManager() {
if(qMgr==null) {
try {
qMgr = new MQQueueManager(MQ_MANAGER);
} catch (MQException e) {
e.printStackTrace();
}
}
return qMgr;
}
public static Properties getProperties() {
if(props==null) {
try {
props = new Properties();
InputStream ips = new BufferedInputStream(new FileInputStream("D://MQ.properties"));
props.load(ips);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return props;
}
public static void mqClose() {
try {
if(qMgr!=null) {
qMgr.close();
qMgr.disconnect();
qMgr = null;
}
} catch (MQException e) {
e.printStackTrace();
}
}

}



package com.founder.gome.bg.service.mq;
import com.founder.gome.bg.service.mq.impl.POServiceImpl;
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.MQQueue;
import com.ibm.mq.MQQueueManager;
public class MQReceiver implements Runnable{   
private String MQ_QUEUE_NAME = "QL_GOME02_EC_SO_TO_DRAGON_SO";
private MQQueueManager qMgr = MQCommon.getMQQueueManager();
public MQReceiver (String mqQueueName) {
if (null != mqQueueName && !"".equals(mqQueueName)) {
this.MQ_QUEUE_NAME = mqQueueName;
}
}
@SuppressWarnings("unchecked")
public void run() {   
int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_OUTPUT | MQC.MQOO_INQUIRE;   
MQQueue queue = null;   
try {
MQEnvironment.hostname = MQCommon.props.getProperty("MQ_HOST_NAME");   
MQEnvironment.channel = MQCommon.props.getProperty("MQ_CHANNEL");   
MQEnvironment.port = Integer.valueOf(MQCommon.props.getProperty("MQ_PROT"));   
MQEnvironment.CCSID = Integer.valueOf(MQCommon.props.getProperty("MQ_CCSID"));
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
queue = qMgr.accessQueue(MQ_QUEUE_NAME, openOptions, null, null,null);   
int depth = queue.getCurrentDepth();   
while(depth-- > 0)   
{   
MQMessage msg = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();   
queue.get(msg, gmo);
int index = msg.getMessageLength();
byte[] buffer = new byte;
msg.readFully(buffer, 0, index);
String message = new String(buffer, "utf-8");
new POServiceImpl().getXML(message, MQ_QUEUE_NAME);
}   
} catch (MQException e) {   
e.printStackTrace();   
} catch (Exception e) {   
e.printStackTrace();   
} finally {
if(queue!=null){   
try {   
queue.close();   
} catch (MQException e) {   
e.printStackTrace();   
}   
}
}   
}
}



package com.founder.gome.bg.service.mq;
import java.io.IOException;
import com.founder.gome.bg.service.mq.impl.POServiceImpl;
import com.ibm.mq.MQC;
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
public class MQSender implements Runnable {   
private String MQ_QUEUE_NAME = "QR_GOME02_EC_SO_TO_DRAGON_SO";
private String xml;
private MQQueue mqQueue = null;   
public MQSender(String xml, String mqQueueName) {
this.xml = xml;
if (null != mqQueueName && !"".equals(mqQueueName)) {
this.MQ_QUEUE_NAME = mqQueueName;
}
}
@SuppressWarnings("unchecked")
public void run() {   
try {   
MQEnvironment.hostname = MQCommon.props.getProperty("MQ_HOST_NAME");   
MQEnvironment.channel = MQCommon.props.getProperty("MQ_CHANNEL");   
MQEnvironment.port = Integer.valueOf(MQCommon.props.getProperty("MQ_PROT"));   
MQEnvironment.CCSID = Integer.valueOf(MQCommon.props.getProperty("MQ_CCSID"));
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
int sendOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;   
mqQueue = MQCommon.getMQQueueManager().accessQueue(MQ_QUEUE_NAME, sendOptions, null, null, null);   
MQPutMessageOptions mqPutMessageOptions = new MQPutMessageOptions();   
MQMessage mqMessage = new MQMessage();
mqMessage.write(xml.getBytes("utf-8"));   
mqQueue.put(mqMessage, mqPutMessageOptions);
POServiceImpl poServiceImpl = new POServiceImpl();
poServiceImpl.writeFile(xml);
} catch (MQException e) {   
e.printStackTrace();   
} catch (IOException e1) {   
e1.printStackTrace();   
} finally {   
if (mqQueue != null) {   
try {   
mqQueue.close();   
} catch (MQException e) {   
e.printStackTrace();   
}   
}   
}   
}   

}
页: [1]
查看完整版本: IBM MQ 简单例子