java 如何发出 JMS 同步请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10778485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:32:11  来源:igfitidea点击:

How to make a JMS Synchronous request

javaarchitecturejmsibm-mq

提问by rk2010

I have an webapp that is expected to fetch and display data from an External App which is accessible only via messaging (JMS).

我有一个 webapp,它应该从一个只能通过消息传递 (JMS) 访问的外部应用程序中获取和显示数据。

So, if a user submits a request on a browser, the same HTTP request thread will have to interact with the Messaging system (MQ Series) such that the same request thread can display the data received from the Messaging System.

因此,如果用户在浏览器上提交请求,则同一个 HTTP 请求线程必须与消息系统(MQ 系列)交互,以便同一个请求线程可以显示从消息系统接收到的数据。

Is there a pattern I can make use of here? I saw some vague references on the net that use "Correlation ID" in this way:

有我可以在这里使用的模式吗?我在网上看到一些模糊的参考,以这种方式使用“相关 ID”:

Msg m = new TextMsg("findDataXYZ");
String cr_id = m.setCorrelationID(id);

sendQueue.send(m).

// now start listening to the Queue for a msg that bears that specific cr_id

Response r = receiverQueue.receive(cr_id);

Is there something better out there? The other patterns I found expect the response to be received asynchronously.. which is not an option for me, since I have to send the response back on the same HTTP request.

那里有更好的东西吗?我发现的其他模式期望异步接收响应......这对我来说不是一个选择,因为我必须在同一个 HTTP 请求上发送响应。

采纳答案by T.Rob

First, open the response queue. Then pass that object to the set reply-to method on the message. That way the service responding to your request knows where to send the reply. Typically the service will copy the message ID to the correlation ID field so when you send the message, take the message ID you get back and use thatto listen on the reply queue. Of course if you use a dynamic reply-to queue even that isn't neessary - just listen for the next message on the queue.

首先,打开响应队列。然后将该对象传递给消息上的 set 回复方法。这样,响应您请求的服务就知道将回复发送到哪里。通常,该服务会将消息 ID 复制到相关 ID 字段,因此当您发送消息时,获取返回的消息 ID 并使用来侦听回复队列。当然,如果您使用动态回复队列,即使这不是必需的 - 只需侦听队列中的下一条消息。

There's sample code that shows all of this. If you installed to the default location, the sample code lives at "C:\Program Files (x86)\IBM\WebSphere MQ\tools\jms\samples\simple\SimpleRequestor.java"on a Windows box or /var/mqm/toolsjms/samples/simple/SimpleRequestor.javaon a *nix box.

有示例代码显示了所有这些。如果您安装到默认位置,示例代码位于"C:\Program Files (x86)\IBM\WebSphere MQ\tools\jms\samples\simple\SimpleRequestor.java"Windows 机器或/var/mqm/toolsjms/samples/simple/SimpleRequestor.java*nix 机器上。

And on the off chance you are wondering "install what, exactly?" the WMQ client install is downloadable for free as SupportPac MQC71.

如果您想知道“安装什么,究竟是什么?” WMQ 客户端安装可作为SupportPac MQC71免费下载。

回答by Shashi

The request/reply messaging pattern is useful for your requirement. You typically use a CorrelationId to relate request & reply messages.

请求/回复消息模式对您的要求很有用。您通常使用 CorrelationId 来关联请求和回复消息。

While sending request message you set JMSReplyTo destination on the message. Typically a temporary queue is used as JMSReplyTo destination. When creating a consumer to receive response use a selector with JMSCorrelationId, something like

在发送请求消息时,您在消息上设置 JMSReplyTo 目标。通常,临时队列用作 JMSReplyTo 目标。创建消费者以接收响应时,使用带有 JMSCorrelationId 的选择器,例如

cons = session.createConsumer(tempDestination,"JMSCorrelationId="+requestMsg.JMSMessageId);

cons = session.createConsumer(tempDestination,"JMSCorrelationId="+requestMsg.JMSMessageId);

At the other end, the application that is processing the request message must use the JMSReplyTo destination to send response. It must also use the MessageId of the request message and set it as CorrelationId of the response message.

在另一端,正在处理请求消息的应用程序必须使用 JMSReplyTo 目标来发送响应。它还必须使用请求消息的 MessageId 并将其设置为响应消息的 CorrelationId。