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[index];
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();
}
}
}
}
}
分享到:
相关推荐
一个简单的IBM MQ操作例子,put方法和get方法
java IBM MQ 7.5.0 生产者和消费者实例
IBM MQ C++实例代码,连接MQ获取消息
JAVA IBM MQ 接收消息、发送消息例子
MQSEND,MQGet方法,java消息通信方法。java代码实例。
IBM MQ 7.5版本, java代码需要的依赖,解决下载不了ibm
各种语言的IBM MQ开发示例
IBM MQ群集基本概念、群集基本使用、配置、实例,简单的使用技巧
IBM MQ实验3_MQ 通讯配置 实例
手把手教你怎么配置IBM WEBSPHERE MQ通过SSL连接,附详细步骤和实例代码
IBM MQ是一款优秀的消息中间件,本文档详细说明mq是的使用及配置,并附有例子代码。
一个C#实现IBM WebSphere MQ 消息收发的实例,自己平时写的,如遇困难,请留言
多实例队列管理器特征是MQV7.0.1版本之后引进的新特征,它是MQ产品的内置功能,丰富了 MQ 高可用性的解决方案。 用户可以在不同机器上定义并启动此队列管理器的多个实例,包括一个活动实例和一个备用实例。 激活的...
一个C#实现IBM WebSphere MQ 消息收发的实例,包含 发送接收等. 使用的时候只需要修改 appconfig 文件的内容即可. 如有问题.请留言
IBM websphere MQ 多种代码事例
这是IBM的MQ的JAVA示例,有基本的接受、发送DEMO,以及客户端、服务段DEMO(一个队列管理器为客户端,另一个队列管理器为服务端)。
IBM官方提供的监控MQ死信队列的例子和工具。
通过设置ibmmq.usePublications=false可以监视MQ V8实例的一组有限指标。 健康警告 此程序包按原样提供,不保证会提供支持或更新。 也不能保证与该软件包的任何将来版本兼容。 API可能会根据任何反馈进行更改。 在...
IBM WebSphere MQ的使用包括所使用到的jar和相关的调用类
IBM Websphere MQ PCF 编程 实例