Author: rgodfrey
Date: Mon Jan  2 21:58:18 2012
New Revision: 1226557

URL: http://svn.apache.org/viewvc?rev=1226557&view=rev
Log:
QPID-464 : Set the default uncaught exception handler to abruptly terminate the 
JVM
(Note this commit also contains fixes for CurrentActorTest as it was discovered 
that this test was throwing uncaught exceptions from created Threads - i.e. not 
the main test thread)

Modified:
    qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java
    
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java
    
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java

Modified: 
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java?rev=1226557&r1=1226556&r2=1226557&view=diff
==============================================================================
--- qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java 
(original)
+++ qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/Main.java 
Mon Jan  2 21:58:18 2012
@@ -230,11 +230,74 @@ public class Main
             {
                 parsePortArray(options, 
commandLine.getOptionValues(pe.getExcludeName()), pe);
             }
-        }
+        }                
+        
+        setExceptionHandler();
         
         startBroker(options);
     }
 
+    protected void setExceptionHandler()
+    {
+        Thread.UncaughtExceptionHandler handler = null;
+        String handlerClass = 
System.getProperty("qpid.broker.exceptionHandler");
+        if(handlerClass != null)
+        {
+            try
+            {
+                handler = (Thread.UncaughtExceptionHandler) 
Class.forName(handlerClass).newInstance();
+            }
+            catch (ClassNotFoundException e)
+            {
+                
+            }
+            catch (InstantiationException e)
+            {
+                
+            }
+            catch (IllegalAccessException e)
+            {
+                
+            }
+            catch (ClassCastException e)
+            {
+
+            }
+        }
+        
+        if(handler == null)
+        {
+            handler =
+                new Thread.UncaughtExceptionHandler()
+                {
+                    public void uncaughtException(final Thread t, final 
Throwable e)
+                    {
+                        try
+                        {
+                            
System.err.println("########################################################################");
+                            System.err.println("#");
+                            System.err.print("# Unhandled Exception ");
+                            System.err.print(e.toString());
+                            System.err.print(" in Thread ");
+                            System.err.println(t.getName());
+                            System.err.println("#");
+                            System.err.println("# Exiting");
+                            System.err.println("#");
+                            
System.err.println("########################################################################");
+                            e.printStackTrace(System.err);
+                        }
+                        finally
+                        {
+                            Runtime.getRuntime().halt(1);
+                        }
+
+                    }
+                };
+
+            Thread.setDefaultUncaughtExceptionHandler(handler);
+        } 
+    }
+
     protected void startBroker(final BrokerOptions options) throws Exception
     {
         Broker broker = new Broker();

Modified: 
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java?rev=1226557&r1=1226556&r2=1226557&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java 
(original)
+++ 
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/MainTest.java 
Mon Jan  2 21:58:18 2012
@@ -165,6 +165,11 @@ public class MainTest extends QpidTestCa
             _options = options;
         }
 
+        @Override
+        protected void setExceptionHandler()
+        {
+        }
+
         public BrokerOptions getOptions()
         {
             return _options;

Modified: 
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java?rev=1226557&r1=1226556&r2=1226557&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java
 (original)
+++ 
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java
 Mon Jan  2 21:58:18 2012
@@ -23,6 +23,7 @@ package org.apache.qpid.server.logging.a
 import org.apache.commons.configuration.ConfigurationException;
 import org.apache.qpid.AMQException;
 import org.apache.qpid.server.AMQChannel;
+import org.apache.qpid.server.logging.LogActor;
 import org.apache.qpid.server.logging.NullRootMessageLogger;
 
 /**
@@ -49,10 +50,7 @@ import org.apache.qpid.server.logging.Nu
 public class CurrentActorTest extends BaseConnectionActorTestCase
 {
     //Set this to be a reasonably large number
-    int THREADS = 10;
-
-    // Record any exceptions that are thrown by the threads
-    Exception[] _errors = new Exception[THREADS];
+    private static final int THREADS = 10;
 
     /**
      * Test that CurrentActor behaves as LIFO queue.
@@ -161,19 +159,11 @@ public class CurrentActorTest extends Ba
     public void testThreadLocal()
     {
 
-        new Runnable(){
-            public void run()
-            {
-                System.out.println(_errors[0]);
-            }
-        };
-
         // Setup the threads
-        Thread[] threads = new Thread[THREADS];
+        LogMessagesWithAConnectionActor[] threads = new 
LogMessagesWithAConnectionActor[THREADS];
         for (int count = 0; count < THREADS; count++)
         {
-            Runnable test = new LogMessagesWithAConnectionActor(count);
-            threads[count] = new Thread(test);
+            threads[count] = new LogMessagesWithAConnectionActor();
         }
 
         //Run the threads
@@ -198,10 +188,10 @@ public class CurrentActorTest extends Ba
         // Verify that none of the tests threw an exception
         for (int count = 0; count < THREADS; count++)
         {
-            if (_errors[count] != null)
+            if (threads[count].getException() != null)
             {
-                _errors[count].printStackTrace();
-                fail("Error occured in thread:" + count);
+                threads[count].getException().printStackTrace();
+                fail("Error occured in thread:" + count + 
"("+threads[count].getException()+")");
             }
         }
     }
@@ -210,13 +200,12 @@ public class CurrentActorTest extends Ba
      * Creates a new ConnectionActor and logs the given number of messages
      * before removing the actor and validating that there is no set actor.
      */
-    public class LogMessagesWithAConnectionActor implements Runnable
+    public class LogMessagesWithAConnectionActor extends Thread
     {
-        int count;
+        Throwable _exception;
 
-        LogMessagesWithAConnectionActor(int count)
+        public LogMessagesWithAConnectionActor()
         {
-            this.count = count;
         }
 
         public void run()
@@ -227,6 +216,7 @@ public class CurrentActorTest extends Ba
             //fixme reminder that we need a better approach for broker testing.
             try
             {
+                LogActor defaultActor = CurrentActor.get();
 
                 AMQPConnectionActor actor = new 
AMQPConnectionActor(getSession(),
                                                                     new 
NullRootMessageLogger());
@@ -237,20 +227,26 @@ public class CurrentActorTest extends Ba
                 sendTestLogMessage(CurrentActor.get());
 
                 // Verify it was the same actor as we set earlier
-                assertEquals("Retrieved actor is not as expected ",
-                             actor, CurrentActor.get());
+                if(!actor.equals(CurrentActor.get()))
+                   throw new IllegalArgumentException("Retrieved actor is not 
as expected ");
 
                 // Verify that removing the actor works for this thread
                 CurrentActor.remove();
 
-                assertNull("CurrentActor should be null", CurrentActor.get());
+                if(CurrentActor.get() != defaultActor)
+                   throw new IllegalArgumentException("CurrentActor 
("+CurrentActor.get()+") should be default actor" + defaultActor);
             }
-            catch (Exception e)
+            catch (Throwable e)
             {
-                _errors[count] = e;
+                _exception = e;
             }
 
         }
+
+        public Throwable getException()
+        {
+            return _exception;
+        }
     }
 
 }



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:[email protected]

Reply via email to