简体   繁体   English

Java非阻塞HTTP服务器

[英]Java Non-Blocking HTTP Server

I have written an application using embedded Jetty that makes network calls to other services. 我使用嵌入式Jetty编写了一个应用程序,可以对其他服务进行网络调用。

I presume that the serving threads are idle whilst waiting for the network calls to complete. 我假设服务线程在等待网络呼叫完成时处于空闲状态。

Is there any way to have a worker thread that switches between requests to perform work that can be done at the current time and then when the network calls return also handle that? 有没有办法让一个工作线程在请求​​之间切换,以执行可以在当前时间完成的工作,然后在网络调用返回时也处理它? A request would be returned when all work has been completed for it. 完成所有工作后将返回请求。

I know this is a common paradigm, and I have used it for non-blocking TCP networking, but I'm unsure as to how to achieve this on a Java HTTP server whilst also waiting on external results. 我知道这是一个常见的范例,我已经将它用于非阻塞TCP网络,但我不确定如何在Java HTTP服务器上实现这一点,同时还要等待外部结果。

Any links or explanations are appreciated. 任何链接或解释都表示赞赏。

Thanks 谢谢

Update: I'm using Membase and ElasticSearch (the only network calls). 更新:我正在使用Membase和ElasticSearch(唯一的网络调用)。 Membase returns "Future" objects and ElasticSearch returns "ListenableActionFuture". Membase返回“Future”对象,ElasticSearch返回“ListenableActionFuture”。 I'd like to be able to continue processing on a thread in response to these objects being returned. 我希望能够继续处理线程以响应返回的这些对象。

您可以查看Deft ,它是单线程,异步,事件驱动的Web服务器。

Netty is a java library that allows you to do asynchronous networking. Netty是一个允许您进行异步网络的Java库。

http://www.jboss.org/netty http://www.jboss.org/netty

Netty supports http, but it is a fairly low level library. Netty支持http,但它是一个相当低级别的库。

A higher level library is finangle by twitter, 更高级别的图书馆是twitter的finangle,

http://twitter.github.com/finagle/ http://twitter.github.com/finagle/

Finangle is built on top of netty, but supports connection pooling, load balancing, and has a lot of other features. Finangle建立在netty之上,但支持连接池,负载平衡,并具有许多其他功能。 Finangle supports http. Finangle支持http。

If you want to do work at the same time as IO, I suggest you add a thread pool to perform the work. 如果您想在IO的同时进行工作,我建议您添加一个线程池来执行工作。 It is possible to re-use the existing threads but its a lot of extra work for possibly too little benefit. 可以重复使用现有的线程,但是它可能带来很多额外的工作,但可能收益太少。

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

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