diff options
| author | Robert Gemmell <robbie@apache.org> | 2012-08-02 15:16:47 +0000 |
|---|---|---|
| committer | Robert Gemmell <robbie@apache.org> | 2012-08-02 15:16:47 +0000 |
| commit | 0ded490520ada9836f84eb210008c1dd7fca87d7 (patch) | |
| tree | 3450dd758c6a2fd6a16fb417f6093946bded3ba2 /java | |
| parent | cfd642cb6362bdc60a8eb6f230a3ba048ddb1059 (diff) | |
| download | qpid-python-0ded490520ada9836f84eb210008c1dd7fca87d7.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>
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1368519 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java')
| -rw-r--r-- | java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java | 7 | ||||
| -rw-r--r-- | java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java | 46 |
2 files changed, 51 insertions, 2 deletions
diff --git a/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java b/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java index 523bafb8e1..1b0e50fd34 100644 --- a/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java +++ b/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/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java b/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java index 0794154e47..8b4a52bb79 100644 --- a/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java +++ b/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; + } + } } |
