xiaodududu / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

Guice can't create async servlet #650

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I've connect Guice to my project, write web.xml and everything gone right until 
I wan't to realize async servlet. I using Tomcat 7.0.20 and Guice 3.0. Servlets 
spec version is 3.0.

Test Comet servlet:

@Singleton
@WebServlet(name = "comet", urlPatterns = {"/comet"}, asyncSupported = true)
public class CometServlet extends HttpServlet {

    private class TestThread implements Runnable {

        private AsyncContext context;

        public TestThread(AsyncContext ctx) {
            context = ctx;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(5000);
                context.getResponse().getWriter().write("TEST!");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AsyncContext ctx = req.startAsync(req, resp);
        new Thread(new TestThread(ctx)).start();
    }

}

Servlet created, it placed at "/comet" path, but async not working and error 
was printed:

HTTP Status 500

java.lang.IllegalStateException: Not supported.
    org.apache.catalina.connector.Request.startAsync(Request.java:1638)
    org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1031)
    javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:379)
    javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:379)
    ru.unimobi.cinema.server.server.servlets.CometServlet.service(CometServlet.java:46)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    com.google.inject.servlet.ServletDefinition.doService(ServletDefinition.java:216)
    com.google.inject.servlet.ServletDefinition.service(ServletDefinition.java:141)
    com.google.inject.servlet.ManagedServletPipeline.service(ManagedServletPipeline.java:93)
    com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:63)
    com.google.inject.servlet.FilterDefinition.doFilter(FilterDefinition.java:134)
    com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:59)
    com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:122)
    com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:110)

I think this is bug, because I write web.xml directly without Guice and it 
working as expected.

Original issue reported on code.google.com by swa...@gmail.com on 30 Aug 2011 at 11:39

GoogleCodeExporter commented 9 years ago
Could you please suggest a workaround for this.

Original comment by igor.pet...@gmail.com on 20 Jan 2012 at 8:55

GoogleCodeExporter commented 9 years ago
Check out this Issue 
http://code.google.com/p/google-guice/issues/detail?id=678. You can use the 
class I have provided in the patch. It allows to inject Guice managed objects 
to servlets managed my the container. So you can use your class without 
@Singleton, just as a regular servlet mapped with annotation. Make sure you 
have asyncSupport=true in all filters in the path to your servlet

Original comment by igor.pet...@gmail.com on 24 Jan 2012 at 9:00