简体   繁体   中英

Java Non-Blocking HTTP Server

I have written an application using embedded Jetty that makes network calls to other services.

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.

Any links or explanations are appreciated.

Thanks

Update: I'm using Membase and ElasticSearch (the only network calls). Membase returns "Future" objects and ElasticSearch returns "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.

http://www.jboss.org/netty

Netty supports http, but it is a fairly low level library.

A higher level library is finangle by twitter,

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 supports http.

If you want to do work at the same time as IO, I suggest you add a thread pool to perform the work. It is possible to re-use the existing threads but its a lot of extra work for possibly too little benefit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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