mq java_MQ测试JAVA客户端实现源码

/**

* TestFotMQ.java

*/

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.util.Properties;

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

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;

/**

* 函数功能: MQjava客户端实现

* 开发人员:@author Zeming_gao

* 开发时间:2013-3-21

* 修改记录:

*/

public class MQClient {

/**

* Logger for this class

*/

private static final Log logger = LogFactory.getLog(MQClient.class);

private String strExtraSendXmlFileName = "D:\\jndi.properties";

private static Properties props;

static {

props = new Properties();

props.put("mqHostName", "139.31.89.67");

props.put("mqPort", "4032");

props.put("mqCCSID", "1381");

props.put("mqUserName", "liujx");

props.put("mqPassword", "linux");

props.put("mqQManager", "Monitor_Queue_Manager");

props.put("mqChannel", "Monitor_Conn_Chanel");

props.put("mqLocalOutQueue", "Q_RECEIVE");

props.put("mqLocalInQueue", "Q_RECEIVE");

}

/**

*

* 函数功能:TODO 主测试方法

* 开发人员:@author Zeming_gao

* 开发时间:2013-3-21

* 相关参数:

*

* @param args

* 修改记录:

*/

public static void main(String[] args) {

MQClient test = new MQClient();

// 发送消息

test.putMsg();

// 接收消息

test.getMsg();

}

public void putMsg() {

// MQ发送数据

try {

// 建立MQ客户端应用上下文环境

MQEnvironment.hostname = props.getProperty("mqHostName");

MQEnvironment.port = Integer.parseInt(props.getProperty("mqPort"));

MQEnvironment.CCSID = Integer

.parseInt(props.getProperty("mqCCSID"));

MQEnvironment.channel = props.getProperty("mqChannel");

MQEnvironment.userID = props.getProperty("mqUserName");

MQEnvironment.password = props.getProperty("mqPassword");

// 连接队列管理器

MQQueueManager qMgr = new MQQueueManager(

props.getProperty("mqQManager"));

int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;

// 打开队列

MQQueue q = null;

try {

q = qMgr.accessQueue(props.getProperty("mqLocalOutQueue"),

openOptions);

} catch (MQException me) {

System.out.println("打开队列出现通讯异常" + me.getMessage() + "\n");

return;

}

InputStream fins = new FileInputStream(new File(

strExtraSendXmlFileName));

byte[] data = new byte[fins.available()];

fins.read(data);

fins.close();

MQMessage msg = new MQMessage();

msg.write(data);

// 放入消息

q.put(msg);

System.out.println("客户端发送数据包成功..");

// 关闭队列

q.close();

// 断开队列管理器连接

qMgr.disconnect();

} catch (MQException e) {

if (logger.isDebugEnabled())

logger.debug(e.getMessage());

e.printStackTrace();

} catch (Exception e) {

if (logger.isDebugEnabled())

logger.debug(e.getMessage());

e.printStackTrace();

}

}

/**

*

* 函数功能:TODO 获取数据

* 开发人员:@author Zeming_gao

* 开发时间:2013-3-21

* 相关参数:

* 修改记录:

*/

public void getMsg() {

// MQ接收数据

try {

// 建立用上下文环境

MQEnvironment.hostname = props.getProperty("mqHostName");

MQEnvironment.port = Integer.parseInt(props.getProperty("mqPort"));

MQEnvironment.CCSID = Integer

.parseInt(props.getProperty("mqCCSID"));

MQEnvironment.channel = props.getProperty("mqChannel");

MQEnvironment.userID = props.getProperty("mqUserName");

MQEnvironment.password = props.getProperty("mqPassword");

// 建立队列管理器

MQQueueManager qMgr = new MQQueueManager(

props.getProperty("mqQManager"));

int openOptions = MQC.MQOO_INPUT_AS_Q_DEF

| MQC.MQOO_FAIL_IF_QUIESCING;

// 打开队列

MQQueue q = qMgr.accessQueue(props.getProperty("mqLocalInQueue"),

openOptions);

MQGetMessageOptions mgo = new MQGetMessageOptions();

mgo.options |= MQC.MQGMO_NO_WAIT;

// 构造返回消息

MQMessage msg = new MQMessage();

if ((msg = fetchOneMsg(q)) != null) {

byte[] xmlData = new byte[msg.getDataLength()];

msg.readFully(xmlData);

logger.info(new String(xmlData));

System.out.println("接收服务器端返回数据包成功..\n接收数据为:\n"

+ new String(xmlData));

}

// 关闭队列

q.close();

// 断开队列管理器

qMgr.disconnect();

} catch (MQException e) {

logger.error(e);

e.printStackTrace();

} catch (Exception e) {

logger.error(e);

e.printStackTrace();

}

}

/**

*

* 函数功能:TODO 从队列中取出消息

* 开发人员:@author Zeming_gao

* 开发时间:2013-3-21

* 相关参数:

*

* @param q

* @return

* @throws Exception

* 修改记录:

*/

private static MQMessage fetchOneMsg(MQQueue q) throws Exception {

MQGetMessageOptions mgo = new MQGetMessageOptions();

mgo.options |= MQC.MQGMO_NO_WAIT;

MQMessage msg = new MQMessage();

try {

// 获取消息

q.get(msg, mgo);

} catch (MQException e) {

return null;

}

return msg;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值