简体   繁体   English

具有异步支持的基于连接池的Web服务器与基于事件循环的Web服务器

[英]Connection pool based web server with async support vs Event Loop based web server

I'm learning about Vertx and it's ecosysteme, firstly i learned about Event loop, and the concept is really nice to me. 我正在学习Vertx及其生态系统,首先我学习了事件循环,这个概念对我来说真的很不错。

But since Servlet 3.1 we can use async support in JAVA based servers. 但是从Servlet 3.1开始,我们可以在基于Java的服务器中使用异步支持。

I'm using Spring and it's new class named deferredresult which can take thread from tomcat, give execution of logic to thread from executor thread pool that make thread from tomcat free to handle another requests and then when it done return response. 我正在使用Spring,它是一个名为deferredresult的新类,该类可以从雄猫中获取线程,将逻辑执行到执行者线程池中,从而使雄猫中的线程可以自由处理其他请求,然后在完成响应后返回。

In event loop all blocking calls should be done by worker vertx, the concept is absolute the same, you give a thread to blocking call and provide callback when task is done event loop execute callback and return response. 在事件循环中,所有阻塞调用均应由工作程序vertx完成,其概念是绝对相同的,您为阻塞调用提供了一个线程,并在任务完成后提供回调。事件循环执行回调并返回响应。

These concepts looks really similar to me. 这些概念看起来与我非常相似。

Maybe i miss something but what is the difference between these concepts? 也许我错过了一些东西,但是这些概念之间有什么区别?

Thread pool based web servers use worker threads as the primary execution context for user requests. 基于线程池的Web服务器使用工作线程作为用户请求的主要执行上下文。 When you develop a Spring or JavaEE application, you call a lot of blocking code (JDBC, Hibernate, JAX-RS client, ...etc). 开发Spring或JavaEE应用程序时,您会调用很多阻塞代码(JDBC,Hibernate,JAX-RS客户端等)。 Then the servlet 3.1 async API was added to solve problems like long polling (if all your worker threads are waiting, you cannot handle requests any more). 然后添加了Servlet 3.1异步API,以解决长时间轮询之类的问题(如果所有工作线程都在等待,则您将无法再处理请求)。

Event loop based systems (Vert.x, Node) however are built to handle a lot of user requests with a single thread. 但是,基于事件循环的系统(Vert.x,Node)被构建为通过单个线程处理许多用户请求。 Usually you combine them with non blocking database drivers or web clients. 通常,您将它们与不阻塞的数据库驱动程序或Web客户端结合使用。 It's a very powerful model (less thread migrations, warm caches, ...etc). 这是一个非常强大的模型 (更少的线程迁移,热缓存等)。 But you must not block the event loop or you can't handle events any more. 但是,您不能阻止事件循环,否则不能再处理事件。 In an ideal world you would use only non blocking libraries but reality is many Java libraries are not and we shouldn't just throw this legacy away. 在理想的情况下,您将只使用非阻塞库,但现实是许多Java库都没有,我们不应该仅仅丢弃这种遗留物。 As a workaround, Vert.x provides a way to offload blocking code execution to a worker pool. 作为解决方法,Vert.x提供了一种将阻止代码执行的工作卸载到工作池的方法。

I hope this clarifies a bit and shows that, beyond the similarities, the use cases are different. 我希望这可以澄清一点,并表明除了相似之处之外,用例也有所不同。

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

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