简体   繁体   English

了解非阻止Web服务调用与非阻止JDBC之间的区别

[英]Understanding the Difference Between Non-Blocking Web Service Calls vs Non-Blocking JDBC

I'm trying to understand conceptually why in Play Framework 2.0, it is considered a best practice to call WS.url().get() for web service calls, but if you wrap any other blocking call such as a JDBC call in a promise, you are advised to do it in an execution context other than the default execution context? 我试图从概念上理解为什么在Play Framework 2.0中,为Web服务调用调用WS.url().get()被认为是最佳实践,但是如果你在其中包含任何其他阻塞调用(如JDBC调用)承诺,建议您在默认执行上下文以外的执行上下文中执行此操作?

I understand that, by default, Play Framework's thread pools are configured so that you have one thread per core, and every controller expects to run completely non-blocking code. 据我所知,默认情况下,Play Framework的线程池配置为每个核心有一个线程,每个控制器都希望运行完全非阻塞的代码。 For this reason, if you make a blocking call in a controller -- say, to a web service --, then you want to make sure this call does not hold up the threads that are available for your controllers. 因此,如果您在控制器(例如,Web服务)中进行阻止调用,那么您需要确保此调用不会阻止可用于控制器的线程。

Otherwise, there will be no threads left to execute controllers because they're all waiting around in a blocking state. 否则,将不会有任何线程执行控制器,因为它们都处于阻塞状态。

But what confuses me are the following collection of points: 但令我困惑的是以下几点:

  • First, what thread pool does the controller code itself execute in? 首先,控制器代码本身执行什么线程池? Is this the default execution context? 这是默认的执行上下文吗?
  • Second, on the one hand I understand that WS.url().get() also executes in the default execution context, yet on the other hand, the Play Framework Documentation on Thread Pool Configuration states that "Note that you may be tempted to ... wrap your blocking code in Futures. This does not make it non blocking, it just means the blocking will happen in a different thread." 其次,一方面我理解WS.url().get()也在默认执行上下文中执行,另一方面, 线程池配置上Play Framework文档指出“请注意,你可能会想要...将你的阻止代码包装在Futures中。这不会使它成为非阻塞,只是意味着阻塞将在不同的线程中发生。“

Doesn't the above mean that WS.url().get() is "just happening in different thread" in the same default execution context? 上面的意思不是说WS.url().get()在同一个默认执行上下文中“只是在不同的线程中发生”吗? What is different about having a JDBC call execute in a different execution context? 在不同的执行上下文中执行JDBC调用有什么不同?

1) Play controller functions are executed within Play's default thread pool, as explained in the linked docs: 1)播放控制器功能在Play的默认线程池中执行,如链接文档中所述:

Play default thread pool - This is the default thread pool in which all application code in Play Framework is executed. 播放默认线程池 - 这是默认线程池,其中执行Play Framework中的所有应用程序代码。 It is an Akka dispatcher, and can be configured by configuring Akka, described below. 它是Akka调度程序,可以通过配置Akka进行配置,如下所述。 By default, it has one thread per processor. 默认情况下,每个处理器有一个线程。

Hence, you want to be very careful about blocking within controller functions, as that will block a thread in the default thread pool. 因此,您需要非常小心在控制器函数中进行阻塞,因为这会阻塞默认线程池中的线程。

2) Play web services is a non-blocking API, so it does not block it's ExecutionContext . 2)播放Web服务是一种非阻塞API,因此它不会阻止它的ExecutionContext Therefore, you can make several WS calls within controller functions, without blocking the default thread pool. 因此,您可以在控制器函数中进行多个WS调用,而不会阻塞默认线程池。 The main difference between a WS call and JDBC call is that a WS call will not block a thread while waiting for the response from the remote server, the calls are made asynchronously. WS调用和JDBC调用之间的主要区别在于WS调用在等待来自远程服务器的响应时不会阻塞线程,这些调用是异步调用的。 A JDBC call (like most java IO) will block on it's thread while waiting for a response. JDBC调用(像大多数java IO一样)会在等待响应时阻塞它的线程。

Executing a JDBC call within a different ExecutionContext will free up the default ExecutionContext to do other work, therefore allowing your server to process more requests. 在不同的ExecutionContext执行JDBC调用将释放默认的ExecutionContext以执行其他工作,从而允许您的服务器处理更多请求。 You can let Akka handle the hard work of context switching for you. 您可以让Akka为您处理上下文切换的艰苦工作。 While the JDBC calls are still blocking a thread, they're at least not blocking the threads that handle requests. 虽然JDBC调用仍然阻塞了一个线程,但它们至少不会阻塞处理请求的线程。

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

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