Servlet 3 Async task on Tomcat 7

前端 未结 2 1054
别那么骄傲
别那么骄傲 2021-01-31 06:23

I\'m trying to implement Simple chat using Servlet 3.0 and Comet pattern based on its async support.

I\'m inspired by this article: http://www.javaworld.com/javaworld/jw

相关标签:
2条回答
  • 2021-01-31 06:48

    I can't comment on Ramesh's code, so I have to place it here... Since no thread wraps round the ChatManager runnable, I believe you should call run() on it and not start(). Also, quite obvious though, it should be new ChatManager()..not new chatManager()...account of Java been case-sensitive.

    0 讨论(0)
  • 2021-01-31 06:49

    You are running while loop inside contextInitialized() method which is wrong. contextInitialized() is invoked by Servlet Container as part of the application startup, having while loop there will block your app start.

    Modified the code such the ContextListener will start one daemon thread which publishes the messages to the watchers

    @WebListener
    public class ChatPushService implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
              final List<AsyncContext> watchers = new  ArrayList<AsyncContext>();
             sce.getServletContext().setAttribute("watchers", watchers);
              // store new messages not published yet
             Queue<String> messages = new ConcurrentLinkedQueue<String>();
             sce.getServletContext().setAttribute("messages", messages);
             new chatManager(sce.getServletContext()).start(); //START DAEMON
    
          }
    }
    public class ChatManager implements Runnable
    {
    ServletContext servletCtx;
    public ChatManager(ServletContext ctx)
    {
         this.servletCtx = ctx;
    }
    public void run()
    {
             List<AsyncContext> watchers = (List<AsyncContext>) servletCtx.getAttribute("watchers");
         Queue<String> messages = (Queue<String>)appScope.getAttribute("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();
                                        }
                                     }
                                 });
                             }
                        }
                 });
              }
        }
    
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题