I copy the java client code for how to call IBM MQ, and pass the request to the queue, but sometimes I getting the wrong response back from the queue.
For example, if I submit the following request:
F LOYFI6331760101046481882
I expect from the response I should get
F LOYFA36331760101046481882
But actually I getting
F LOYFA36331760101051292448
As you can see the card number is wrong.
Here is the code
import javax.jms.BytesMessage;
import javax.jms.Destination;
import javax.jms.JMSConsumer;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.TextMessage;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;
public class MQClient {
// System exit status value (assume unset value to be 1)
private static int status = 1;
public static byte[] sendAndReceive(String HOST, Integer PORT, String QMGR, String CHANNEL, String requestQueue, String responseQueue, String payload) {
// Variables
JMSContext context = null;
Destination destination = null;
JMSProducer producer = null;
JMSConsumer consumer = null;
BytesMessage receivedMessage = null;
byte[] result = null;
try {
// Create a connection factory
JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
JmsConnectionFactory cf = ff.createConnectionFactory();
// Set the properties
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, HOST);
cf.setIntProperty(WMQConstants.WMQ_PORT, PORT);
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, CHANNEL);
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, QMGR);
cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "JmsPutGet (JMS)");
cf.setStringProperty(WMQConstants.WMQ_TARGET_CLIENT, "1");
// Create JMS objects
context = cf.createContext();
destination = context.createQueue("queue:///" + requestQueue +"?targetClient=1");
TextMessage message = context.createTextMessage(payload);
producer = context.createProducer();
producer.send(destination, message);
System.out.println("Sent message:\n" + message);
destination = context.createQueue("queue:///" + responseQueue + "?targetClient=1");
consumer = context.createConsumer(destination); // autoclosable
receivedMessage= (BytesMessage)consumer.receive();
System.out.println("Receiving message:" + receivedMessage);
int text_length = new Long(receivedMessage.getBodyLength()).intValue();
result = new byte[text_length];
receivedMessage.readBytes(result, text_length);
System.out.println("\nReceived message:\n" + new String(result));
recordSuccess();
} catch (JMSException jmsex) {
recordFailure(jmsex);
}finally {
context.close();
}
return result;
}
}
I have another project to run concurrently to call MQClient.sendAndReceive()
method, with same host
, port
, QMGR
, channel
, requestQueue
and responseQueue
, only payload
is different.
So how do I fix the code above to make sure I always getting the correct response corresponding to the request?
EDIT:
1. For JoshMac questions, app means the IBM MQ one? Or the app that will call my sendAndReceive
function?
- Here is the flow I have, I using mule flow take the request from the POS, process the request, which need to call IBM MQ (which sit on AS400), to get the response back from MQ, and send back to POS. (In this example, I need to submit my request to
INQ1
and get the response fromINQR1
). Based on the answer below, it seems like thesendAndReceive
function is treat asRequester
, I need another flow to call theResponder
to handle the response, soreceivedMessage= (BytesMessage)consumer.receive();
will not getting stuck? Correct me if I am wrong