搭建JAVA访问WebSphere MQ消息传输
2010-02-25 16:23
267 查看
本文主要通过两台机器,搭建MQ消息传输的环境,并编写测试程序进行测试。
第一、准备工作
准备2台Win2000环境(XP也可),通过以太网连通。
机器A:代码为00000000,IP地址为:10.1.1.1
机器B:代码为88888888,IP地址为:10.1.1.2
安装MQ 5.3
第二、创建MQ对象
A机器上:
1、打开“WebSphere MQ资源管理器”,新建队列管理器,名称为QM_00000000,其余采用默认设置;
2、在QM_00000000队列管理器中创建本地队列,名称为LQ_00000000;
3、创建传输队列,名称为XQ_88888888(新建时选择“本地队列”,将“用法”设置为“传输”);
4、创建远程队列定义,名称为RQ_88888888,指定远程队列名称为LQ_88888888,远程队列管理器名称为QM_88888888,传输队列名称为XQ_88888888;
5、创建发送方通道,名称为00000000.88888888,传输协议为TCP/IP,连接名称为10.1.1.2(1414),传输队列为XQ_88888888;
6、创建接受方通道,名称为88888888.00000000,采用默认设置;
7、创建服务器连接通道,名称为DC.SVRCONN,采用默认设置(该通道主要给后面的测试程序使用)。
B机器和A机器上的操作一样,只是命名不同,如下:
1、打开“WebSphere MQ资源管理器”,新建队列管理器,名称为QM_88888888,其余采用默认设置;
2、在QM_88888888队列管理器中创建本地队列,名称为LQ_88888888;
3、创建传输队列,名称为XQ_00000000(新建时选择“本地队列”,将“用法”设置为“传输”);
4、创建远程队列定义,名称为RQ_00000000,指定远程队列名称为LQ_00000000,远程队列管理器名称为QM_00000000,传输队列名称为XQ_00000000;
5、创建发送方通道,名称为88888888.00000000,传输协议为TCP/IP,连接名称为10.1.1.1(1414),传输队列为XQ_00000000;
6、创建接受方通道,名称为00000000.88888888,采用默认设置;
7、创建服务器连接通道,名称为DC.SVRCONN,采用默认设置。
第三、消息测试
在A、B机器上分别启动其发送方通道,正常情况通道状态应为“正在运行”。
通过如下测试程序进行测试,文件名为:MQTest.java,在机器A上进行运行(如在B上运行请读者自行适当修改)。
import java.io.IOException;
import java.util.Hashtable;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
public class MQSample{
//定义队列管理器和队列的名称
private static String qmName = "QM_00000000";
private static String qName = "RQ_88888888";
private static MQQueueManager qMgr;
private static Hashtable properties = new Hashtable();
public static void main(String args[]) {
try {
properties.put("hostname", "10.1.1.1");
properties.put("port", new Integer(1414));
properties.put("channel", "DC.SVRCONN");
properties.put("CCSID", new Integer(1381));
properties.put("transport","MQSeries");
// Create a connection to the queue manager
qMgr = new MQQueueManager(qmName,properties);
// Set up the options on the queue we wish to open...
int openOptions = 16;
// Now specify the queue that we wish to open,
// and the open options...
MQQueue remoteQ = qMgr.accessQueue(qName, openOptions);
// Define a simple WebSphere MQ message, and write some text in UTF format..
MQMessage putMessage = new MQMessage();
putMessage.writeUTF("Test");
// specify the message options...
MQPutMessageOptions pmo = new MQPutMessageOptions();
// accept the defaults, same as MQPMO_DEFAULT
// put the message on the queue
remoteQ.put(putMessage,pmo);
System.out.println("Message has been input into the Remote Queue");
// Close the queue...
remoteQ.close();
// Disconnect from the queue manager
qMgr.disconnect();
}catch (MQException ex) {
// If an error has occurred in the above, try to identify what went wrong
// Was it a WebSphere MQ error?
System.out.println("A WebSphere MQ error occurred : Completion code " + ex.completionCode +
" Reason code " + ex.reasonCode);
}catch (IOException ex) {
// Was it a Java buffer space error?
System.out.println("An error occurred whilst writing to the message buffer: " + ex);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
第一、准备工作
准备2台Win2000环境(XP也可),通过以太网连通。
机器A:代码为00000000,IP地址为:10.1.1.1
机器B:代码为88888888,IP地址为:10.1.1.2
安装MQ 5.3
第二、创建MQ对象
A机器上:
1、打开“WebSphere MQ资源管理器”,新建队列管理器,名称为QM_00000000,其余采用默认设置;
2、在QM_00000000队列管理器中创建本地队列,名称为LQ_00000000;
3、创建传输队列,名称为XQ_88888888(新建时选择“本地队列”,将“用法”设置为“传输”);
4、创建远程队列定义,名称为RQ_88888888,指定远程队列名称为LQ_88888888,远程队列管理器名称为QM_88888888,传输队列名称为XQ_88888888;
5、创建发送方通道,名称为00000000.88888888,传输协议为TCP/IP,连接名称为10.1.1.2(1414),传输队列为XQ_88888888;
6、创建接受方通道,名称为88888888.00000000,采用默认设置;
7、创建服务器连接通道,名称为DC.SVRCONN,采用默认设置(该通道主要给后面的测试程序使用)。
B机器和A机器上的操作一样,只是命名不同,如下:
1、打开“WebSphere MQ资源管理器”,新建队列管理器,名称为QM_88888888,其余采用默认设置;
2、在QM_88888888队列管理器中创建本地队列,名称为LQ_88888888;
3、创建传输队列,名称为XQ_00000000(新建时选择“本地队列”,将“用法”设置为“传输”);
4、创建远程队列定义,名称为RQ_00000000,指定远程队列名称为LQ_00000000,远程队列管理器名称为QM_00000000,传输队列名称为XQ_00000000;
5、创建发送方通道,名称为88888888.00000000,传输协议为TCP/IP,连接名称为10.1.1.1(1414),传输队列为XQ_00000000;
6、创建接受方通道,名称为00000000.88888888,采用默认设置;
7、创建服务器连接通道,名称为DC.SVRCONN,采用默认设置。
第三、消息测试
在A、B机器上分别启动其发送方通道,正常情况通道状态应为“正在运行”。
通过如下测试程序进行测试,文件名为:MQTest.java,在机器A上进行运行(如在B上运行请读者自行适当修改)。
import java.io.IOException;
import java.util.Hashtable;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
public class MQSample{
//定义队列管理器和队列的名称
private static String qmName = "QM_00000000";
private static String qName = "RQ_88888888";
private static MQQueueManager qMgr;
private static Hashtable properties = new Hashtable();
public static void main(String args[]) {
try {
properties.put("hostname", "10.1.1.1");
properties.put("port", new Integer(1414));
properties.put("channel", "DC.SVRCONN");
properties.put("CCSID", new Integer(1381));
properties.put("transport","MQSeries");
// Create a connection to the queue manager
qMgr = new MQQueueManager(qmName,properties);
// Set up the options on the queue we wish to open...
int openOptions = 16;
// Now specify the queue that we wish to open,
// and the open options...
MQQueue remoteQ = qMgr.accessQueue(qName, openOptions);
// Define a simple WebSphere MQ message, and write some text in UTF format..
MQMessage putMessage = new MQMessage();
putMessage.writeUTF("Test");
// specify the message options...
MQPutMessageOptions pmo = new MQPutMessageOptions();
// accept the defaults, same as MQPMO_DEFAULT
// put the message on the queue
remoteQ.put(putMessage,pmo);
System.out.println("Message has been input into the Remote Queue");
// Close the queue...
remoteQ.close();
// Disconnect from the queue manager
qMgr.disconnect();
}catch (MQException ex) {
// If an error has occurred in the above, try to identify what went wrong
// Was it a WebSphere MQ error?
System.out.println("A WebSphere MQ error occurred : Completion code " + ex.completionCode +
" Reason code " + ex.reasonCode);
}catch (IOException ex) {
// Was it a Java buffer space error?
System.out.println("An error occurred whilst writing to the message buffer: " + ex);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
相关文章推荐
- 用spring搭建微信公众号开发者模式下服务器处理用户消息的加密传输构架(java)
- java搭建本地服务器,android客户端访问,tomcat配置详细步骤
- 阿里云服务器搭建,mysql+java+tomcat+nginx ,mysql远程访问设置
- 【linux】RocketMQ:一个纯java的开源消息中间件--开发测试环境搭建
- 关于阿里云Centos服务器搭建Java网站不能访问的问题
- nat123 + tomcat 在内网搭建外网可以访问的java web服务器
- java语言搭建SSL的Socket并发送字符串消息(最简单应用实例/常见异常及解决)
- java中基于soap协议传输xml消息
- 花生壳+Win7+tomcat+adsl搭建服务器,外网可以访问JAVA项目的页面
- Java 与 C socket通信传输结构体消息/Java解析C语言结构体
- Linux Samba目录服务搭建与Java客户端访问
- Javaの网络数据传输之Http消息格式
- RocketMQ:一个纯java的开源消息中间件--开发测试环境搭建
- RocketMQ:一个纯java的开源消息中间件--开发测试环境搭建
- [转]使用 Java API 处理 WebSphere MQ 大消息
- RocketMQ:一个纯java的开源消息中间件--开发测试环境搭建
- Java 大作业回忆--day04-- 程序框架的搭建和指令的传输
- Tomcat搭建web,提供JAVA页面访问!
- linux搭建java服务器,实现远程访问