【问题标题】:How to connect to the MQ Server QUEUE MANAGER using MQ Client如何使用 MQ 客户端连接到 MQ 服务器队列管理器
【发布时间】:2017-02-17 04:19:54
【问题描述】:

我尝试使用 MQ 客户端向 MQ 服务器发送消息

错误是:- 发生 WebSphere MQ 错误:完成代码 2 原因代码 2058。我知道这个原因代码是因为队列管理器名称错误..但是队列管理器名称是正确的......

安装 WebSphere MQ 客户端后,我只需运行以下命令: SET MQSERVER=QM_ORANGE/TCP/IPADDRESS(端口号)

并运行这个程序 公共类 MQSample {

  // code identifier
  static final String sccsid = "@(#) MQMBID sn=p750-002-131001_DE su=_FswqMCqGEeOZ3ui-rZDONA pn=MQJavaSamples/wmqjava/MQSample.java";

  // define the name of the QueueManager
  private static final String qManager = "QM_ORANGE";
  // and define the name of the Queue
  private static final String qName = "Q1";

  /**
   * Main entry point
   * 
   * @param args - command line arguments (ignored)
   */
  public static void main(String args[]) {
    try {
      // Create a connection to the QueueManager
      System.out.println("Connecting to queue manager: " + qManager);
      MQQueueManager qMgr = new MQQueueManager(qManager);

      // Set up the options on the queue we wish to open
      //int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF | MQConstants.MQOO_OUTPUT;
      int openOptions = MQConstants.MQOO_OUTPUT;
     // int openOptions1 = MQConstants.MQOO_INPUT_AS_Q_DEF;
      // Now specify the queue that we wish to open and the open options
      System.out.println("Accessing queue: " + qName);
      MQQueue queue = qMgr.accessQueue(qName, openOptions);
      //MQQueue queue1 = qMgr.accessQueue(qName, openOptions1);
      // Define a simple WebSphere MQ Message ...
      MQMessage msg = new MQMessage();
      // ... and write some text in UTF8 format
      msg.writeUTF("Hello, World!");

      // Specify the default put message options
      MQPutMessageOptions pmo = new MQPutMessageOptions();

      // Put the message to the queue
      System.out.println("Sending a message...");
      queue.put(msg, pmo);


      //



         openOptions = MQC.MQOO_INQUIRE + MQC.MQOO_FAIL_IF_QUIESCING
                + MQC.MQOO_INPUT_SHARED;

         queue = qMgr.accessQueue("QM_APPLE", openOptions,
                null, // default q manager
                null, // no dynamic q name
                null); // no alternate user id

        System.out.println("MQRead v1.0 connected.\n");

        int depth = queue.getCurrentDepth();
        System.out.println("Current depth: " + depth + "\n");
        if (depth == 0) {
            return;
        }

        MQGetMessageOptions getOptions = new MQGetMessageOptions();
        getOptions.options = MQC.MQGMO_NO_WAIT + MQC.MQGMO_FAIL_IF_QUIESCING
                + MQC.MQGMO_CONVERT;
        while (true) {

            MQMessage message = new MQMessage();
            try {
                queue.get(message, getOptions);

                byte[] b = new byte[message.getMessageLength()];
                message.readFully(b);
                System.out.println(new String(b));
                message.clearMessage();
            } catch (IOException e) {
                System.out.println("IOException during GET: " + e.getMessage());
                break;
            } catch (MQException e) {
                if (e.completionCode == 2
                        && e.reasonCode == MQException.MQRC_NO_MSG_AVAILABLE) {
                    if (depth > 0) {
                        System.out.println("All messages read.");
                    }
                } else {
                    System.out.println("GET Exception: " + e);
                }
                break;
            }
        }
        queue.close();
        //_queueManager.disconnect();

      // Disconnect from the QueueManager
      System.out.println("Disconnecting from the Queue Manager");
      qMgr.disconnect();
      System.out.println("Done!");
    }
    catch (MQException ex) {
      System.out.println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode
          + " Reason Code " + ex.reasonCode);
      ex.printStackTrace();
      for (Throwable t = ex.getCause(); t != null; t = t.getCause()) {
        System.out.println("... Caused by ");
        t.printStackTrace();
      }

    }
    catch (java.io.IOException ex) {
      System.out.println("An IOException occured whilst writing to the message buffer: " + ex);
    }
    return;
  }
}

