0

我复制了如何调用 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,requestQueueresponseQueue, 只是payload不同。

那么如何修复上面的代码以确保我始终获得与请求对应的正确响应?

编辑: 1. 对于 JoshMac 问题,app 是指 IBM MQ 吗?或者将调用我的sendAndReceive函数的应用程序?

  1. 这是我的流程,我使用 mule 流程从 POS 获取请求,处理需要调用 IBM MQ(位于 AS400 上)的请求,从 MQ 获取响应,然后发送回 POS。(在这个例子中,我需要将我的请求提交到INQ1并从中获取响应INQR1)。根据下面的答案,似乎该sendAndReceive函数被视为Requester,我需要另一个流程来调用Responder来处理响应,所以receivedMessage= (BytesMessage)consumer.receive();不会卡住吗?如果我错了,请纠正我
4

2 回答 2

0

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:

  1. Create your own temporary dynamic queue and set it as the "Reply-To" queue

i.e.

Queue replyQ = session.createTemporaryQueue();
  1. 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

于 2019-05-28T16:47:45.683 回答
0

看起来所有响应都被发送到 INQR1。为了帮助您的应用程序识别哪些响应是针对它的以及哪些可以忽略,您可以使用选择器,通常选择器是通过相关 id 进行的,尽管您可以使用其他字段。

本质上,您正在使用有 JMS 示例的请求/响应模式 - https://github.com/ibm-messaging/mq-dev-patterns/tree/master/JMS

您的请求者的逻辑如下:

String correlationID = String.format("%24.24s", UUID.randomUUID().toString());
message.setJMSCorrelationIDAsBytes(b);

生成关联 ID,并将其添加到消息中。

然后,您的请求者将创建一个基于相关 id 的选择器来过滤响应队列:

try {
      b = correlationID.getBytes();
      selector = "JMSCorrelationID='ID:" + getHexString(b) + "'";
    } catch (Exception e) {
       ...
    }

用于构建消息使用者:

JMSConsumer consumer = context.createConsumer(requestQueue, selector);
Message receivedMessage = consumer.receive();

当您的响应者收到初始请求时,它可以获取相关 ID:

String correlationID = receivedMessage.getJMSCorrelationID();

并使用它来设置响应的相关 ID:

message.setJMSCorrelationID(correlationID);
于 2019-05-29T14:28:35.527 回答