Author: robbie
Date: Thu Aug  2 15:16:47 2012
New Revision: 1368519

URL: http://svn.apache.org/viewvc?rev=1368519&view=rev
Log:
QPID-4172: HouseKeepingTask now reverts thread name before exiting to reduce 
confusion when inspecting thread dumps.

Applied patch from Philip Harvey <[email protected]> and Oleksandr 
Rudyy<[email protected]>

Modified:
    
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
    
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java

Modified: 
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java?rev=1368519&r1=1368518&r2=1368519&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
 (original)
+++ 
qpid/trunk/qpid/java/broker/src/main/java/org/apache/qpid/server/virtualhost/HouseKeepingTask.java
 Thu Aug  2 15:16:47 2012
@@ -44,9 +44,9 @@ public abstract class HouseKeepingTask i
 
     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 i
         finally
         {
             CurrentActor.remove();
+
+            // eagerly revert the thread name to make thread dumps more 
meaningful if captured after task has finished
+            Thread.currentThread().setName(originalThreadName);
         }
     }
 

Modified: 
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java
URL: 
http://svn.apache.org/viewvc/qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java?rev=1368519&r1=1368518&r2=1368519&view=diff
==============================================================================
--- 
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java
 (original)
+++ 
qpid/trunk/qpid/java/broker/src/test/java/org/apache/qpid/server/virtualhost/HouseKeepingTaskTest.java
 Thu Aug  2 15:16:47 2012
@@ -64,4 +64,50 @@ public class HouseKeepingTaskTest extend
         //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;
+        }
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to