开发者

What does it mean when a web service is asynchronous?

开发者 https://www.devze.com 2023-01-26 13:42 出处:网络
What does it mean when a web service is asynchronous?Is this only used when you call it with Ajax and you have a part on your开发者_StackOverflow社区 page that refreshes when the web service is done?T

What does it mean when a web service is asynchronous? Is this only used when you call it with Ajax and you have a part on your开发者_StackOverflow社区 page that refreshes when the web service is done? Thank you.


I know this is an old topic, but whether a web service is synchronous or asynchronous depends on the design of the web service and has nothing to do with Ajax. An asynchronous web service transaction proceeds like this:

  1. The client calls the web service. In the call the client sends a callback end point implemented as a service by the client.
  2. The web service returns a "message received" reply. ... (Some other processing occurs) ...
  3. The web service completes its task, then calls the callback endpoint provided by the client.
  4. The client callback replies with message received.

See Developing Asynchronous Web Services or How to: Create Asynchronous Web Service Methods


The question is whether it's the web service that's asynchronous, or your access to it. In the context of a web page, it's more likely that the service is synchronous, but that it is being accessed asynchronously.

Most likely, the service is being called via AJAX. The call is made to the service, and the page then continues. When the response comes in, either the success or the failure functions are executed, asynchronously.


Synchronous means that you call a web service (or function or whatever) and wait until it returns - all other code execution and user interaction is stopped until the call returns. Asynchronous means that you do not halt all other operations while waiting for the web service call to return. Other code executes and/or the user can continue to interact with the page (or program UI).

So, I would not say that the web service itself is asynchronous, I would say that your ajax call to the service is asynchronous.


When you call synchronous web service the service processes the request and return HTTP status code 200 OK (1) if everything went as expected, or error 4xx. The call is blocked while processing and the request and can take significant time.

When web service is asynchronous the main difference is that call should return instantly with HTTP 202 ACCEPTED (2) which means that request is taken in queue but not processed yet.

(1) 200 OK http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.1
(2) 202 ACCEPTED http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3


An asynchronous web service allows a client to submit a request, process the request and respond to the client after a given time -- the client would not block all activity on receiving a response.

Comparatively, a web service that is synchronous would provide a client directly with a response, expecting the client to block all activity until a response is returned. In this case the web service would limit the client to process requests one at a time.

0

精彩评论

暂无评论...
验证码 换一张
取 消