首页 > 解决方案 > 从 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,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();不会卡住吗?如果我错了,请纠正我

标签: javamultithreadingibm-mqmule4

解决方案


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


推荐阅读