Consistent implementation of AsyncListenableTaskExecutor
Issue: SPR-11282
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -67,6 +67,7 @@ public interface AsyncTaskExecutor extends TaskExecutor {
|
||||
* @param task the {@code Runnable} to execute (never {@code null})
|
||||
* @return a Future representing pending completion of the task
|
||||
* @throws TaskRejectedException if the given task was not accepted
|
||||
* @since 3.0
|
||||
*/
|
||||
Future<?> submit(Runnable task);
|
||||
|
||||
@@ -76,6 +77,7 @@ public interface AsyncTaskExecutor extends TaskExecutor {
|
||||
* @param task the {@code Callable} to execute (never {@code null})
|
||||
* @return a Future representing pending completion of the task
|
||||
* @throws TaskRejectedException if the given task was not accepted
|
||||
* @since 3.0
|
||||
*/
|
||||
<T> Future<T> submit(Callable<T> task);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,12 +23,14 @@ import java.util.concurrent.Future;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
|
||||
import org.springframework.core.task.AsyncTaskExecutor;
|
||||
import org.springframework.core.task.AsyncListenableTaskExecutor;
|
||||
import org.springframework.core.task.TaskRejectedException;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
import org.springframework.util.concurrent.ListenableFutureTask;
|
||||
|
||||
/**
|
||||
* Adapter that takes a JDK 1.5 {@code java.util.concurrent.Executor} and
|
||||
* Adapter that takes a JDK {@code java.util.concurrent.Executor} and
|
||||
* exposes a Spring {@link org.springframework.core.task.TaskExecutor} for it.
|
||||
* Also detects an extended {@code java.util.concurrent.ExecutorService}, adapting
|
||||
* the {@link org.springframework.core.task.AsyncTaskExecutor} interface accordingly.
|
||||
@@ -39,15 +41,15 @@ import org.springframework.util.Assert;
|
||||
* @see java.util.concurrent.ExecutorService
|
||||
* @see java.util.concurrent.Executors
|
||||
*/
|
||||
public class TaskExecutorAdapter implements AsyncTaskExecutor {
|
||||
public class TaskExecutorAdapter implements AsyncListenableTaskExecutor {
|
||||
|
||||
private Executor concurrentExecutor;
|
||||
private final Executor concurrentExecutor;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new TaskExecutorAdapter,
|
||||
* using the given JDK 1.5 concurrent executor.
|
||||
* @param concurrentExecutor the JDK 1.5 concurrent executor to delegate to
|
||||
* using the given JDK concurrent executor.
|
||||
* @param concurrentExecutor the JDK concurrent executor to delegate to
|
||||
*/
|
||||
public TaskExecutorAdapter(Executor concurrentExecutor) {
|
||||
Assert.notNull(concurrentExecutor, "Executor must not be null");
|
||||
@@ -56,7 +58,7 @@ public class TaskExecutorAdapter implements AsyncTaskExecutor {
|
||||
|
||||
|
||||
/**
|
||||
* Delegates to the specified JDK 1.5 concurrent executor.
|
||||
* Delegates to the specified JDK concurrent executor.
|
||||
* @see java.util.concurrent.Executor#execute(Runnable)
|
||||
*/
|
||||
@Override
|
||||
@@ -111,4 +113,30 @@ public class TaskExecutorAdapter implements AsyncTaskExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ListenableFuture<?> submitListenable(Runnable task) {
|
||||
try {
|
||||
ListenableFutureTask<Object> future = new ListenableFutureTask<Object>(task, null);
|
||||
this.concurrentExecutor.execute(future);
|
||||
return future;
|
||||
}
|
||||
catch (RejectedExecutionException ex) {
|
||||
throw new TaskRejectedException(
|
||||
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
|
||||
try {
|
||||
ListenableFutureTask<T> future = new ListenableFutureTask<T>(task);
|
||||
this.concurrentExecutor.execute(future);
|
||||
return future;
|
||||
}
|
||||
catch (RejectedExecutionException ex) {
|
||||
throw new TaskRejectedException(
|
||||
"Executor [" + this.concurrentExecutor + "] did not accept task: " + task, ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,15 +26,14 @@ import java.util.concurrent.FutureTask;
|
||||
* @author Arjen Poutsma
|
||||
* @since 4.0
|
||||
*/
|
||||
public class ListenableFutureTask<T> extends FutureTask<T>
|
||||
implements ListenableFuture<T> {
|
||||
public class ListenableFutureTask<T> extends FutureTask<T> implements ListenableFuture<T> {
|
||||
|
||||
private final ListenableFutureCallbackRegistry<T> callbacks = new ListenableFutureCallbackRegistry<T>();
|
||||
|
||||
private final ListenableFutureCallbackRegistry<T> callbacks =
|
||||
new ListenableFutureCallbackRegistry<T>();
|
||||
|
||||
/**
|
||||
* Creates a new {@code ListenableFutureTask} that will, upon running, execute the
|
||||
* given {@link Callable}.
|
||||
* Create a new {@code ListenableFutureTask} that will, upon running,
|
||||
* execute the given {@link Callable}.
|
||||
* @param callable the callable task
|
||||
*/
|
||||
public ListenableFutureTask(Callable<T> callable) {
|
||||
@@ -42,9 +41,9 @@ public class ListenableFutureTask<T> extends FutureTask<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code ListenableFutureTask} that will, upon running, execute the given
|
||||
* {@link Runnable}, and arrange that {@link #get()} will return the given result on
|
||||
* successful completion.
|
||||
* Create a {@code ListenableFutureTask} that will, upon running,
|
||||
* execute the given {@link Runnable}, and arrange that {@link #get()}
|
||||
* will return the given result on successful completion.
|
||||
* @param runnable the runnable task
|
||||
* @param result the result to return on successful completion
|
||||
*/
|
||||
@@ -52,9 +51,10 @@ public class ListenableFutureTask<T> extends FutureTask<T>
|
||||
super(runnable, result);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void addCallback(ListenableFutureCallback<? super T> callback) {
|
||||
callbacks.addCallback(callback);
|
||||
this.callbacks.addCallback(callback);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -62,7 +62,7 @@ public class ListenableFutureTask<T> extends FutureTask<T>
|
||||
Throwable cause;
|
||||
try {
|
||||
T result = get();
|
||||
callbacks.success(result);
|
||||
this.callbacks.success(result);
|
||||
return;
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
@@ -75,9 +75,10 @@ public class ListenableFutureTask<T> extends FutureTask<T>
|
||||
cause = ex;
|
||||
}
|
||||
}
|
||||
catch (Throwable t) {
|
||||
cause = t;
|
||||
catch (Throwable ex) {
|
||||
cause = ex;
|
||||
}
|
||||
callbacks.failure(cause);
|
||||
this.callbacks.failure(cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user