mirror of https://github.com/apache/activemq.git
Applied patch for https://issues.apache.org/activemq/browse/AMQ-1647
git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@646254 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
ed4d6f66cb
commit
5660ebc8f2
|
@ -30,7 +30,7 @@ class PooledTaskRunner implements TaskRunner {
|
||||||
private boolean queued;
|
private boolean queued;
|
||||||
private boolean shutdown;
|
private boolean shutdown;
|
||||||
private boolean iterating;
|
private boolean iterating;
|
||||||
private Thread runningThread;
|
private volatile Thread runningThread;
|
||||||
|
|
||||||
public PooledTaskRunner(Executor executor, Task task, int maxIterationsPerRun) {
|
public PooledTaskRunner(Executor executor, Task task, int maxIterationsPerRun) {
|
||||||
this.executor = executor;
|
this.executor = executor;
|
||||||
|
@ -39,8 +39,11 @@ class PooledTaskRunner implements TaskRunner {
|
||||||
runable = new Runnable() {
|
runable = new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
runningThread = Thread.currentThread();
|
runningThread = Thread.currentThread();
|
||||||
runTask();
|
try {
|
||||||
runningThread = null;
|
runTask();
|
||||||
|
} finally {
|
||||||
|
runningThread = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -77,7 +80,7 @@ class PooledTaskRunner implements TaskRunner {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* shut down the task
|
* shut down the task
|
||||||
*
|
*
|
||||||
* @throws InterruptedException
|
* @throws InterruptedException
|
||||||
*/
|
*/
|
||||||
public void shutdown(long timeout) throws InterruptedException {
|
public void shutdown(long timeout) throws InterruptedException {
|
||||||
|
@ -114,15 +117,20 @@ class PooledTaskRunner implements TaskRunner {
|
||||||
// Don't synchronize while we are iterating so that
|
// Don't synchronize while we are iterating so that
|
||||||
// multiple wakeup() calls can be executed concurrently.
|
// multiple wakeup() calls can be executed concurrently.
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
for (int i = 0; i < maxIterationsPerRun; i++) {
|
try {
|
||||||
if (!task.iterate()) {
|
for (int i = 0; i < maxIterationsPerRun; i++) {
|
||||||
done = true;
|
if (!task.iterate()) {
|
||||||
break;
|
done = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
synchronized( runable ) {
|
||||||
|
iterating = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
synchronized (runable) {
|
synchronized (runable) {
|
||||||
iterating = false;
|
|
||||||
if (shutdown) {
|
if (shutdown) {
|
||||||
queued = false;
|
queued = false;
|
||||||
runable.notifyAll();
|
runable.notifyAll();
|
||||||
|
|
|
@ -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" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue