【问题标题】:How to read message from a IBM MessageQueue or remote MQ如何从 IBM MessageQueue 或远程 MQ 读取消息
【发布时间】:2016-08-29 11:20:06
【问题描述】:

目前我正在使用 JAVA Vuser 协议在负载运行器中编写 MQ 脚本。我正在使用一个输入队列和一个输出队列。我能够成功地使用输入队列放置消息,但我无法从输出队列中读取消息。

下面是我用来从 MQ 发送/获取消息的代码。请让我知道如何从输出 MQ 读取消息。

lr.start_transaction("test_message");

try {
      MQQueue destQueue1 = queueMgr.accessQueue(putQueueName, MQC.MQOO_INQUIRE);
              pmo.options = MQC.MQPMO_NEW_MSG_ID; 
              requestMsg.replyToQueueName =getQueueName; 
              requestMsg.report=MQC.MQRO_PASS_MSG_ID; 
              requestMsg.format = MQC.MQFMT_STRING; 
              requestMsg.messageType=MQC.MQMT_REQUEST;
              requestMsg.writeString(msgBody);
              putQueue.put(requestMsg, pmo);
             } catch(Exception e) {
            lr.error_message("Error sending message.");
            lr.exit(lr.EXIT_VUSER, lr.FAIL);
            }
            putQueue.close();

         // Get the response message object from the response queue
        try {
             responseMsg.correlationId = requestMsg.messageId; 
             gmo.matchOptions=MQC.MQMO_MATCH_CORREL_ID; 
             gmo.options= MQC.MQGMO_NO_SYNCPOINT; 
             gmo.matchOptions=MQC.MQMO_NONE; 
             gmo.options= MQC.MQGMO_SYNCPOINT; 
             gmo.options= MQC.MQGMO_CONVERT; 
             gmo.options= MQC.MQGMO_WAIT;
             gmo.waitInterval=MQC.MQWI_UNLIMITED; 
             gmo.waitInterval=60000;
             getQueue.get(responseMsg, gmo);
            System.out.println("QueueDepth for get:"+getQueue.getCurrentDepth());
             //Check the message content
     byte[] responseMsgData = responseMsg.readStringOfByteLength(responseMsg.getTotalMessageLength()).getBytes();
            String msg = new String(responseMsgData);
            lr.output_message(msg); 
            } catch(Exception e) {
            lr.error_message("Error receiving message.");
            lr.exit(lr.EXIT_VUSER, lr.FAIL);
            }
       lr.end_transaction("test_message", lr.AUTO);

【问题讨论】:

    标签: queue message-queue ibm-mq


    【解决方案1】:

    您似乎是 MQ 的新手。您的代码中有多个问题。这是一段演示 MQ 请求/响应场景的代码。代码使用 MQ v8 开发。根据你的MQ版本和需要进行修改。

    /**
     * Reqeust reply scenario
     */
    public void mqRequestRespose() {
          Hashtable<String, Object> properties;
    
          try {
              System.out.println("***Request/Reply Started ***  ");
              properties = new Hashtable<String, Object>();
              properties.put("hostname", "localhost");
              properties.put("port", new Integer(1414));
              properties.put("channel", "APP.SVRCONN.CHN");
              properties.put(MQConstants.USE_MQCSP_AUTHENTICATION_PROPERTY,"true");
              properties.put(MQConstants.USER_ID_PROPERTY, "username");
              properties.put(MQConstants.PASSWORD_PROPERTY, "password");
    
              /**
               * Connect to a queue manager 
               */
              MQQueueManager queueManager = new MQQueueManager("APPQMGR", properties);
    
              /**
               * Now create a subscription by providing our own temporary queue
               */
              MQQueue mqRequestQ = queueManager.accessQueue("REQUEST.QUEUE", CMQC.MQOO_FAIL_IF_QUIESCING | CMQC.MQOO_OUTPUT );
              MQQueue mqReplyQ =  queueManager.accessQueue("REPLY.QUEUE", CMQC.MQOO_FAIL_IF_QUIESCING | CMQC.MQOO_INPUT_AS_Q_DEF);
    
              /**
               * Build a request message and send it to request queue.
               */
              System.out.println("***Sending a request ***");                 
              MQMessage msgRequest = new MQMessage();
              msgRequest.writeUTF("Give me quote for IBM");
              mqRequestQ.put(msgRequest);
    
              /**
               * Wait for 30 seconds to receive reply from reply queue
               */
              System.out.println("*** Waiting for reply ***");                
              MQGetMessageOptions mqgmo = new MQGetMessageOptions();
              mqgmo.options = CMQC.MQGMO_WAIT | CMQC.MQGMO_CONVERT;
              mqgmo.waitInterval = 30000;
              mqgmo.matchOptions=CMQC.MQMO_MATCH_CORREL_ID;
    
              MQMessage msgReply = new MQMessage();
              msgReply.correlationId = msgRequest.messageId;
              try {
                  mqReplyQ.get(msgReply, mqgmo);                  
                  System.out.println("***Reply received***");
                  System.out.println("STOCK QUOTE: USD" + msgReply.readUTF());
              }catch (MQException mqex) {
                  System.out.println("***No reply received in given time***");                
              }
            } catch (Exception e) {
                System.err.println(e);
                e.printStackTrace();
                for (Throwable t = e.getCause(); t != null; t = t.getCause()) {
                    System.out.println("... Caused by ");
                    t.printStackTrace();
              }
        }               
    }
    

    【讨论】:

    • 您创建了属性 Hashtable,但实际上并没有在任何地方使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    相关资源
    最近更新 更多