users@jersey.java.net

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

From: <myxjtu_at_yahoo.com>
Date: Tue, 16 Oct 2012 18:29:26 +0000 (GMT)

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.

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)?

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);
                            
                        }});
       
 
                }
}
}