summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Gemmell <robbie@apache.org>2012-08-03 09:31:58 +0000
committerRobert Gemmell <robbie@apache.org>2012-08-03 09:31:58 +0000
commit71ec27ab45190e0e78cef1830631ac4e753ff348 (patch)
tree9a34611a446604949c8df859214b94585a3654d4
parent3566e5541595848d70d777bbd3d2b76d57e6d08b (diff)
downloadqpid-python-71ec27ab45190e0e78cef1830631ac4e753ff348.tar.gz
QPID-4172: HouseKeepingTask now reverts thread name before exiting to reduce confusion when inspecting thread dumps.
Applied patch from Philip Harvey <phil@philharveyonline.com> and Oleksandr Rudyy<orudyy@gmail.com> merged from trunk r1368519 git-svn-id: https://svn.apache.org/repos/asf/qpid/branches/0.18@1368846 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java7
-rw-r--r--qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java46
2 files changed, 51 insertions, 2 deletions
diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
index 523bafb8e1..1b0e50fd34 100644
--- a/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
+++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
@@ -44,9 +44,9 @@ public abstract class HouseKeepingTask implements Runnable
final public void run()
{
- // Don't need to undo this as this is a thread pool thread so will
- // always go through here before we do any real work.
+ String originalThreadName = Thread.currentThread().getName();
Thread.currentThread().setName(_name);
+
CurrentActor.set(new AbstractActor(_rootLogger)
{
@Override
@@ -67,6 +67,9 @@ public abstract class HouseKeepingTask implements Runnable
finally
{
CurrentActor.remove();
+
+ // eagerly revert the thread name to make thread dumps more meaningful if captured after task has finished
+ Thread.currentThread().setName(originalThreadName);
}
}
diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java
index 0794154e47..8b4a52bb79 100644
--- a/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java
+++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java
@@ -64,4 +64,50 @@ public class HouseKeepingTaskTest extends QpidTestCase
//clean up the test actor
CurrentActor.remove();
}
+
+ public void testThreadNameIsSetForDurationOfTask() throws Exception
+ {
+ //create and set a test actor
+ LogActor testActor = new TestLogActor(new NullRootMessageLogger());
+ CurrentActor.set(testActor);
+
+ String originalThreadName = Thread.currentThread().getName();
+
+ String vhostName = "HouseKeepingTaskTestVhost";
+
+ String expectedThreadNameDuringExecution = vhostName + ":" + "ThreadNameRememberingTask";
+
+ ThreadNameRememberingTask testTask = new ThreadNameRememberingTask(new MockVirtualHost(vhostName));
+
+ testTask.run();
+
+ assertEquals("Thread name should have been set during execution", expectedThreadNameDuringExecution, testTask.getThreadNameDuringExecution());
+ assertEquals("Thread name should have been reverted after task has run", originalThreadName, Thread.currentThread().getName());
+
+ //clean up the test actor
+ CurrentActor.remove();
+ }
+
+
+ private static final class ThreadNameRememberingTask extends HouseKeepingTask
+ {
+ private String _threadNameDuringExecution;
+
+ private ThreadNameRememberingTask(VirtualHost vhost)
+ {
+ super(vhost);
+ }
+
+ @Override
+ public void execute()
+ {
+ _threadNameDuringExecution = Thread.currentThread().getName(); // store current thread name so we can assert it later
+ throw new RuntimeException("deliberate exception to check that thread name still gets reverted");
+ }
+
+ public String getThreadNameDuringExecution()
+ {
+ return _threadNameDuringExecution;
+ }
+ }
}