Reputation: 155
Currently I'm doing MQ scripting in load runner using JAVA Vuser protocol.I'm using one Input queue and one Output queue. I'm able to put the message using Input queue successfully, But I'm unable to read the message from the output queue.
Below is the code I'm using to PUT/GET message from MQ. Kindly Let me know how to read message from output 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);
Upvotes: 2
Views: 2060
Reputation: 15263
You seem to be new to MQ. There are multiple problems in your code. Here is a piece of code demonstrating MQ Request/Response scenario. Code is developed using MQ v8. Modify it according to your MQ version and need.
/**
* 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();
}
}
}
Upvotes: 1