实现两个MQ队列管理器间进行消息传送的程序的方法
实现两个MQ队列管理器间进行消息传送的程序的方法
从上周开始看websphere mq,从一无所知到现在终于有所了解,写了个简单的传送消息代码,
我现在是通过在两个队列管理器间设置远程队列来进行消息传递的,
这两天写了一个传送一百多M文件的程序,
还没有在远程机器上测试过,不知道传送效果如何?
先把传送消息的代码贴出来:
/*==============================================================================<br>
* 版权: 版权所有 (c) 2004 - 2005<br>
* 文件包:Message<br>
* 所含类:<br>
* 编写人员:alaxing<br>
* 创建日期:2004-10-23<br>
* 功能说明:两个MQ队列管理器间进行消息传送的简单程序实现<br>
* 更新记录:<br>
* 日期 作者 内容<br>
* =============================================================================<br>
*
* ==============================================================================*/
import com.ibm.mq.*;
public class Message {
private MQQueueManager qMgr;
private MQQueue mqQueue;
public Message() {
}
/**
* 从主机名为chenj1的MQ发送消息到主机名为linxf的MQ上
*/
public void putMsg() {
try {
//源方主机名
String hostName = "chenj1";
//源方服务器连接通道
String channel = "S_chenj1";
//源方队列管理器
String qManager = "QM_chenj1";
//远程队列管理器 定义远程队列管理器为QM_linxf ,远程队列为file_linxf
String qName = "remote_chenj_linxf";
MQEnvironment.hostname = hostName;
MQEnvironment.channel = channel;
MQEnvironment.CCSID = 1381;
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
MQC.TRANSPORT_MQSERIES);
MQEnvironment.disableTracing();
MQException.log = null;
//连接到队列管理器
qMgr = new MQQueueManager(qManager);
System.out.println("成功连接QueueManager");
int openOptions =MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;
mqQueue = qMgr.accessQueue(qName, openOptions);
MQPutMessageOptions mpmo = new MQPutMessageOptions();
MQMessage mqMessage = new MQMessage();
mqMessage.writeString(new java.util.Date().toString());
mqQueue.put(mqMessage, mpmo);
}
catch (MQException ex) {
System.out.println("Error Code : " + ex.reasonCode);
ex.printStackTrace();
}
catch (java.io.IOException ex) {
ex.printStackTrace();
}
finally {
try {
if (mqQueue != null) {
mqQueue.close();
System.out.println("成功关闭Queue");
}
if (qMgr != null) {
qMgr.close();
qMgr.disconnect();
System.out.println("成功关闭、断开QueueManager");
}
}
catch (MQException ex) {
} //end try-catch
} //end try-catch
}
/**
* 从主机名为linxf的MQ上接收消息
*/
public void getMsg() {
try {
//接收方主机名
String hostName = "linxf";
//接收方服务器连接通道
String channel = "S_linxf";
//接收方队列名称
String qManager = "QM_linxf";
//接收方接收的本地队列
String qName = "file_linxf";
MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY,
MQC.TRANSPORT_MQSERIES);
MQEnvironment.CCSID = 1381;
MQEnvironment.hostname = hostName;
MQEnvironment.channel = channel;
MQQueueManager qMgr = new MQQueueManager(qManager);
System.out.println("成功连接QueueManager");
int openOptions = MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING;
mqQueue = qMgr.accessQueue(qName, openOptions);
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;
gmo.options = gmo.options + MQC.MQGMO_WAIT;
gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;
gmo.waitInterval = 3000;
MQMessage inMsg = new MQMessage();
mqQueue.get(inMsg, gmo);
String msg = inMsg.readString(inMsg.getMessageLength());
System.out.println("this message is " + msg);
qMgr.commit();
}
catch (MQException ex) {
System.out.println("Error Code : " + ex.reasonCode);
ex.printStackTrace();
}
catch (java.io.IOException ex) {
ex.printStackTrace();
}
finally {
try {
if (mqQueue != null) {
mqQueue.close();
System.out.println("成功关闭Queue");
}
if (qMgr != null) {
qMgr.close();
qMgr.disconnect();
System.out.println("成功关闭、断开qMgr");
}
}
catch (MQException ex) {
} //end try-catch
} //end try-catch
}
public static void main(String[] args) {
Message msg = new Message();
msg.putMsg();
msg.getMsg();
}
}