users@jersey.java.net

[Jersey] Re: Is it possible to specify thread pool for AsyncInvoker?

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Fri, 19 Oct 2012 12:00:22 +0200

On Oct 16, 2012, at 8:29 PM, myxjtu_at_yahoo.com wrote:

> I am trying new features of Jersey 2.0, especially on async features.
> Here is a code snippet I wrote (I left out the irrelevant code) . The
> issues I face are:
> 1. when I send concurrent queries, I see huge number of threads
> created. For example, if I send 500 queries concurently, I see around
> 20k threads created. I do not understand why it happens. BTW, I use
> ThreadMXBean to monitor the number of threads.

Which version of Jersey you're using? Are all the threads relevant to the async invocation? How many of these created threads are actively running (a much more important number than the number of created threads)?

If possible, can you provide a reproducible test case that we could analyze?

>
> 2. for the "async()" call (a method of "AsyncInvoker"), is it possible
> to specify a thread pool (like using QUEUE_EXECUTOR in the following
> case)?

You can register a custom org.glassfish.jersey.spi.RequestExecutorsProvider in your client configuration:

client.configuration.register(new RequestExecutorsProvider() {
    public ExecutorService getRequestingExecutor() {
        ...
    }
});

Marek
>
> Thanks!
>
> public class JerseyServletAsync {
>
> private static final ExecutorService QUEUE_EXECUTOR = Executors
> .newCachedThreadPool(new
> ThreadFactoryBuilder().setNameFormat(
> "executor-%d").build());
>
> @GET
> @Produces(MediaType.APPLICATION_XML)
> public void getTaxonomy(@Suspended final AsyncResponse ar) {
>
> QUEUE_EXECUTOR.submit(new Runnable() {
> @Override public void run() {
> try {
>
> Client client = ClientFactory.newClient();
>
> client.target(UriBuilder.fromUri("http://mycom.com").path("/mypath").bu
> ild())
>
> .request(MediaType.APPLICATION_JSON).async().get(new
> InvocationCallback<String>() {
>
> @Override
> public void completed(String arg0) {
>
> ar.resume(targ0);
>
> }
>
> @Override
> public void failed(ClientException arg0) {
>
> ar.resume(arg0);
>
> }});
>
>
> }
> }
> }