Session scoped dependencies and transferring request processing to async thread.

27 views
Skip to first unread message

Anuj Patel

unread,
Aug 2, 2016, 11:50:41 AM8/2/16
to google-guice
Hi All,

I am currently trying to *hack* an approach to use Aysnc Servlet support for Guice 4.1.0 app (along with guice-servlets extension, servlet spec 3.1.0).

Most of the things are working just fine but I run into a problem where Sesion scoped deps are not injected/Provider fails with null pointer because session is not available at all in filter's localContext is null since obviously the processing is in completely different scope.

I was wondering if anybody has faced this problem or may someone can provide any pointers in direction to fix this. 

Example pseudo code. (Can add more details/examples if required, but not sure if I can share actual snippets)


/** Service**/
package com.dummy.app.serviceA;

import com.google.inject.AbstractModule;

public class SubModule  extends AbstractModule {

    @Override
    protected void configure() {
        bind(SomeService.class).in(ServletScopes.SESSION);
    }
}


package com.dummy.web.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SomeServlet  extends AbstractModule {

  private final SomeService service;

 @Override
    protected final void doGet(final HttpServletRequest req, final HttpServletResponse resp)  throws ServletException,
                                                                                                                                                              IOException {
        // Put into context when context is initialized from GuiceServletContextListener.
        ExecutorService executor = (ExecutorService) req.getServletContext().getAttribute("ASYNC_SERV_EXECUTOR");
        final AsyncContext asyncContext = req.startAsync();
        executor.submit( () -> service.processRequest(req,resp))
    }
}

Best.
Reply all
Reply to author
Forward
0 new messages