activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rajdav...@apache.org
Subject svn commit: r646254 - in /activemq/trunk/activemq-core/src: main/java/org/apache/activemq/thread/PooledTaskRunner.java test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java
Date Wed, 09 Apr 2008 10:13:22 GMT
Author: rajdavies
Date: Wed Apr  9 03:13:11 2008
New Revision: 646254

URL: http://svn.apache.org/viewvc?rev=646254&view=rev
Log:
Applied patch for https://issues.apache.org/activemq/browse/AMQ-1647

Added:
    activemq/trunk/activemq-core/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java
  (with props)
Modified:
    activemq/trunk/activemq-core/src/main/java/org/apache/activemq/thread/PooledTaskRunner.java

Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/thread/PooledTaskRunner.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/thread/PooledTaskRunner.java?rev=646254&r1=646253&r2=646254&view=diff
==============================================================================
--- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/thread/PooledTaskRunner.java
(original)
+++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/thread/PooledTaskRunner.java
Wed Apr  9 03:13:11 2008
@@ -30,7 +30,7 @@
     private boolean queued;
     private boolean shutdown;
     private boolean iterating;
-    private Thread runningThread;
+    private volatile Thread runningThread;
 
     public PooledTaskRunner(Executor executor, Task task, int maxIterationsPerRun) {
         this.executor = executor;
@@ -39,8 +39,11 @@
         runable = new Runnable() {
             public void run() {
                 runningThread = Thread.currentThread();
-                runTask();
-                runningThread = null;
+                try {
+                    runTask();
+                } finally {
+                    runningThread = null;
+                }
             }
         };
     }
@@ -77,7 +80,7 @@
 
     /**
      * shut down the task
-     * 
+     *
      * @throws InterruptedException
      */
     public void shutdown(long timeout) throws InterruptedException {
@@ -114,15 +117,20 @@
         // Don't synchronize while we are iterating so that
         // multiple wakeup() calls can be executed concurrently.
         boolean done = false;
-        for (int i = 0; i < maxIterationsPerRun; i++) {
-            if (!task.iterate()) {
-                done = true;
-                break;
+        try {
+            for (int i = 0; i < maxIterationsPerRun; i++) {
+                if (!task.iterate()) {
+                    done = true;
+                    break;
+                }
+            }
+        } finally {
+            synchronized( runable ) {
+                iterating = false;
             }
         }
 
         synchronized (runable) {
-            iterating = false;
             if (shutdown) {
                 queued = false;
                 runable.notifyAll();

Added: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java
URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java?rev=646254&view=auto
==============================================================================
--- activemq/trunk/activemq-core/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java
(added)
+++ activemq/trunk/activemq-core/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java
Wed Apr  9 03:13:11 2008
@@ -0,0 +1,92 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.activemq.thread;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import junit.framework.TestCase;
+
+public class PooledTaskRunnerTest extends TestCase {
+    private ExecutorService executor;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        executor = Executors.newCachedThreadPool();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        executor.shutdownNow();
+
+        super.tearDown();
+    }
+
+    public void testNormalBehavior() throws Exception {
+        final CountDownLatch latch = new CountDownLatch( 1 );
+
+        PooledTaskRunner runner = new PooledTaskRunner( executor, new Task() {
+            public boolean iterate() {
+                latch.countDown();
+
+                return false;
+            }
+        }, 1 );
+
+        runner.wakeup();
+
+        assertTrue( latch.await( 1, TimeUnit.SECONDS ) );
+
+        runner.shutdown();
+    }
+
+    public void testShutsDownAfterRunnerFailure() throws Exception {
+        Future<Object> future = executor.submit( new Callable<Object>() {
+            public Object call() throws Exception {
+                final CountDownLatch latch = new CountDownLatch( 1 );
+
+                PooledTaskRunner runner = new PooledTaskRunner( executor, new Task() {
+                    public boolean iterate() {
+                        latch.countDown();
+
+                        throw new RuntimeException();
+                    }
+                }, 1 );
+
+                runner.wakeup();
+
+                assertTrue( latch.await( 1, TimeUnit.SECONDS ) );
+
+                runner.shutdown();
+
+                return null;
+            }
+        } );
+
+        try {
+            future.get( 5, TimeUnit.SECONDS );
+        } catch( TimeoutException e ) {
+            fail( "TaskRunner did not shut down cleanly" );
+        }
+    }
+}
\ No newline at end of file

Propchange: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/thread/PooledTaskRunnerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message