use ThreadPool and Cron
authoradam <adam@megacz.com>
Sun, 8 Jul 2007 22:52:39 +0000 (22:52 +0000)
committeradam <adam@megacz.com>
Sun, 8 Jul 2007 22:52:39 +0000 (22:52 +0000)
darcs-hash:20070708225239-5007d-d61c76630826879662a7d65c34cb8d7d64d90de2.gz

src/org/ibex/mail/Script.java
src/org/ibex/mail/SqliteTable.java

index 0e404e0..f3a189c 100644 (file)
@@ -172,7 +172,7 @@ public class Script extends JS.Obj implements Target {
                     Log.warn("dbug", b.getClass().getName());
                     Message m = (Message)b;
                     final Process p = Runtime.getRuntime().exec(JSU.toString(a));
-                    new Thread() {
+                    Main.threadPool.start(new Runnable() {
                         public void run() {
                             try {
                                 BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
@@ -181,7 +181,7 @@ public class Script extends JS.Obj implements Target {
                                     Log.warn("shell", s);
                             } catch (Exception e) { e.printStackTrace(); }
                         }
-                    }.start();
+                    });
                     OutputStream os = p.getOutputStream();
                     Stream stream = new Stream(os);
 
index f24fabf..98950f6 100644 (file)
@@ -49,34 +49,32 @@ public class SqliteTable {
         this.reapTable = reapTable;
         this.reapColumn = reapColumn;
         if (reapTable != null && reapColumn != null)
-            new Reaper().start();
+            Main.cron.executeLater(1000 * REAPER_INTERVAL_SECONDS, new Reaper());
     }
 
     public static final int REAPER_INTERVAL_SECONDS = 60 * 60;
 
-    private class Reaper extends Thread {
-
+    private class Reaper implements Runnable {
         public void run() {
-            while(true) {
-                try {
-                    try { Thread.sleep(1000 * REAPER_INTERVAL_SECONDS); } catch (Exception e) { };
-                    Log.warn(Reaper.class, filename + " reaping...");
-                    long when = System.currentTimeMillis();
-                    when -= 5 * 24 * 60 * 60 * 1000;
-                    synchronized(SqliteTable.this) {
-                        PreparedStatement ps = conn.prepareStatement("select count(*) from "+reapTable+" where "+reapColumn+"<?");
-                        ps.setTimestamp(1, new Timestamp(when));
-                        ResultSet rs = ps.executeQuery();
-                        if (rs.next())
-                            Log.warn(Reaper.class, filename + " reaping " + rs.getInt(1) + " entries");
-                        Log.warn(Reaper.class, filename + ": " + "delete from "+reapTable+" where "+reapColumn+"<"+when);
-                        ps = conn.prepareStatement("delete from "+reapTable+" where "+reapColumn+"<?");
-                        ps.setTimestamp(1, new Timestamp(when));
-                        int rows = ps.executeUpdate();
-                        Log.warn(Reaper.class, filename + " done reaping; removed " + rows + " rows");
-                    }
-                } catch (Exception e) { Log.error(Reaper.class, e); }
-            }
+            try {
+                Log.warn(Reaper.class, filename + " reaping...");
+                long when = System.currentTimeMillis();
+                when -= 5 * 24 * 60 * 60 * 1000;
+                synchronized(SqliteTable.this) {
+                    PreparedStatement ps =
+                        conn.prepareStatement("select count(*) from "+reapTable+" where "+reapColumn+"<?");
+                    ps.setTimestamp(1, new Timestamp(when));
+                    ResultSet rs = ps.executeQuery();
+                    if (rs.next())
+                        Log.warn(Reaper.class, filename + " reaping " + rs.getInt(1) + " entries");
+                    Log.warn(Reaper.class, filename + ": " + "delete from "+reapTable+" where "+reapColumn+"<"+when);
+                    ps = conn.prepareStatement("delete from "+reapTable+" where "+reapColumn+"<?");
+                    ps.setTimestamp(1, new Timestamp(when));
+                    int rows = ps.executeUpdate();
+                    Log.warn(Reaper.class, filename + " done reaping; removed " + rows + " rows");
+                }
+            } catch (Exception e) { Log.error(Reaper.class, e); }
+            Main.cron.executeLater(1000 * REAPER_INTERVAL_SECONDS, this);
         }
     }