【问题讨论】:

    标签: ibm-mq mq


    【解决方案1】:

    您已设置MQSERVER 环境变量。 MQ C Client 理解这个环境变量并相应地连接到在 IP 地址中指定的机器上运行的队列管理器。 MQ Java 的行为方式不同。

    在您的应用程序中,您只在MQQueueManager 构造函数中指定了队列管理器名称。这意味着应用程序希望通过服务器绑定连接连接到在同一台机器上运行的队列管理器。

    您可以执行以下操作来连接到队列管理器:(更改主机、端口、通道和队列管理器名称)。请注意,示例是使用 MQ v8 Java 客户端编写的。

              Hashtable properties = new Hashtable<String, Object>();
              properties.put(MQConstants.HOST_NAME_PROPERTY, "qm.mycomp.com");
              properties.put(MQConstants.PORT_PROPERTY, 1414); 
              properties.put(MQConstants.CHANNEL_PROPERTY, "APP.SVRCONN"); 
              properties.put(MQConstants.USE_MQCSP_AUTHENTICATION_PROPERTY,"true");
              properties.put(MQConstants.USER_ID_PROPERTY, "myuserid");
              properties.put(MQConstants.PASSWORD_PROPERTY, "passw0rd");
    
              /**
               * Connect to a queue manager 
               */
              MQQueueManager queueManager = new MQQueueManager("QM", properties);
    

    更新

    所以您不想在程序中硬编码连接参数?您可以使用它自己的 MQSERVER 环境变量、获取它、解析它和连接参数。您还可以使用配置文件或 LDAP 服务器来拉取连接信息。

    更新二

    您根本没有阅读 MQ 文档。 MQ 客户端是一组库/jars/.net 程序集等,它们以不同的语言公开 API。您使用这些 API 开发应用程序以与队列管理器进行通信。这就是您在上面的程序中所做的。如果没有这些库,您将无法连接到队列管理器(许多人认为队列管理器就是服务器)。当您的应用程序与队列管理器在同一台机器上运行时,可以通过共享内存与队列管理器进行通信。但是在不同的机器上运行时,通信是通过 TCP/IP(或 SNA)进行的。

    希望这能消除混乱。

    【讨论】:

    • 是的先生..当队列管理器在同一台机器上运行时这将起作用......但实际上我的要求是通过客户端连接在服务器中运行的队列管理器而不指定代码中的IP地址 是否可以使用任何命令设置服务器的IP地址??
    • 不使用环境变量不...正如我在我的程序中提到的只传递队列管理器名称....我有一个 mqclient.ini 文件,其中包含TCP: Library1=DLLName1 KeepAlive=Yes ClntSndBuffSize=32768 ClntRcvBuffSize=32768 Connect_Timeout=0 CHANNELS: DefRecon=YES ServerConnectionParms=QM_ORANGE.QM_APPLE1/TCP/192.168.1.5(1414)
    • 如果没有主机名/IP 地址、端口和通道,应用程序无法连接到在不同机器上运行的队列管理器。
    • 仅使用名称,您只能连接到在同一台机器上运行的队列管理器。
    • @shashi- 好的,先生。那么MQ客户端有什么用??我之前已经通过端口和IP地址连接到服务器,没有使用客户端...
    猜你喜欢
    • 1970-01-01
    • 2015-01-22
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 1970-01-01
    相关资源
    最近更新 更多