Ibm mq 如何使用MQ客户端连接到MQ服务器队列管理器

Ibm mq 如何使用MQ客户端连接到MQ服务器队列管理器,ibm-mq,mq,Ibm Mq,Mq,我尝试使用MQ客户端向MQ服务器发送消息 错误是:-发生WebSphere MQ错误:完成代码2原因代码2058。我知道此原因代码是因为队列管理器名称错误。。但是队列管理器名称是正确的 安装WebSphere MQ客户端后,我只需运行以下命令: 设置MQSERVER=QM\U ORANGE/TCP/IPADDRESS(端口号) 然后运行这个程序 公共类MQSample{ // code identifier static final String sccsid = "@(#) MQMBI

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

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

安装WebSphere MQ客户端后,我只需运行以下命令: 设置MQSERVER=QM\U 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;
  }
}

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

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

您可以执行以下操作来连接到队列管理器:(更改主机、端口、通道和队列管理器名称)

          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);
Hashtable properties=newhashtable();
properties.put(MQConstants.HOST_NAME_PROPERTY,“qm.mycop.com”);
properties.put(MQConstants.PORT_PROPERTY,1414);
properties.put(MQConstants.CHANNEL_属性,“APP.SVRCONN”);
properties.put(MQConstants.USE_MQCSP_AUTHENTICATION_PROPERTY,“true”);
properties.put(MQConstants.USER_ID_PROPERTY,“myuserid”);
properties.put(MQConstants.PASSWORD_属性,“passw0rd”);
/**
*连接到队列管理器
*/
MQQueueManager队列管理器=新MQQueueManager(“QM”,属性);
更新

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

更新II

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


希望这能消除混淆。

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

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

您可以执行以下操作来连接到队列管理器:(更改主机、端口、通道和队列管理器名称)

          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);
Hashtable properties=newhashtable();
properties.put(MQConstants.HOST_NAME_PROPERTY,“qm.mycop.com”);
properties.put(MQConstants.PORT_PROPERTY,1414);
properties.put(MQConstants.CHANNEL_属性,“APP.SVRCONN”);
properties.put(MQConstants.USE_MQCSP_AUTHENTICATION_PROPERTY,“true”);
properties.put(MQConstants.USER_ID_PROPERTY,“myuserid”);
properties.put(MQConstants.PASSWORD_属性,“passw0rd”);
/**
*连接到队列管理器
*/
MQQueueManager队列管理器=新MQQueueManager(“QM”,属性);
更新

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

更新II

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


希望这能消除混淆。

是的,先生..当队列管理器在同一台机器上运行时,这将起作用…但实际上,我的要求是通过客户端连接服务器上运行的队列管理器,而不在代码中指定IP地址。是否可以使用任何命令设置服务器的IP地址?否,而不使用t环境变量…正如我在程序中提到的那样,只传递队列管理器名称…我有一个mqclient.ini文件,其中包含
TCP:Library1=DLLName1 KeepAlive=Yes ClntSndBuffSize=32768 ClntRcvBuffSize=32768 Connect\u Timeout=0通道:defreco=yesserverconnectionparms=QM\u ORANGE.QM\u APPLE1/TCP/192.168.1.5(1414)
如果没有主机名/IP地址、端口和通道,应用程序无法连接到运行在不同机器上的队列管理器。只有名称,您只能连接到运行在同一机器上的队列管理器。@shashi-好的,先生。那么MQ客户机有什么用?我已经