Java Soap 如何支持异步调用而 Rest 不支持?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23857402/
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
How Soap supports asynchronous call while Rest does not?
提问by M Sach
I was going thru Soap vs Rest on net and found most of them says Soap supports asynchronous call while Rest does not but did not get any concrete example of that. Can anybody help me here?
我在网上浏览了 Soap 与 Rest,发现他们中的大多数人说 Soap 支持异步调用,而 Rest 不支持,但没有得到任何具体的例子。有人可以帮我吗?
Here is one of the resources i was referring to
这是我所指的资源之一
http://web.archive.org/web/20120421084456/http://www.prescod.net/rest/rest_vs_soap_overview/http://searchsoa.techtarget.com/tip/REST-vs-SOAP-How-to-choose-the-best-Web-servicehttp://seanmehan.globat.com/blog/2011/06/17/soap-vs-rest/
http://web.archive.org/web/20120421084456/http://www.prescod.net/rest/rest_vs_soap_overview/ http://searchsoa.techtarget.com/tip/REST-vs-SOAP-How-to-选择最佳网络服务http://seanmehan.globat.com/blog/2011/06/17/soap-vs-rest/
As per my understanding both should be synchronous. In both cases client makes a call to web service either thru soap or rest, client waits till response comes back from service. So how come soap supports asynchronous behaviour while rest does not?
根据我的理解,两者都应该是同步的。在这两种情况下,客户端通过soap 或rest 调用Web 服务,客户端等待直到响应从服务返回。那么为什么soap 支持异步行为而rest 不支持呢?
回答by Evandro Pomatti
SOAP defines a reply approach that allows for asynchronous computing, like a callback mechanism. You can achieve the same with REST but there is no specification for it, so you would have to build it yourself.
SOAP 定义了一种允许异步计算的回复方法,如回调机制。您可以使用 REST 实现相同的功能,但没有针对它的规范,因此您必须自己构建它。
Here is an example using JAX-WS 2.0demonstrating the feature.
下面是一个使用 JAX-WS 2.0演示该功能的示例。
I found useful information in Wikipedia WS-Addressing, which has a link for this W3C Specification.
我在Wikipedia WS-Addressing 中找到了有用的信息,其中有此W3C 规范的链接。
In the past I also developed in SAP ESB designer which allowed for asynchronous service interface methods. Although I never used that feature, that tool was fully compliant with SOAP specification and I am pretty sure it would work just like the Java example above, since the WSDL was used to generate JAX-WS server based. If I have time next week I will use that option to so see what happens and post it here.
过去我也在 SAP ESB 设计器中开发,它允许异步服务接口方法。尽管我从未使用过该功能,但该工具完全符合 SOAP 规范,而且我很确定它会像上面的 Java 示例一样工作,因为 WSDL 用于生成基于 JAX-WS 服务器的。如果我下周有时间,我将使用该选项来查看会发生什么并将其张贴在这里。
You should also check this answerwhich address pertinent aspects of this approach.
回答by user3958474
REST is purely an HTTP transport based call and you will receive a response say 200 OK on the other side,
REST 纯粹是一个基于 HTTP 传输的调用,你会在另一端收到一个 200 OK 的响应,
SOAP uses two varieties,
SOAP 使用两个品种,
- Synchronous Messaging over HTTP
- Asynchronous Messaging over HTTP
- 基于 HTTP 的同步消息传递
- 基于 HTTP 的异步消息传递
With synchronous messaging, the Requestor makes a request and the transport layer code blocks waiting for a response from the Provider. The Requestor receives the response on the same HTTP connection that the Requestor initially established to send the request. Synchronous exchange is usually easier to implement and requires that the Provider be able to generate a response in a short time, specifically in a time less than the HTTP timeout value(generally 120sec). [A single HTTP Connection is used that itself behaves Synchronously]
通过同步消息传递,请求者发出请求,传输层代码阻塞等待提供者的响应。请求者在请求者最初为发送请求而建立的同一 HTTP 连接上接收响应。同步交换通常更容易实现,并且需要 Provider 能够在短时间内生成响应,特别是在小于 HTTP 超时值(通常为 120 秒)的时间内。[使用单个 HTTP 连接,它本身的行为是同步的]
With asynchronous messaging, the Requestor is able to release transport specific resources once the request is acknowledged by the responder, knowing that a response will eventually be received. When the Provider completes the processing of the message it sends a response back to the Requestor over a new HTTP connection. [Here we utilize two HTTP Connections to implement an asynchronous messaging
通过异步消息传递,一旦请求被响应者确认,请求者就能够释放传输特定资源,知道最终会收到响应。当提供者完成对消息的处理后,它会通过新的 HTTP 连接将响应发送回请求者。【这里我们利用两个 HTTP Connections 来实现异步消息传递
- first HTTP Connection is used that for sending the Request and receiving an acknowledgement HTTP Response 200/OK
- second HTTP Connection is used for receiving the callback and responding HTTP Response 200/OK]
- 第一个 HTTP 连接用于发送请求和接收确认 HTTP 响应 200/OK
- 第二个HTTP Connection用于接收回调和响应HTTP Response 200/OK]
回答by Arijit
I have created a REST service which actually calls an async method to interact with DB layer and made our service asynchronous. I feel it is possible to implement asynchronous service in REST API. But in that case you need to have a call back service which will check whether the process has been completed in regular interval.
我创建了一个 REST 服务,它实际上调用了一个异步方法来与数据库层交互并使我们的服务异步。我觉得可以在 REST API 中实现异步服务。但是在这种情况下,您需要有一个回调服务,它会定期检查该过程是否已完成。