/**
   * Context destroyed.
   *
   * @param sce the sce
   */
  @Override
  public void contextDestroyed(ServletContextEvent sce) {

    ConnectionPool connectionPool;
    connectionPool = ConnectionPool.getInstance();
    connectionPool.shutDown();

    //  ShutDown Abandoned Connection  :  memory leaks after Tomcat stops
    try {
      AbandonedConnectionCleanupThread.shutdown();
      LOG.info("Abandoned connection shut down!");
    } catch (InterruptedException e) {
      LOG.warn("SEVERE problem cleaning up: " + e.getMessage());
      e.printStackTrace();
    }
    //  This manually deregisters JDBC driver, which prevents Tomcat  from complaining about memory
    // leaks this class
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
      Driver driver = drivers.nextElement();
      try {
        DriverManager.deregisterDriver(driver);
        LOG.info(String.format("deregistering jdbc driver: %s ", driver));
      } catch (SQLException e) {
        LOG.error(String.format("Error deregistering driver: %s ", driver), e);
      }
    }
  }
Exemple #2
0
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
     throws IOException, ServletException {
   try {
     Thread.sleep(1000);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
 public void doPost(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   resp.getWriter().println("<HTML><BODY>");
   resp.getWriter().println(this + ": <br>");
   for (int c = 0; c < 10; c++) {
     resp.getWriter().println("Counter = " + counter + "<BR>");
     try {
       Thread.currentThread().sleep((long) Math.random() * 1000);
       counter++;
     } catch (InterruptedException exc) {
       exc.printStackTrace();
     }
   }
   resp.getWriter().println("</BODY></HTML>");
 }
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // get the number of workers to run
    int count = this.getRequestedCount(request, 4);
    // get the executor service
    final ServletContext sc = this.getServletContext();
    final ExecutorService executorService = (ExecutorService) sc.getAttribute("myExecutorService");
    // create work coordinator
    CountDownLatch countDownLatch = new CountDownLatch(count);

    Long t1 = System.nanoTime();
    // create the workers
    List<RunnableWorkUnit2> workers = new ArrayList<RunnableWorkUnit2>();
    for (int i = 0; i < count; i++) {
      RunnableWorkUnit2 wu =
          new RunnableWorkUnit2("RunnableTask" + String.valueOf(i + 1), countDownLatch);
      workers.add(wu);
    }
    // run the workers through the executor
    for (RunnableWorkUnit2 wu : workers) {
      executorService.execute(wu);
    }

    try {
      System.out.println("START WAITING");
      countDownLatch.await();
      System.out.println("DONE WAITING");
    } catch (InterruptedException ex) {
      ex.printStackTrace();
    }

    Long t2 = System.nanoTime();
    Writer w = response.getWriter();
    w.write(String.format("\n Request processed in %dms!", (t2 - t1) / 1000000));
    w.flush();
    w.close();
  }