Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Get server threadpool from Jersey @GET resourse

Am 25.11.2015 4:26 nachm. schrieb Joakim Erdfelt <joakim@xxxxxxxxxxx>:
> Manage your own ThreadPool, or use the Servlet AsyncContext features for async processing of individual requests. 


Sadly in JAX-RS you don't have access to AsyncContext, here is my solution. I had to use reflection because of classloader issues...

public class TestResource { 

        private Logger log; 

        public TestResource() { 
                log = Logger.getLogger(TestResource.class.getName()); 

        public void getResourceData(@Suspended AsyncResponse ar, @Context ServletContext sc) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { 
                log.log(Level.INFO, "In thread {0}", Thread.currentThread()); 

                Method methodGetContextHandler = sc.getClass().getMethod("getContextHandler"); 
                Object contextHandler = methodGetContextHandler.invoke(sc, null); 
                Method methodGetServer = contextHandler.getClass().getMethod("getServer"); 
                Object server = methodGetServer.invoke(contextHandler, null); 
                Method methodGetThreadpool = server.getClass().getMethod("getThreadPool"); 
                Executor threadPool = (Executor) methodGetThreadpool.invoke(server, null); 

                Runnable run = new Runnable() { 
                        public void run() { 
                                Logger log = Logger.getAnonymousLogger(); 
                                log.log(Level.INFO, "In thread {0}", Thread.currentThread()); 
                                ar.resume("it works!"); 

The JPA context has a warp() function, it would be cool if ServletContext could have something similar: sc.wrap(Server.class) to get the underlying server class.

> Joakim Erdfelt / joakim@xxxxxxxxxxx

Back to the top