8

Spawning lots of threads is never a good idea (and when you create too many, you may run out of memory anyway).

Usually, Jersey needs to create one thread per request. And this seems to be the case, whether I use async() (where Jersey creates the threads for me - I've investigated this in a debugger), or not (where I obviously have to create the threads myself).

So here's one concrete situation where this is not good enough:

I'm HTTP posting to remote servers, at a rate of up to 500 requests/second. But as the response may take some time to arrive (I calculate up to 30 seconds), the total number of threads can reach several thousands easily (at which point, a JVM process usually crashes). Moreover, it's just insane to create so many threads. It should actually be a piece of cake for the available processor/network/OS-resources to deal with that load.

So what I'd like to do, is to just fire off the requests - and be informed by the OS, when the HTTP response arrives.

  • As said above, simply using target.request(...).async()....doesn't do the trick (because then, Jersey just spawns its own threads).
  • Also, limiting the number of threads via new ClientConfig().property(ClientProperties.ASYNC_THREADPOOL_SIZE, 10) is not helpful at all, because it means, that at most 10 requests will be sent at a time, which is clearly not what I want (it would just pile-up the queue).

I experimented with new ClientConfig().connectorProvider(new GrizzlyConnectorProvider())to get NIO support - but didn't see any difference in behaviour at all.

So is there any way to fire off a request without having to create one extra thread per request?

Chris Lercher
  • 36,020
  • 19
  • 96
  • 128
  • I just found, that there is [jersey-non-blocking-client](http://search.maven.org/#artifactdetails%7Ccom.sun.jersey.contribs%7Cjersey-non-blocking-client%7C1.18.1%7Cjar), and a corresponding [blog post](https://blogs.oracle.com/PavelBucek/entry/jersey_non_blocking_client), where the author addresses the problem (though he seriously underestimates the impact of non-blocking vs blocking). However, unfortunately the project is for an old version of Jersey, which I cannot use. Maybe newer versions of Jersey already come with non-blocking support? And if yes, how to activate it? – Chris Lercher Oct 02 '14 at 11:40
  • The number of threads you can start is firstly a matter of heap space. There is nothing magic about 2048. – user207421 Oct 03 '14 at 00:38
  • @EJP: Thanks, I corrected the question. – Chris Lercher Oct 03 '14 at 12:45
  • 1
    @EJP: BTW, the 2048 limit I see seems to be enforced by Mac OS X: `sysctl kern.num_taskthreads` shows `kern.num_taskthreads: 2048` - and it seems, that the Oracle JVM actually uses kernel threads (interesting). – Chris Lercher Oct 03 '14 at 13:05

2 Answers2

5

I am using the CloseableHttpAsyncClient to make async requests to an external service. it works fine with few hundred requests per second, i haven't observed that number of threads like in your case. It's an external dependency which you can integrate through Maven via

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpasyncclient</artifactId>
  <version>4.0.1</version>
</dependency>

Hope this helps.

baris
  • 78
  • 1
  • 4
  • In the end, I went with Apache's HttpAsyncClient (I also tried AsyncHttpClient as suggested by @Alper, which also works great - just little differences). As far as I can see, the current versions of Jersey cannot deliver nonblocking behavior, because it is limited by JAX-RS 2.0 (a few [places](http://java.dzone.com/articles/whats-new-jax-rs-20) mention, that this might become possible with JAX-RS 3.0) – Chris Lercher Nov 04 '14 at 14:57
1

Make your requests using https://github.com/AsyncHttpClient/async-http-client, it uses netty. It'll fire it's calls off the request thread, and make a call back to your code, so it does not tie up the container request threads.

Alper Akture
  • 2,295
  • 1
  • 27
  • 43
  • (I deleted my previous comment, I had made a mistake) Yes, AsyncHttpClient does it right - it creates a worker thread pool, and can deal with much more active requests than workers. However, it would be quite a lot of effort to rework our application to use AsyncHttpClient, as we use a lot of Jersey features... wouldn't it be possible to achieve the same with Jersey? – Chris Lercher Oct 02 '14 at 10:03
  • We use Jersey for our service endpoints, and when we require a call to another service, we use AsyncHttpClient. If you are already using the jersey client, I guess that may not be super easy to change. You can use an ExecutorService and a future, to make the call off the calling thread and accomplish the same thing. I'll add another example. – Alper Akture Oct 03 '14 at 00:19
  • Using my own ThreadPool doesn't really solve the problem: If the ThreadPool is bounded, and I use sync calls to Jersey, then the callers to the executor will block. If I use async calls to Jersey, then Jersey unfortunately creates its own threads (not just workers, but one per open request)... (the only thing that could maybe work, would be to use sync calls, and then kill my thread - but that feels like a very bad idea.) – Chris Lercher Oct 03 '14 at 09:40
  • You're right, that was not going to solve anything, I removed that example. – Alper Akture Oct 03 '14 at 18:59
  • 1
    You've take a look at [this](https://jersey.java.net/documentation/latest/async.html) I guess as well, as a possibility? – Alper Akture Oct 03 '14 at 19:07
  • 1
    @AlperAkture's suggestion of using Jersey async with AsyncHttpClient is money. Exactly what I was looking for. – bluecollarcoder Jun 10 '15 at 18:18