[cvs] expresso commit by lhamel: add finish to close thread

JCorporate Ltd jcorp at jcorporate.com
Tue Feb 1 22:56:28 UTC 2005


Log Message:
-----------
add finish to close thread

Modified Files:
--------------
    expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/logging:
        LogHandler.java

Revision Data
-------------
Index: LogHandler.java
===================================================================
RCS file: /home/javacorp/.cvs/expresso/expresso/expresso-web/WEB-INF/src/com/jcorporate/expresso/core/logging/LogHandler.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/logging/LogHandler.java -Lexpresso-web/WEB-INF/src/com/jcorporate/expresso/core/logging/LogHandler.java -u -r1.13 -r1.14
--- expresso-web/WEB-INF/src/com/jcorporate/expresso/core/logging/LogHandler.java
+++ expresso-web/WEB-INF/src/com/jcorporate/expresso/core/logging/LogHandler.java
@@ -81,11 +81,10 @@
 
 /**
  * The LogHandler is an asynchronous version of logging - it accepts
- * requests to log, then logs them "eventually" in a separate thread
+ * requests to log, then logs them "eventually" in a separate thread,
+ * to a database, not a file.  Used for 'dbLog' in expressoLogging.xml
  *
  * @author Michael Nash
- * @deprecated As of Expresso 5.6  This has not been used since
- *             Log4j was integrated with Expresso.
  */
 public class LogHandler
         extends ExpressoThread {
@@ -114,21 +113,12 @@
      */
     private int sleepTime = 30;
     private static String thisClass = LogHandler.class.getName();
+    private static boolean shouldContinue = true; // flag for stopping thread
 
     /**
      * Constructor
      */
     public LogHandler() {
-
-        /* String sleepTimeString = StringUtil.notNull(ConfigManager.getProperty(
-
-            "loghandler.sleeptime"));
-
-        if (!sleepTimeString.equals("")) {
-
-            sleepTime = new Integer(sleepTimeString).intValue();
-
-        } */
     } /* LogHandler() */
 
     /**
@@ -136,6 +126,11 @@
      */
     public synchronized void addToQueue(LogEntry le) {
         queue.addElement(le);
+        try {
+            checkQueue();
+        } catch (LogException e) {
+            log.error("Unable to check log queue: ", e);
+        }
     } /* addToQueue(LogEntry) */
 
     /**
@@ -176,17 +171,10 @@
         }
         try {
             LogEntry oneLogEntry = null;
-            int writeCount = 0;
 
             while (queue.size() > 0) {
-                oneLogEntry = (LogEntry) queue.firstElement();
-                queue.removeElementAt(0);
+                oneLogEntry = (LogEntry) queue.remove(0);
                 oneLogEntry.add();
-                writeCount++;
-
-                if (writeCount > maxWrites) {
-                    break;
-                }
             } /* for each queue entry */
 
         } catch (Exception de) {
@@ -222,10 +210,7 @@
      * we need to flush what we've got and then bail.
      */
     public void finalize() {
-        try {
-            flush();
-        } catch (LogException e) {
-        }
+        finish();
     } /* finalize() */
 
     /**
@@ -233,8 +218,10 @@
      */
     public static void flush()
             throws LogException {
-        startUp();
-        myInstance.checkQueue();
+        if ( queue.size() > 0 ) {
+            startUp();
+            myInstance.checkQueue();
+        }
     } /* flush() */
 
 
@@ -409,27 +396,23 @@
      *
      * @param dbName
      * @param channelName
-     * @param level
+     * @param level_Unused
      * @param objectName
      * @param e
      * @param color
      * @param uid
      * @param jobNumber
      */
-    public static void log(String dbName, String channelName, int level,
+    public static void log(String dbName, String channelName, int level_Unused,
                            String objectName, Throwable e, String color,
                            String uid, String jobNumber)
             throws LogException {
-        String myName = (thisClass +
-                "log(String, String, int, String, Exception," +
-                "String, String, String)");
-        String message = e.getMessage();
-
-        if (e instanceof DBException) {
-            DBException de = (DBException) e;
-            message = de.getMessage() + ":" + de.getDBMessage();
-        }
         if (trace) {
+            String message = e.getMessage();
+            if (e instanceof DBException) {
+                DBException de = (DBException) e;
+                message = de.getMessage() + ":" + de.getDBMessage();
+            }
             System.err.println(objectName + ":" + message);
             e.printStackTrace(System.err);
         }
@@ -451,6 +434,9 @@
             myLog.setTimeStamp();
             myInstance.addToQueue(myLog);
         } catch (DBException de) {
+            String myName = (thisClass +
+                    "log(String, String, int, String, Exception," +
+                    "String, String, String)");
             throw new LogException(myName + ":" + de.getMessage());
         }
     } /* log(String, String, int, String, Throwable, String, String, String) */
@@ -583,7 +569,7 @@
             if (trace) {
                 System.err.println(myName + ":Log Handler starts");
             }
-            while (true) {
+            while (shouldContinue) {
                 if (idleTimes >= maxIdleTimes) {
                     if (trace) {
                         System.err.println(myName +
@@ -601,15 +587,16 @@
                             ae.getMessage());
                 }
 
-                yield();
-                sleep(sleepTime * 1000);
+                try {
+                    sleep(sleepTime * 1000);
+                } catch (InterruptedException e) {
+                    // no problem
+                }
             } /* forever */
 
-        } catch (InterruptedException ie) {
-            log.info(myName + ": LogHandler was interrupted:" +
-                    ie.getMessage());
         } catch (Throwable t) {
-            System.err.println("Test");
+            System.err.println("Problem running LogHandler: ");
+            t.printStackTrace();
         }
     } /* run() */
 
@@ -645,27 +632,27 @@
      */
     private static void setupLevels() {
         maxLevel = 9;
-
-        //MR      String myName = (THIS_CLASS + "setupLevels()");
-        //MR      try {
-        //MR         String logLev = Setup.getValueRequired("default", "LogLevel");
-        //MR         if (logLev.equals("")) {
-        //MR            logLev = ("9");
-        //MR         }
-        //MR         try {
-        //MR            setMax(new Integer(logLev).intValue());
-        //MR         } catch(NumberFormatException ne) {
-        //MR            System.err.println(myName + ":Log level '" + logLev
-        //MR               + "' cannot be converted to a number");
-        //MR         }
-        //MR      } catch(DBException de) {
-        //MR         throw new LogException(myName + ":Unable to read setup values", de);
-        //MR      }
     } /* setupLevel() */
 
+    /**
+     * call to shutdown thread
+     */
+    public synchronized static void finish() {
+        if ( myInstance != null ) {
+            if ( myInstance.isAlive() ) {
+                try {
+                    flush();
+                } catch (LogException e) {
+                    e.printStackTrace();
+                }
+                shouldContinue = false;
+                myInstance.interrupt();
+            }
+        }
+    }
 
     /**
-     *
+     *  start thread
      *
      */
     public synchronized static void startUp() {
@@ -711,17 +698,17 @@
      * @param e          Exception being logged
      */
     public static void tryLog(String objectName, Exception e) {
-        String myName = (thisClass + "tryLog(String, Exception)");
-        String message = e.getMessage();
-
-        if (e instanceof DBException) {
-            DBException de = (DBException) e;
-            message = de.getMessage() + ":" + de.getDBMessage();
-        }
         try {
             startUp();
             log(objectName, e);
         } catch (LogException he) {
+            String myName = (thisClass + "tryLog(String, Exception)");
+            String message = e.getMessage();
+
+            if (e instanceof DBException) {
+                DBException de = (DBException) e;
+                message = de.getMessage() + ":" + de.getDBMessage();
+            }
             System.err.println(myName + ":Unable to log exception '" +
                     message + "' from object '" + objectName +
                     "':" + he.getMessage());
@@ -738,11 +725,10 @@
      * @param msg
      */
     public static void tryLog(String objectName, String msg) {
-        String myName = (thisClass + "tryLog(String, String)");
-
         try {
             log(objectName, msg);
         } catch (LogException he) {
+            String myName = (thisClass + "tryLog(String, String)");
             System.err.println(myName + ":Unable to log message '" + msg +
                     "' from object '" + objectName + "':" +
                     he.getMessage());


More information about the cvs mailing list