IBM MQ两个队列管理器之间的通信

本文详细介绍了如何设置IBM MQ,使两个位于不同主机的队列管理器能够相互通信。内容包括:确保队列管理器名称的唯一性,检查端口开放性,创建本地队列、传输队列、远程队列定义,配置发送和接收方通道,并解释了通道名称和传输队列选择的重要性。成功配置后,测试消息可以在两个队列管理器之间传递。

本文章主要介绍两个不同主机间的队列管理器是如何实现相互通信的

前提:

1.确保两边的队列管理器的名称不能相同(如果名称相同将无法通信,在连接的时候虽然发送通道和接收通道都是可以运行的,但是当放入测试消息的时候会报2087 AMQ4048的错误)。

2.确保两个队列管理器直接的监听端口正在运行并且是对外开放的,可以通过telnet ip port进行测试。

实现过程:

队列管理器A (IB9QMGR):

1.新建本地队列 LQ_0000 (这个本地队列实际上准备接受远程信息的)

2.新建传输队列 TQ_0000 (即新建本地队列,然后将属性-->常规中“使用情况”由默认的“正常”改为“传输”)

3.新建远程队列定义 RQ_0000,在属性-->常规中填入相应的远程队列(LQ_8888),远程队列管理器(QM1),选择第2步中创建的传输队列 TQ_0000

4.新建发送方通道 CHL_0000_8888 在属性-->常规中,“连接名称”以ip(port)的形式填入远程主机的IP和端口,如:192.168.178.129(1414)(注意连接名称只能这样填写,不能自己任意命名,而且必须正确,否则两台主机是无法通信的);“传输队列”选择第2步中创建的传输队列 TQ_0000

5.新建接收方通道 CHL_8888_0000

(对于发送和接收方通道要特别注意QM A 的发送方就是QM B 的接收方,两个通道的名称必须是相同的,对于QM B 同理)

队列管理器 B  (QM1):

1.新建本地队列 LQ_8888(这个本地队列实际上准备接受远程信息的)

2.新建传输队列 TQ_8888 (即新建本地队列,然后将属性-->常规中“使用情况”由默认的“正常”改为“传输”)

3.新建远程队列定义 RQ_8888,在属性-->常规中填入相应的远程队列(LQ_0000),远程队列管理器(IB9QMGR),选择第2步中创建的传输队列 TQ_8888

4.新建发送方通道 CHL_8888_0000 在属性-->常规中,“连接名称”以ip(port)的形式填入远程主机的IP和端口,如:192.168.2.49(2414)(注意连接名称只能这样填写,不能自己任意命名,而且必须正确,否则两台主机是无法通信的);“传输队列”选择第2步中创建的传输队列 TQ_8888

5.新建接收方通道 CHL_0000_8888


以上两个队列管理器配置完成后,启动各自的发送方通道,如果正常状态都会是“正在运行”,并且各自的接收方通道也会伴随自动启动,状态为“正在运行”。

可以往QM A 的远程队列 RQ_0000 放入测试消息,如果在QM B 的本地队列 LQ_8888 接收到消息,这说明配置是成功的。

也可以通过以下的应用程序进行测试

运行程序之前必须先在QM A 上建立服务器连接通道 JMS_RECEIVE,以提供给应用程序连上队列管理器

package com.watson.mq;

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 = "IB9QMGR"; 
    private static String qName = "RQ_0000";
    
    private static MQQueueManager qMgr; 
    private static Hashtable properties = new Hashtable();

    public static void main(String args[]) {
        try {
            properties.put("hostname", "192.168.2.49");
            properties.put("port", new Integer(2414));
            properties.put("channel", "JMS_RECEIVE");
            
            // 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();
        }
    }
}

运行以上程序需导入ibm mq的jar包,在<mq_install>/java/lib下的com.ibm.mq.*


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值