ServletContextListener:这不正确用法吗?

时间:2013-03-27 18:04:05

标签: java multithreading java-ee servlet-3.0

我正在阅读关于“Servlet 3.0中的异步处理支持”(http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.html?page=2)的专家(?)教程。其中包含以下代码段:

@WebServlet(name="myServlet", urlPatterns={"/slowprocess"}, asyncSupported=true)
public class MyServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        AsyncContext aCtx = request.startAsync(request, response);
        ServletContext appScope = request.getServletContext();
        ((Queue<AsyncContext>)appScope.getAttribute("slowWebServiceJobQueue")).add(aCtx);
    }
}

@WebServletContextListener
public class SlowWebService implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        Queue<AsyncContext> jobQueue = new ConcurrentLinkedQueue<AsyncContext>();
        sce.getServletContext().setAttribute("slowWebServiceJobQueue", jobQueue);
        // pool size matching Web services capacity
        Executor executor = Executors.newFixedThreadPool(10);
        while(true)
        {
            if(!jobQueue.isEmpty())
            {
                final AsyncContext aCtx = jobQueue.poll();
                executor.execute(new Runnable(){
                    public void run() {
                        ServletRequest request = aCtx.getRequest();
                        // get parameteres
                        // invoke a Web service endpoint
                        // set results
                        aCtx.forward("/result.jsp");
                    }
                });
            }
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
    }

}

因为,这是一个专家'这是怎么做'的文章我假设他们不会写一些愚蠢的代码。 但是我无法接受'contextInitialized()'中的'while(true)'循环。

  1. 根据我的理解,这个上下文监听器线程永远不会退出'contextInitialized()'方法。对吗?
  2. 我的应用程序中的其他Servlet会发生什么?他们可以免费提供服务吗?或者这是否会导致整个servlet容器崩溃?

1 个答案:

答案 0 :(得分:2)

1)我会将代码更改为:

@WebServletContextListener
public class SlowWebService implements ServletContextListener, Runnable {
    private volatile boolean running;
    ExecutorService executor;
    Thread runner;
    Queue<AsyncContext> jobQueue;

    public void contextInitialized(ServletContextEvent sce) {
        jobQueue = new ConcurrentLinkedQueue<AsyncContext>();
        sce.getServletContext().setAttribute("slowWebServiceJobQueue", jobQueue);
        // pool size matching Web services capacity
        executor = Executors.newFixedThreadPool(10);
        runner = new Thread(this);
        runner.start();
    }

    public void run() {
        running = true;
        while(running)
        {
            try {
                if(!jobQueue.isEmpty())
                {
                    final AsyncContext aCtx = jobQueue.poll();
                    executor.execute(new Runnable(){
                        public void run() {
                            ServletRequest request = aCtx.getRequest();
                            // get parameteres
                            // invoke a Web service endpoint
                            // set results
                            aCtx.forward("/result.jsp");
                        }
                    });
                }
            }
            catch (InterruptException e) {
            }
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
        running = false;
        runner.interrupt();
        executor.shutdown();
    }
}

2)只要具有不同的urlPattern,其他Servlet不应受到影响。