changeset 1301:ea2a5aedd25a

Print unhandled exceptions in timer code Reviewed-by: vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-November/008612.html
author Omair Majid <omajid@redhat.com>
date Tue, 05 Nov 2013 15:15:18 -0500
parents e12ff00f2846
children 8d65d5454f78
files common/core/src/main/java/com/redhat/thermostat/common/ThreadPoolTimerFactory.java
diffstat 1 files changed, 25 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/common/ThreadPoolTimerFactory.java	Tue Nov 05 15:15:17 2013 -0500
+++ b/common/core/src/main/java/com/redhat/thermostat/common/ThreadPoolTimerFactory.java	Tue Nov 05 15:15:18 2013 -0500
@@ -102,7 +102,7 @@
 
         @Override
         public void setAction(Runnable action) {
-            this.action = action;
+            this.action = new ExceptionPrintingRunnable(action);
         }
 
         @Override
@@ -130,6 +130,30 @@
         
     }
 
+    /**
+     * A decorator for another Runnable object that ensures all exceptions are
+     * printed. Needed because scheduled executors remain silent on failures.
+     */
+    private static class ExceptionPrintingRunnable implements Runnable {
+
+        private final Runnable original;
+
+        public ExceptionPrintingRunnable(Runnable original) {
+            this.original = original;
+        }
+
+        @Override
+        public void run() {
+            try {
+                original.run();
+            } catch (Exception e) {
+                e.printStackTrace();
+                throw e;
+            }
+        }
+
+    }
+
     private ScheduledExecutorService timerThreadPool;
 
     @Override