如何从IBM MessageQueue或远程MQ

时间:2016-05-04 06:58:10

标签: queue message-queue ibm-mq

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

下面是我用来从MQ获取PUT / GET消息的代码。请告诉我如何从输出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);

1 个答案:

答案 0 :(得分: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();
          }
    }               
}