【发布时间】:2019-05-30 23:58:42
【问题描述】:
我复制了如何调用 IBM MQ 的 java 客户端代码,并将请求传递给队列,但有时我从队列中得到错误的响应。
例如,如果我提交以下请求:
F LOYFI6331760101046481882
我期望得到我应该得到的回应
F LOYFA36331760101046481882
但实际上我得到了
F LOYFA36331760101051292448
如您所见,卡号是错误的。
这里是代码
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;
}
}
我有另一个项目要同时运行以调用MQClient.sendAndReceive() 方法,具有相同的host、port、QMGR、channel、requestQueue 和responseQueue,只有payload 不同。
那么我该如何修复上面的代码,以确保我总是得到与请求对应的正确响应?
编辑:
1. 对于 JoshMac 的问题,app 是指 IBM MQ 吗?或者会调用我的sendAndReceive 函数的应用程序?
- 这是我的流程,我使用 mule 流程从 POS 获取请求,处理请求,这需要调用 IBM MQ(位于 AS400 上),从 MQ 获取响应,然后发送回POS。 (在这个例子中,我需要将我的请求提交给
INQ1并从INQR1获得响应)。根据下面的答案,似乎sendAndReceive函数被视为Requester,我需要另一个流程来调用Responder来处理响应,所以receivedMessage= (BytesMessage)consumer.receive();不会卡住?如果我错了,请纠正我
【问题讨论】:
-
你能用不同的主题来区分吗?
-
@yuanqingfei 可以举个例子吗?
-
这是一个消耗多个主题的示例:stackoverflow.com/questions/35715767/…希望对您有所帮助
-
响应的应用是否将关联ID设置为原始消息ID?如果是这样,您可以通过相关 ID 获取。
-
响应的应用是否将关联ID设置为原始消息ID?
标签: java multithreading ibm-mq mule4