如何使MQ Server回复消息与正确的请求匹配 [英] How to match MQ Server reply messages to the correct request

查看:232
本文介绍了如何使MQ Server回复消息与正确的请求匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在连接到IBM Websphere MQ.我希望能够将回复消息与正确的请求消息匹配.我已经浏览了数百页才能得到它,但是没有运气.

I'm connecting to an IBM Websphere MQ. I want to be able to match the reply message with the correct request message. I've trawled through hundreds of pages to get this and have had no luck.

我有一个类-MQHandler-该类将消息发送到一个已定义的队列,并从另一个队列读取请求.效果很好,但是,如果多个用户同时使用该应用程序,则会混淆消息.

I have a class - MQHandler - which sends a message to one defined queue, and reads the request from another. This works fine, however, if multiple users are using the application at the same time, messages get mixed up.

我似乎无法在接收器上得到一种方法来指示要匹配的CorrelationID. 像...

I can't seem to get a method on the receiver to indicate the CorrelationID to match. Something like...

consumer.receive(选择器);

您可以检查以下方法以确保我正确执行此操作吗?

Can you check the below methods to ensure I'm doing this correctly?

/**
 * When the class is called, this initialisation is done first.
 * 
 * @throws JMSException
 */
public void init() throws JMSException
{
    // Create a connection factory
    JmsFactoryFactory ff;
    try
    {
        ff = JmsFactoryFactory.getInstance( WMQConstants.WMQ_PROVIDER );
        cf = ff.createConnectionFactory();

        // Set the properties
        cf.setStringProperty( WMQConstants.WMQ_HOST_NAME, hostServer );
        cf.setIntProperty( WMQConstants.WMQ_PORT, 1414 );
        cf.setStringProperty( WMQConstants.WMQ_CHANNEL, channel );
        cf.setIntProperty( WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT );
        cf.setStringProperty( WMQConstants.WMQ_QUEUE_MANAGER, qManager );

        connection = cf.createConnection();

        session = connection.createSession( false, Session.AUTO_ACKNOWLEDGE );
    }
    catch( JMSException e )
    {
        throw e;
    }

} // end of init



/**
 * @param request
 * @return
 * @throws JMSException
 */
private String sendRequest( String request ) throws JMSException
{

    // Create JMS objects
    Destination destination = session.createQueue( "queue:///" + writeQueueName );

    // Enable write of MQMD fields. See documentation for further
    // details.
    ((JmsDestination) destination).setBooleanProperty( WMQConstants.WMQ_MQMD_WRITE_ENABLED, true );

    // Set message context, if needed. See comment at the top.

    // Create a producer
    MessageProducer producer = session.createProducer( destination );

    // Create a message
    TextMessage message = session.createTextMessage( request );

    // Generate a custom message id
    message.setJMSCorrelationID( generateRandomID() );

    // Start the connection
    connection.start();

    // And, send the message
    producer.send( message );
    System.out.println(message);

    return message.getJMSCorrelationID();
}


/**
 * @param customMessageId
 * @return
 * @throws JMSException
 */
private String recvResponse( String customMessageId ) throws JMSException
{
    Destination destination = session.createQueue( "queue:///" + readQueueName );

    // Enable read of MQMD fields.
    ((JmsDestination) destination).setBooleanProperty( WMQConstants.WMQ_MQMD_READ_ENABLED, true );
    ((JmsDestination) destination).setObjectProperty( WMQConstants.JMS_IBM_MQMD_CORRELID, customMessageId );

    // Create a consumer
    MessageConsumer consumer = session.createConsumer( destination );

    // Start the connection
    connection.start();

    // And, receive a message from the queue
    TextMessage receivedMessage = (TextMessage)consumer.receive( 15000 );

    connection.close();
    session.close();

    return receivedMessage.getText();
}

这是主要方法的摘要...

Here is a snippet of the main method...

    try
    {
        String customMessageId;
        init();
        customMessageId = sendRequest( request );
        return recvResponse( customMessageId );
    }
    catch( Exception ex )
    {
        System.out.println( "Error on MQ." );
        throw new Exception( "\n\n*** An error occurred ***\n\n" + ex.getLocalizedMessage()
                             + "\n\n**********************************" );
    }

推荐答案

QueueReceiver queueReceiver = 
    session.createReceiver(destination, "JMSCorrelationID='customMessageId'");

TextMessage receivedMessage = (TextMessage)queueReceiver.receive( 15000 );

在我的示例中,customMessageId应该包含您先前设置的实际值.

In my example, customMessageId should contain the actual value you have previously set.

此外,我已经看到许多情况,人们会生成一个correlationID,并将其设置在出站邮件中,希望能够根据该值选择响应.教科书这样做的方法是,服务提供商应用在响应时将消息ID复制到相关ID.请求者仍将指定JMSCorrelationID作为选择器,但将使用原始JMSMessageID作为值.由于JMSMessageID即使在QMgrs中也保证是唯一的,因此您在此值上发生冲突的可能性要小得多.您需要确保客户将服务值复制到相关ID中的行为与服务提供商的行为相匹配.

Also, I have seen many cases where people generate a correlationID and set it in the outbound message expecting to be able to select the response based on that value. The textbook way to do this is for the service provider app to copy the message ID to the correlation ID when responding. The requestor would still specify the JMSCorrelationID as the selector but would use the original JMSMessageID as the value. Since the JMSMessageID is guaranteed to be unique even across QMgrs, you are MUCH less likely to get collisions on this value. You will need to insure that your client matches the behavior of the service provider with respect to which value gets copied into the correlation ID.

这篇关于如何使MQ Server回复消息与正确的请求匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