question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Guice can't create async servlet

See original GitHub issue

From swapii on August 30, 2011 07:39:51

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: http://code.google.com/p/google-guice/issues/detail?id=650_

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cstamascommented, Mar 29, 2018

I think this issue, at least it’s title is wrong: “Guice can’t create async servlet” is not true.

Guice can create async servlet (just don’t forget to set <async-supported>true</async-supported> on guice filter in web.xml).

Just FTR, as I got puzzled by this opened issue, while creating a POC that basically proved the opposite of this issue.

0reactions
ataiscommented, Mar 3, 2020

There is yet another way to register the async servlet in guice context. It works with constructor injection and no annotations.

// initialize all your beans, with servlets
val i = Guice.createInjector(new AbstractModule(){...})

// Guice context listener just needs to register the beans to servlet context
class AppServletContextListener extends GuiceServletContextListener {
  def getInjector: Injector = i.createChildInjector(new ServletRegistrator(i))
}

class ServletRegistration(i: Injector) extends ServletModule with ScalaServletModule {
  override def configureServlets(): Unit = {
    serve("/sync-home").`with`(classOf[SyncHomeServlet])

    // async
    val registration = getServletContext.addServlet(classOf[AsyncHomeServlet].getSimpleName, classOf[AsyncHomeServlet])
    registration.setAsyncSupported(true)
    registration.addMapping("/async-home")
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Guice 3.0 with Jersey and Async Servlets - java - Stack Overflow
Trying to build an application that uses @Suspended and AsyncResponse but cannot get it to work, my app works fine without the use...
Read more >
Set "async-supported" with guice-servlet? - Google Groups
Hello. How i can set "async-supported" property with guice-servlet ? In web.xml i do this: <servlet> <async-supported>true</async-supported> .. </servlet>
Read more >
Creating asynchronous servlets with Tomcat 7 (Servlet 3.0 API)
Client request comes in via HTTP and is dispatched to some servlet. · The servlet's service() method is executed in some thread provided...
Read more >
Async Servlets - Jayway
In many cases just a few threads can handle lots of requests simultaneously, for example if an asynchronous HTTP client is used to...
Read more >
Async Servlet Feature of Servlet 3 - Java Code Geeks - 2022
The problem with container specific solution is that we can't move to other servlet container without changing our application code, that's why ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found