Using RequestProcessor instead of ExecutionService, as RP is more likely to shutdown automatically
authorjtulach@netbeans.org
Thu, 06 Dec 2007 09:34:10 +0000
changeset 2738bac2b7ac1618
parent 2737 df5f4f3b083e
child 2739 7f62de29485a
Using RequestProcessor instead of ExecutionService, as RP is more likely to shutdown automatically
logger/uihandlerserver/src/java/org/netbeans/server/uihandler/LogsManager.java
     1.1 --- a/logger/uihandlerserver/src/java/org/netbeans/server/uihandler/LogsManager.java	Wed Dec 05 16:19:14 2007 +0000
     1.2 +++ b/logger/uihandlerserver/src/java/org/netbeans/server/uihandler/LogsManager.java	Thu Dec 06 09:34:10 2007 +0000
     1.3 @@ -41,12 +41,15 @@
     1.4  
     1.5  package org.netbeans.server.uihandler;
     1.6  
     1.7 -import java.util.concurrent.TimeoutException;
     1.8 +import java.io.BufferedInputStream;
     1.9 +import java.io.File;
    1.10 +import java.io.FileInputStream;
    1.11 +import java.io.IOException;
    1.12 +import java.io.InputStream;
    1.13  import javax.servlet.ServletException;
    1.14  import javax.servlet.ServletRequest;
    1.15  import org.netbeans.modules.exceptions.entity.Logfile;
    1.16  import java.util.concurrent.ExecutionException;
    1.17 -import java.io.*;
    1.18  import java.util.ArrayList;
    1.19  import java.util.Arrays;
    1.20  import java.util.Collection;
    1.21 @@ -57,11 +60,6 @@
    1.22  import java.util.List;
    1.23  import java.util.Map;
    1.24  import java.util.Set;
    1.25 -import java.util.concurrent.ExecutorService;
    1.26 -import java.util.concurrent.Executors;
    1.27 -import java.util.concurrent.Future;
    1.28 -import java.util.concurrent.ScheduledExecutorService;
    1.29 -import java.util.concurrent.TimeUnit;
    1.30  import java.util.logging.Handler;
    1.31  import java.util.logging.Level;
    1.32  import java.util.logging.LogRecord;
    1.33 @@ -79,6 +77,7 @@
    1.34  import org.openide.util.Exceptions;
    1.35  import org.openide.util.Lookup;
    1.36  import org.openide.util.NbCollections;
    1.37 +import org.openide.util.RequestProcessor;
    1.38  
    1.39  /**
    1.40   *
    1.41 @@ -89,7 +88,7 @@
    1.42      private static Object logInfoLock = new Object();
    1.43      
    1.44      private File dir;
    1.45 -    private transient Future<?> initTask;
    1.46 +    private transient RequestProcessor.Task initTask;
    1.47      private transient boolean closed;
    1.48      
    1.49      /** statistics to work on and their global values */
    1.50 @@ -102,10 +101,11 @@
    1.51      private List<Integer> initCounts = Collections.nCopies(2, 0);
    1.52      
    1.53      /** executor to use for changing data */
    1.54 -    private final ScheduledExecutorService EXEC = Executors.newSingleThreadScheduledExecutor();
    1.55 +    private final RequestProcessor EXEC;
    1.56      
    1.57      private LogsManager(File userDir) {
    1.58          String msg = "Creating manager for " + userDir;
    1.59 +        EXEC = new RequestProcessor("LogsManager: " + userDir);
    1.60          LOG.log(Level.FINEST, msg, new Exception(msg));
    1.61          this.dir = userDir;
    1.62          this.statistics = new ArrayList<LogsManager.Value>();
    1.63 @@ -113,7 +113,7 @@
    1.64              addStatisticsData(this.statistics, s);
    1.65          }
    1.66          
    1.67 -        this.initTask = EXEC.submit(new Verify());
    1.68 +        this.initTask = EXEC.post(new Verify());
    1.69          
    1.70          
    1.71          DEFAULT = this;
    1.72 @@ -259,19 +259,16 @@
    1.73          this.closed = true;
    1.74          DbInsertion.waitLogFileInsertFinished();
    1.75          DbInsertion.waitIssueInsertFinished();
    1.76 -        boolean b = this.initTask.cancel(true);
    1.77 +        boolean b = this.initTask.cancel();
    1.78          try {
    1.79 -            EXEC.submit(new Runnable() {
    1.80 -                public void run() {
    1.81 -                }
    1.82 -            }).get(10, TimeUnit.SECONDS);
    1.83 +            this.initTask.waitFinished(10000);
    1.84          } catch (Exception ex) {
    1.85              // ignore, we just need to wait a bit till the task finishes
    1.86              LOG.log(Level.INFO, "Timeout while waiting for parsing task to finish");
    1.87          }
    1.88          LOG.log(java.util.logging.Level.INFO, "Cancel of init task: {0}", b);
    1.89 -        EXEC.shutdown();
    1.90 -        LOG.log(java.util.logging.Level.INFO, "is shutdown {0}", EXEC.isShutdown());
    1.91 +        EXEC.stop();
    1.92 +        LOG.log(java.util.logging.Level.INFO, "shutdown");
    1.93      }
    1.94      
    1.95      public void addLog(final File f, final String remoteHost) {
    1.96 @@ -312,7 +309,7 @@
    1.97          }
    1.98          LOG.info("request for parsing " + f);
    1.99          R run = new R();
   1.100 -        initTask = EXEC.submit(run);
   1.101 +        initTask = EXEC.post(run);
   1.102      }
   1.103      
   1.104      /**
   1.105 @@ -463,12 +460,11 @@
   1.106              LOG.warning("Specify dir attribute, otherwise the server cannot work");
   1.107              return;
   1.108          }
   1.109 -        if (!initTask.isDone()) {
   1.110 +        if (!initTask.isFinished()) {
   1.111              int timeout = Integer.getInteger("uihandlerserver.timeout", 30);
   1.112 -            try {
   1.113 -                initTask.get(timeout, TimeUnit.SECONDS);
   1.114 -            } catch (TimeoutException ex) {
   1.115 -                LOG.log(Level.WARNING, "Timeout (" + timeout + ") waiting for page context", ex); // NOI18N
   1.116 +            boolean finished = initTask.waitFinished(1000 * timeout);
   1.117 +            if (!finished) {
   1.118 +                LOG.log(Level.WARNING, "Timeout ({0} s) waiting for page context", timeout); // NOI18N
   1.119                  try {
   1.120                      //context.getSession().setAttribute("post.init.url", HttpUtils.getRequestURL(context.getRequest()));
   1.121                      context.getRequest().getRequestDispatcher("/initializing.jsp").forward(context.getRequest(), context.getResponse());
   1.122 @@ -721,6 +717,10 @@
   1.123  
   1.124              cnt = 0;
   1.125              files = dir.listFiles();
   1.126 +            if (files == null) {
   1.127 +                LOG.warning("Not a directory: " + dir);
   1.128 +                files = new File[0];
   1.129 +            }
   1.130              Collections.shuffle(Arrays.asList(files));
   1.131          }
   1.132          public void run() {
   1.133 @@ -814,7 +814,7 @@
   1.134              
   1.135              if (cnt < files.length) {
   1.136                  LOG.log(Level.FINE, "Breaking the parsing at {0}", cnt);
   1.137 -                EXEC.schedule(this, 10, TimeUnit.MILLISECONDS);
   1.138 +                EXEC.post(this, 10, Thread.MIN_PRIORITY);
   1.139                  return;
   1.140              }
   1.141              
   1.142 @@ -839,7 +839,7 @@
   1.143              em.close();
   1.144  
   1.145              init();
   1.146 -            EXEC.schedule(this, 5 * 60 * 60, TimeUnit.SECONDS);
   1.147 +            EXEC.post(this, 5 * 60 * 60 * 1000, Thread.MIN_PRIORITY);
   1.148          }
   1.149          
   1.150      }