title: Servlet 3 Async task on Tomcat 7
body: <p>I'm trying to implement Simple chat using Servlet 3.0 and Comet pattern based on its async support.</p>
  
  <p>I'm inspired by this article:
  <a href="http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=3" rel="noreferrer">http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=3</a></p>
  
  <p>My servlet looks like this.</p>
  
  <pre><code>@WebServlet(name="chatServlet", urlPatterns={"/ChatServlet"}, asyncSupported=true)
  public class ChatServlet extends HttpServlet {
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           AsyncContext aCtx = request.startAsync(request, response); 
           ServletContext appScope = request.getServletContext();    
           List&lt;AsyncContext&gt; watchers = (List&lt;AsyncContext&gt;) appScope.getAttribute("watchers");
           watchers.add(aCtx); //register the watcher
      }
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            AsyncContext aCtx = request.startAsync(request, response); 
            ServletContext appScope = request.getServletContext(); 
            Queue&lt;String&gt; messages = (Queue&lt;String&gt;)appScope.getAttribute("messages");
            messages.add(someMessage);
      }
  } 
  </code></pre>
  
  <p>now my Listener looks like this:</p>
  
  <pre><code>@WebListener
  public class ChatPushService implements ServletContextListener {
  
          @Override
          public void contextInitialized(ServletContextEvent sce) {
                final List&lt;AsyncContext&gt; watchers = new  ArrayList&lt;AsyncContext&gt;();
               sce.getServletContext().setAttribute("watchers", watchers);
                // store new messages not published yet
               Queue&lt;String&gt; messages = new ConcurrentLinkedQueue&lt;String&gt;();
               sce.getServletContext().setAttribute("messages", messages);
               Executor messageExecutor = Executors.newCachedThreadPool(); 
               final Executor watcherExecutor = Executors.newCachedThreadPool();
               while(true)
                {      
  
                   if(!messages.isEmpty()) 
                   {
                       System.out.println("notEmpty");
                      String message =  messages.poll(); 
                      messageExecutor.execute(new Runnable(){
  
                          @Override
                          public void run() {
                               for(final AsyncContext aCtx : watchers){
                                   watcherExecutor.execute(new Runnable(){
  
                                       @Override
                                          public void run() {
                                             try {
                                              aCtx.getResponse().getWriter().print("brrrrr");
                                          } catch (IOException e) {
                                              // TODO Auto-generated catch block
                                              e.printStackTrace();
                                          }
                                       }
                                   });
                               }
                          }
                   });
                }
          }
  
      }
      }
  </code></pre>
  
  <p>When I'm starting my it's freezing during container initiation.</p>
  
  <pre><code>Nov 1, 2011 1:12:09 AM org.apache.catalina.core.AprLifecycleListener init
  INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/lib/jvm/java-6-openjdk/jre/lib/amd64/server:/usr/lib/jvm/java-6-openjdk/jre/lib/amd64:/usr/lib/jvm/java-6-openjdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib/jni:/lib:/usr/lib
  Nov 1, 2011 1:12:09 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
  WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Servlet3Comet' did not find a matching property.
  Nov 1, 2011 1:12:09 AM org.apache.coyote.AbstractProtocol init
  INFO: Initializing ProtocolHandler ["http-bio-8080"]
  Nov 1, 2011 1:12:09 AM org.apache.coyote.AbstractProtocol init
  INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
  Nov 1, 2011 1:12:09 AM org.apache.catalina.startup.Catalina load
  INFO: Initialization processed in 624 ms
  Nov 1, 2011 1:12:09 AM org.apache.catalina.core.StandardService startInternal
  INFO: Starting service Catalina
  Nov 1, 2011 1:12:09 AM org.apache.catalina.core.StandardEngine startInternal
  INFO: Starting Servlet Engine: Apache Tomcat/7.0.22
  </code></pre>
  
  <p>It looks like <code>public void contextInitialized</code> function is not running asynchronously on background and is blocking further container initialization. </p>
  
  <p>Why? </p>
  
  <p>can anybody help me on this issue? </p>