java - 从 ibm mq 返回的响应有时与请求不匹配
问题描述
我复制了如何调用 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();
不会卡住吗?如果我错了,请纠正我
解决方案
Can you use different topic to differentiate?
That's a bad idea when you are doing point-to-point messaging.
destination = context.createQueue("queue:///" + responseQueue + "?targetClient=1");
It sounds like your responseQueue is shared between multiple consumers. You have 2 options:
- Create your own temporary dynamic queue and set it as the "Reply-To" queue
i.e.
Queue replyQ = session.createTemporaryQueue();
- Use MsgId / CorrelId request-reply messaging pattern.
i.e. Follow the suggestions on this page: Not able to get response from IBM MQ using JMS application
推荐阅读
- c# - 无法跟踪实体类型的实例
- python - Python - 从文件中绘制数据
- sql - 根据 XML 值拆分行
- google-apps-script - 功能 onEdit(e) - 解决方案与截止日期 - 一旦任务在截止日期前“完成”?
- flutter - Flutter:包含操作按钮的可选(丰富)文本
- ios - Swift - FetchRequest 自定义排序(函数)
- mongodb - 猫鼬 findOneandUpdate 数学运算
- android - 原因:java.lang.IllegalArgumentException:标签编号 6 与部署我自己的模型后轴 1 上的形状不匹配
- ethereum - 使用 OpenSea 的 SDK createSellOrder 方法预签名交易 + 待售清单
- c - 为什么我的二叉搜索树节点会中断?