i have a program to write 4 response to queue at a time but it is only writing the first response, I tried this code. But its only writing one response to the queue. Im using a ibm m queue , For getting all the request its fine . But when it comes to putting request into the queue the first response is only writing and the loop executes only one time . Dont know what is happening
MQQueueManager queueManager = null;
MQQueue queue = null;
try {
String queueManagerName = "TEST";`
String queueName = "REPONSE_QUEUE";
String port = "1452";
String host = "DESKTOP-32F2BVV";
String chanel = "NEW";
log.info("Queue Manager Name : " + queueManagerName);
log.info("Queue Name : " + queueName);
log.info("Port : " + port);
log.info("Host : " + host);
log.info("Chanel : " + chanel);
if (null != host && host != "" && host.trim().length() > 0) {
MQEnvironment.hostname = host;
}
if (null != port && port != "" && port.trim().length() > 0) {
MQEnvironment.port = Integer.parseInt(port);
}
if (null != chanel && chanel != "" && chanel.trim().length() > 0) {
MQEnvironment.channel = chanel;
}
queueManager = new MQQueueManager(queueManagerName.trim());
int option = MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING;
log.info("Open Option : " + option);
queue = queueManager.accessQueue(queueName.trim(), option, null, null, null);
for(int i=0;i<paymentResponse.size();i++) {
System.out.println(paymentResponse.size());
System.out.println(paymentResponse.get(i));
MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.options = MQConstants.MQPMO_ASYNC_RESPONSE;
MQMessage mqMessage = new MQMessage();
// mqMessage.messageId = messageId.getBytes();
mqMessage.format = MQC.MQFMT_STRING;
mqMessage.feedback = MQC.MQFB_NONE;
mqMessage.messageType = MQC.MQMT_DATAGRAM;
mqMessage.characterSet = 1208;
mqMessage.encoding = 546;
// mqMessage.replyToQueueName = replayQueue.trim();
// mqMessage.replyToQueueManagerName = replayQueueManager.trim();
// mqMessage.writeUTF(paymentRequest);
// mqMessage.writeString(paymentRequest);
byte[] bytes = paymentResponse.get(i).getBytes("UTF-8");
mqMessage.write(bytes, 0, bytes.length);
queue.put(mqMessage, pmo);
}
} catch (MQException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
return null;
}
You cutoff the beginning part of the method, so I'm going to guess that paymentResponse
is an ArrayList object.
But when it comes to putting request into the queue the first response is only writing and the loop executes only one time . Dont know what is happening
You should use the debugger that is in your IDE because without seeing the output, my first guess is that paymentResponse[1]
is NULL. Hence, it is jumping out of the loop and into the catch for Exception. Therefore, only 1 message is put to the queue.
You have several items that goes against coding best practises.
MQEnvironment
class as it is not thread safe.CCSID
and Encoding
rather than using the platform defaults. Is your data in a different format or encoding that is different from you local platform?MQMT_REPLY
.MQPMO_ASYNC_RESPONSE
? What is so important about your reply messages that the code cannot wait for MQ to complete?MQC
and MQConstants
Either use CMQC
class or MQConstants
class. Don't use MQC as it is depreciated.Here's how your code should be:
String qMgrName = "TEST";
String queueName = "REPONSE_QUEUE";
String port = "1452";
String host = "DESKTOP-32F2BVV";
String channel = "NEW";
String userId = "myUser";
String password = "myPwd";
MQQueueManager qMgr = null;
MQQueue queue = null;
Hashtable<String,Object> mqht = new Hashtable<String,Object>();
int option = CMQC.MQOO_OUTPUT + CMQC.MQOO_FAIL_IF_QUIESCING;
MQMessage mqMessage;
byte[] bytes = new byte[0];
if (null != host && host != "" && host.trim().length() > 0)
mqht.put(CMQC.HOST_NAME_PROPERTY, host.trim());
if (null != port && port != "" && port.trim().length() > 0) {
try {
mqht.put(CMQC.PORT_PROPERTY, new Integer(Integer.parseInt(port.trim())));
} catch (NumberFormatException e) {
mqht.put(CMQC.PORT_PROPERTY, new Integer(1414)); // default value
}
}
if (null != channel && channel != "" && channel.trim().length() > 0)
mqht.put(CMQC.CHANNEL_PROPERTY, channel.trim());
if (null != userId && userId != "" && userId.trim().length() > 0)
mqht.put(CMQC.USER_ID_PROPERTY, userId.trim());
if (null != password && password != "" && password.trim().length() > 0)
mqht.put(CMQC.PASSWORD_PROPERTY, password.trim());
try {
qMgr = new MQQueueManager(qMgrName.trim(), mqht);
queue = qMgr.accessQueue(queueName.trim(), option);
System.out.println(paymentResponse.size());
for(int i=0; i < paymentResponse.size(); i++) {
System.out.println(paymentResponse.get(i));
if (null != paymentResponse.get(i)) {
mqMessage = new MQMessage();
mqMessage.format = CMQC.MQFMT_STRING;
mqMessage.messageType = CMQC.MQMT_REPLY;
try {
bytes = paymentResponse.get(i).getBytes("UTF-8");
mqMessage.write(bytes, 0, bytes.length);
queue.put(mqMessage, new MQPutMessageOptions());
} catch (MQException e) {
System.out.println("CC=" +e.completionCode + " : RC=" + e.reasonCode);
} catch (IOException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
}
}
}
} catch (MQException e) {
System.out.println("CC=" +e.completionCode + " : RC=" + e.reasonCode);
} catch (Exception e) {
System.out.println(e);
}
finally
{
try {
if (queue != null) {
queue.close();
System.out.println("closed: "+ queueName);
}
} catch (MQException e) {
System.out.println("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
try {
if (qMgr != null) {
qMgr.disconnect();
System.out.println("disconnected from "+ qMgrName);
}
} catch (MQException e) {
System.out.println("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
}