gpt4 book ai didi

queue - 如何将 MQ Server 回复消息与正确的请求相匹配

转载 作者:行者123 更新时间:2023-12-02 04:35:23 25 4
gpt4 key购买 nike

我正在连接到 IBM Websphere MQ。我希望能够将回复消息与正确的请求消息相匹配。我翻阅了数百页才找到这个,但没有成功。

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

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

consumer.receive(选择器);

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

/**
* 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();
}

这是主要方法的片段...

    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 应包含您之前设置的实际值。

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

关于queue - 如何将 MQ Server 回复消息与正确的请求相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5021283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com