`

Websphere MQ 开发实例

    博客分类:
  • MQ
 
阅读更多
IBM MQSeries基本由一个消息传输系统和一个应用程序接口组成,其资源是消息和队列(Messaging and Queuing)。 队列管理器(Queue Manager):管理队列的系统,实现网络通信,保证消息安全可靠地传输到目的地。
用于确保队列之间的信息提供,包括网络中不同系统上的的远程队列之间的信息提供。并保证网络故障或关闭后的恢复。
队列:一个安全的信息存储区。因为信息存放在队列中,所以应用程序可以相互独立的运行,以不同的速度,在不同的时间,在不同的地点。
本地队列:对程序而言,本地队列属于该程序所连接的队列管理器。
远程队列:该队列不属于该程序所连接的队列管理器,而只是远端队列管理器的队列在本地的定义。
传输队列:它是一个本地队列,保存了指定要发送到远端的消息。
死信队列:它是一个本地队列,用于存放无法传递的消息。
通道:在两个队列管理器之间建立起来的数据传输链路。
应用程序接口:应用程序和信息系统之间通过MQSeries API实现的接口。



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;
import com.ibm.mq.MQQueueManager;

/**
 * 发送消息程序
 */
public class Send {

 public static void main(String[] args) {
   try {

      // 主机名称
      String hostName = "192.168.1.101";
      // 端口(缺省 1414)
      int port = 1515;
      // 通道名称(缺省)
      String channel = "SYSTEM.DEF.SVRCONN";
      // 队列管理器名称
      String qManager = "send";
      // 队列名称 (远程队列名)
      String qName = "Q1";

      // Set up the MQEnvironment properties for Client Connections.
      // 建立MQEnvironment 属性以便客户机连接.
      MQEnvironment.hostname = hostName;
      MQEnvironment.port = port;
      MQEnvironment.channel = channel;
      MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
      MQEnvironment.CCSID = 1381; 
      
      // Connection To the Queue Manager.
      // 连接到队列管理器.
      MQQueueManager qMgr = new MQQueueManager(qManager);

      /**
       * Set up the open options to open the queue for out put
       * and additionally we have set the option to fail if the queue manager
       * is quiescing.
       *
       * 建立打开选项以便打开用于输出的队列,进一步而言,如果队列管理器是
       * 停顿的话,我们也已设置了选项去应对不成功情况.
       */
      int openOptions = MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING;

      // Open the queue.
      // 打开队列.
      MQQueue queue = qMgr.accessQueue(qName, openOptions, null, null,
        null);

      // Set the put message options , we will use the default setting.
      // 设置放置消息选项我们将使用默认设置.
      MQPutMessageOptions pmo = new MQPutMessageOptions();

      /**
       * Next we Build a message The MQMessage class encapsulates the data
       * buffer that contains the actual message data, together with all the MQMD
       * parameters that describe the message.
       *
       * 下一步我们建立消息,MQMessage类压缩了包含实际消息数据的数据缓冲区,
       * 和描述消息的所有MQMD 参数.
       *
       * To Build a new message, create a new instance of MQMessage class
       * and use writxxx (we will be using writeString method).
       * The put() method of MQQueue also takes an instance of the
       * MQPutMessageOptions class as a parameter.
       *
       * 欲建立新消息,创建MQMessage类新实例以及使用writxxx(我们将使用writeString 方法.).
       * MQQueue 的put()方法也可作为参数MQPutMessageOptions 类的实例.
       */

      // Create The message buffer.
      // 创建消息缓冲区.
      MQMessage outMsg = new MQMessage();

      // Set the MQMD format field.
      // 设置MQMD 格式字段.
      outMsg.format = MQC.MQFMT_STRING;
              

      // Prepare message with user data.
      // 准备用户数据消息.
      String msgString = "HEAD7C5218991310615536   BOCOMC     0100521899        200909170720470020090917101000000022164428000000020000000000000000000000020000  1110408715  0000    0 1564439990000044399900000                          1 1  000                                               DOMN200909170720470016100115400000000156          HA1310011130028738445 AT032007101900000068835700000110000000000000000020090915000000010000000000431643000000000000                       PENG PING          6532CAFCR102                                                                    000281548016372856DQPD        2007100120071030                               000002000000000000000000000068835700000110000000000000000000000001000000000043164300000048411900000010000000620090917072055550                                                                                                                                      19790117                                                               518000          20090917000000272977113714719646         1310710130979       MALE                    SSNO180001016790                             MS0000000000                2007101920090912000000002007103001                  0           00000004841192007102004064394C       000000000           TAIL";

      // Now we put The message on the Queue.
      // 现在我们在队列上放置消息.
      outMsg.writeString(msgString);


      // Commit the transaction.
      // 提交事务处理.
      queue.put(outMsg, pmo);
      qMgr.commit();
      System.out.println(" The message has been Sussesfully put/n/n#########");

      // Close the the Queue and Queue manager objects.
      // 关闭队列和队列管理器对象.
      queue.close();
      qMgr.disconnect();

     } catch (MQException ex) {

      System.out.println("An MQ Error Occurred: Completion Code is :/t" +
      ex.completionCode + "/n/n The Reason Code is :/t" + ex.reasonCode);
      ex.printStackTrace();

     } catch (Exception e) {

      e.printStackTrace();

     }

 }

}

 

 

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;

/**
 * 消息接收器应用程序
 *
 * 调入MQSeries Java API package
 * 为客户机连接设置环境属性
 * 连接到队列管理器
 * 为打开MQSeries 队列设置选项
 * 为获取消息打开应用程序
 * 设置选项,从应用程序队列获取消息
 * 创建消息缓冲区
 * 从队列获取消息到消息缓冲区
 * 从消息缓冲区读取用户数据并在控制台上显示
 */

public class Receiver {

  public static void PtpReceiver() {
    try {
     
     // 主机名称
     String hostName = "192.168.1.101";
     // 端口(缺省 1414)
     int port = 1616;
     // 通道名称(缺省)
     String channel = "SYSTEM.DEF.SVRCONN";
     // 队列管理器名称
     String qManager = "receive";
     // 队列名称 (本地对列名)
     String qName = "Q1";

     // 建立MQEnvironment 属性以便客户机连接
     MQEnvironment.hostname = hostName;
     MQEnvironment.port = port;
     MQEnvironment.channel = channel;
     MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
     MQEnvironment.CCSID = 1381; 
     
     // Connection To the Queue Manager.
     // 连接到队列管理器.
     MQQueueManager qMgr = new MQQueueManager(qManager);

     /**
      * Set up the open options to open the queue for out put and
      * additionally we have set the option to fail if the queue manager
      * is quiescing.
      *
      * 建立打开选项以便打开用于输出的队列,
      * 进一步而言,如果队列管理器是停顿的话,我们也
      * 已设置了选项去应对不成功情况.
      */

     // Open the queue.
     // 打开队列.
     int openOptions = MQC.MQOO_INPUT_SHARED | MQC.MQOO_FAIL_IF_QUIESCING;

     // Set the put message options.
     // 设置放置消息选项.
     MQQueue queue = qMgr.accessQueue(qName, openOptions, null, null, null);

     MQGetMessageOptions gmo = new MQGetMessageOptions();

     // Get messages under sync point control.
     // 在同步点控制下获取消息.
     gmo.options = gmo.options + MQC.MQGMO_SYNCPOINT;

     // Wait if no messages on the Queue.
     // 如果在队列上没有消息则等待.
     gmo.options = gmo.options + MQC.MQGMO_WAIT;

     // Fail if QeueManager Quiescing.
     // 如果队列管理器停顿则失败.
     gmo.options = gmo.options + MQC.MQGMO_FAIL_IF_QUIESCING;

     // Sets the time limit for the wait.
     // 设置等待的时间限制.
     gmo.waitInterval = 3000;
    

     /**
      * Next we Build a message The MQMessage class encapsulates the data
      * buffer that contains the actual message data, together with all the MQMD
      * parameters that describe the message.
      *
      * 下一步我们建立消息,MQMessage
      * 类压缩了包含实际消息数据的数据缓冲区,
      * 和描述消息的所有MQMD 参数.
      */

     // Create the message buffer.
     // 创建消息缓冲区.
     MQMessage inMsg = new MQMessage();

     // Get the message from the queue on to the message buffer.
     // 从队列到消息缓冲区获取消息.
     queue.get(inMsg, gmo);

     // Read the User data from the message.
     // 从消息读取用户数据.
     String msgString = inMsg.readString(inMsg.getMessageLength());
     // 控制台输出
     System.out.println(" The Message from the Queue is : /n/n" + msgString);

     // Commit the transaction.
     // 提交事务处理.
     qMgr.commit();

     // Close the the Queue and Queue manager objects.
     // 关闭队列和队列管理器对象.
     queue.close();
     qMgr.disconnect();

    } catch (MQException ex) {

     System.out.println("An MQ Error Occurred: Completion Code is :/t" +
     ex.completionCode + "/n/n The Reason Code is :/t" + ex.reasonCode);
     ex.printStackTrace();

    } catch (Exception e) {

     e.printStackTrace();

    }

   }

   /**
    * @param args
    */
   public static void main(String[] args) {
    // TODO Auto-generated method stub
    PtpReceiver();
   }

}

 


注意字符集问题

一:MQEnvironment.CCSID = 1381;(在JAVA连接代码时指定一下字符集)

二:修改字符集设置

一般Unix、Linux平台中MQ默认的字符集为819,而Windows平台为1381,所以你必须改变其字符集,使两边的字符集相同。改变方法:

1.通过DOS进入MQ的安装目录,进入/bin下。假如要更改的队列管理器为A

2.用指令“strmqm A”启动队列管理器A。

3.用指令“runmqsc A”启动A的MQSC。

4.运行指令“ALTER QMGR CCSID(819)”“end”则修改字符集为819
分享到:
评论

相关推荐

    Websphere MQ入门教程

    6.2.4 WebSphere MQ对象配置实例 81 6.3通道的维护 83 6.3.1通道的状态 83 6.3.2通道维护命令 84 6.3.3设置MaxChannels和MaxActiveChannels属性 88 6.4配置侦听程序 88 6.4.1 Windows 平台 88 6.4.2 unix 平台 88 ...

    WebSphere MQ自学笔记

    本人自学IBM的WebSphereMQ自学笔记,内有MQ安装文档,建MQ的例子等。 1、 MQ6.0安装 2 1.1 启动MQ v6.0 安装程序 2 1.2 软件需求检查 2 1.3 WebSphere Eclipse Platform V3.0.1软件安装 2 1.4 网络配置检查 3 1.5 ...

    Websphere MQ入门教程.doc

    6.2.4 WebSphere MQ对象配置实例 81 6.3通道的维护 83 6.3.1通道的状态 83 6.3.2通道维护命令 84 6.3.3设置MaxChannels和MaxActiveChannels属性 88 6.4配置侦听程序 88 6.4.1 Windows 平台 88 6.4.2 unix 平台 88 ...

    IBM websphere MQ 多种代码事例

    IBM websphere MQ 多种代码事例

    精通 WebSphere MQ pdf 版

    和实例模型,对于WebSphere MQ 设计和编程人员会有相当的吸引力,可以作为有一定经验 者的高级读物,也是相关开发人员必不可少的参考书。 本书注重实践,附有大量例程,帮助读者在实践中加深理解,也为相关设计和...

    LINUX MQ开发实例

    useful material for the entry level MQ developer.

    简单的MQ使用实例,详细解释

    从简单的demo中进行了界面开发。初学很有用!

    Websphere Message Broker 入门经典必备实例

    Websphere Message Broker 入门经典必备实例 一共3个实例,详细的PDF实例,

    MQTest.rar

    针对IBM WebSphere MQ,开发的MQ访问类和调用实例。MQ,消息队列的简称是一种应用程序对应用程序的通信方法。说白了也就是通过队列的方式来对应用程序进行数据通信。而无需专用链接来链接它们。

    ibm_integration_bus:IBM Integration Bus的厨师食谱

    所有产品先决条件,包括WebSphere MQ版本7.5.0.1 该食谱还创建了一个用于管理组件的用户帐户,并创建和配置了运行中的Integration Bus实例所需的所有组件。 有关此食谱的问题的答案,请参见提供的。

Global site tag (gtag.js) - Google Analytics