Jersey non blocking client

Although Jersey already have support for making asynchronous requests, it is implemented by standard blocking way – every asynchronous request is handled by one thread and that thread is released only after request is completely processed. That is OK for lots of cases, but imagine how that will work when you need to do lots of parallel requests. Of course you can limit (and its really wise thing to do, you do want control your resources) number of threads used for asynchronous requests, but you’ll get another maybe not pleasant consequence – obviously processing time will incerase.

There are few projects which are trying to deal with that problem, commonly named as async http clients. I didn’t want to “re-implement a wheel” and I decided I’ll use AHC – Async Http Client made by Jeanfrancois Arcand. There is also interesting implementation from Apache – HttpAsyncClient, but it is still in “very early stages of development” and others haven’t been in similar or better shape as AHC.

How this works? Non-blocking clients allow users to make same asynchronous requests as we can do with standard approach but implementation is different – threads are better utilized, they don’t spend most of time in idle state. Simply described – when you make a request (send it over the network), you are waiting for reply from other side. And there comes main advantage of non-blocking approach – it uses these threads for further work, like making other requests or processing responses etc.. Idle time is minimized and your resources (threads) will be far better used.

Who should consider using this? Everyone who is making lots of asynchronous requests. I haven’t done proper benchmark yet, but some simple dumb tests are showing huge improvement in cases where lots of concurrent asynchronous requests are made in short period.

Last but not least – this module is still experimental, so if you don’t like something or if you have ideas for improvements/any feedback, feel free to comment this blog post, send mail to or contact me personally. All feedback is greatly appreciated!

maven dependency (will be present in maven 2 repo by the end of the day):



code snippet:

 ClientConfig cc = new DefaultNonBlockingClientConfig();
 cc.getProperties().put(NonBlockingClientConfig.PROPERTY_THREADPOOL_SIZE, 10); // default value, feel free to change
 Client c = NonBlockingClient.create(cc);

 AsyncWebResource awr = c.asyncResource("");

 Future<ClientResponse> responseFuture = awr.get(ClientResponse.class);

 // or
 awr.get(new TypeListener<ClientResponse>(ClientResponse.class) {
     public void onComplete(Future<ClientResponse> f) throws InterruptedException {

javadoc (temporary location, won’t be updated):

This entry was posted in java and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>