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

Again, strongly discourage the direct access to the server ThreadPool like that.
Either use the AsyncContext or your own Executor/ThreadPool.

Your implementation has none of the safety measures that are built into the AsyncContext layer's use of the Server ThreadPool.

Joakim Erdfelt / joakim@xxxxxxxxxxx

On Wed, Nov 25, 2015 at 9:54 AM, Joakim Erdfelt <joakim@xxxxxxxxxxx> wrote:

Joakim Erdfelt / joakim@xxxxxxxxxxx

On Wed, Nov 25, 2015 at 8:50 AM, Thomas Meyer <thomas@xxxxxxxx> wrote:
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
jetty-users mailing list
To change your delivery options, retrieve your password, or unsubscribe from this list, visit

Back to the top