diff options
| author | Robert Godfrey <rgodfrey@apache.org> | 2012-01-02 21:58:18 +0000 |
|---|---|---|
| committer | Robert Godfrey <rgodfrey@apache.org> | 2012-01-02 21:58:18 +0000 |
| commit | 7b0c33ff443deb937d26f07c039bd483e9bcbe29 (patch) | |
| tree | 19c45aece9590865ee56d8952a927d4f96b2970a /java | |
| parent | 2c10159e28ff85e52840d5c6964123e4c410458d (diff) | |
| download | qpid-python-7b0c33ff443deb937d26f07c039bd483e9bcbe29.tar.gz | |
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)
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1226557 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
3 files changed, 91 insertions, 27 deletions
diff --git a/java/broker/src/main/java/org/apache/qpid/server/Main.java b/java/broker/src/main/java/org/apache/qpid/server/Main.java index 0c038c7800..e53c73b8a8 100644 --- a/java/broker/src/main/java/org/apache/qpid/server/Main.java +++ b/java/broker/src/main/java/org/apache/qpid/server/Main.java @@ -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(); diff --git a/java/broker/src/test/java/org/apache/qpid/server/MainTest.java b/java/broker/src/test/java/org/apache/qpid/server/MainTest.java index eea8e173f4..3e4c30291c 100644 --- a/java/broker/src/test/java/org/apache/qpid/server/MainTest.java +++ b/java/broker/src/test/java/org/apache/qpid/server/MainTest.java @@ -165,6 +165,11 @@ public class MainTest extends QpidTestCase _options = options; } + @Override + protected void setExceptionHandler() + { + } + public BrokerOptions getOptions() { return _options; diff --git a/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java b/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java index 32ad1d110d..9a065ea2db 100644 --- a/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java +++ b/java/broker/src/test/java/org/apache/qpid/server/logging/actors/CurrentActorTest.java @@ -23,6 +23,7 @@ package org.apache.qpid.server.logging.actors; 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.NullRootMessageLogger; 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 BaseConnectionActorTestCase 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 BaseConnectionActorTestCase // 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 BaseConnectionActorTestCase * 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 BaseConnectionActorTestCase //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 BaseConnectionActorTestCase 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; + } } } |
