简体   繁体   English

REST Web服务:同步还是异步?

[英]REST web services: synchronous or asynchrous?

What is the default behavior of REST web services - synchronous or asynchronous ? REST Web服务的默认行为是什么 - 同步还是异步
If it's synchronous then can we create asynchronous ? 如果它是同步的,那么我们可以创建异步吗?

"Synchronous" or "Asynchronous" is the behaviour of the client that is requesting the resource. “同步”或“异步”是请求资源的客户端行为 It has nothing to do with REST webservice, its structure, or the supporting server. 它与REST Web服务,其结构或支持服务器无关。

Synchronous behaviour: 同步行为:

  • Client constructs an HTTP structure, sends over the socket connection. 客户端构造HTTP结构,通过套接字连接发送。
  • Waits for the response HTTP. 等待响应HTTP。

Asychronous behaviour: 异步行为:

  • Client constructs HTTP structure, sends the request, and moves on. 客户端构造HTTP结构,发送请求,然后继续。
  • There's another thread that is waiting on the socket for the response. 还有另一个线程在套接字上等待响应。 Once response arrives, the original sender is notified (usually, using a callback like structure). 一旦响应到达,就会通知原始发件人(通常使用类似回调的结构)。

@Thrustmaster has explained it well. @Thrustmaster解释得很好。 I just wanted to add a point to make it sound simpler. 我只想添加一个点,使其听起来更简单。

REST web service is nothing but an HTTP call. REST Web服务只是一个HTTP调用。 You make a HTTP request to a URL and get a HTTP response back. 您向URL发出HTTP请求并返回HTTP响应。 How to handle the request and response is up to the caller. 如何处理请求和响应取决于调用者。

REST services has not nothing to do with being Synchronous or asynchronous. REST服务与同步或异步无关。

Client Side: Clients calling must support asynchronous to achieve it like AJAX in browser. 客户端:客户端调用必须支持异步才能像浏览器中的AJAX一样实现它。

Server Side: Multi- Thread environment / Non blocking IO are used to achieve asynchronous service. 服务器端:多线程环境/非阻塞IO用于实现异步服务。

I think this might be a good help for you to understand the RESTful web services in Java: 我认为这对您理解Java中的RESTful Web服务可能是一个很好的帮助:


You can control the client being synchronous or asynchronous from the client side. 您可以从客户端控制客户端同步异步 An example - using AJAX . 一个例子 - 使用AJAX

Yes you can have Asynchronous as well as Synchronous Web Service. 是的,您可以拥有异步和同步Web服务。 You can use any of the frameworks like Restlet, JAXB, JAX-RS. 您可以使用Restlet,JAXB,JAX-RS等任何框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM