Close

Spring - Task Execution Introduction

[Last Updated: Nov 18, 2023]

Spring Core framework provides abstraction of task execution. Task execution is referred as creating a new thread and perform some task in it. Spring's task execution abstraction hides implementation differences between Java SE 1.4, Java SE 5 and Java EE environments. Spring underlying implementation can be based on Java Executor framework or a simple Java thread or even it can be based on a third party library like Quartz.



The interfaces


java.util.concurrent.ExecutorExecutororg.springframework.core.task.TaskExecutorTaskExecutororg.springframework.core.task.AsyncTaskExecutorAsyncTaskExecutororg.springframework.core.task.AsyncListenableTaskExecutorAsyncListenableTaskExecutorLogicBig

Spring task execution is based on the TaskExecutor and it's sub-interfaces.

The diagram on the left is interactive and clickable, it will take you the api pages.

  • TaskExecutor is same as Executor interface.
  • AsyncTaskExecutor can handle Callable submission and returns Future.
  • AsyncListenableTaskExecutor returns ListenableFuture on task submission, which can attach listener to get callbacks on task completion. Deprecated since Spring 6.0 and added methods in AsyncTaskExecutor which return CompletableFuture.

Definition of TaskExecutor

(Version: spring-framework 6.0.13)
package org.springframework.core.task;
   ........
@FunctionalInterface
public interface TaskExecutor extends Executor {
    @Override
    void execute(Runnable task); 
}

Definition of AsyncTaskExecutor

(Version: spring-framework 6.0.13)
package org.springframework.core.task;
   ........
public interface AsyncTaskExecutor extends TaskExecutor {
    .............
    @Deprecated
    void execute(Runnable task, long startTimeout); 1
    Future<?> submit(Runnable task); 2
    <T> Future<T> submit(Callable<T> task); 3
    default CompletableFuture<Void> submitCompletable(Runnable task) {
        return CompletableFuture.runAsync(task, this);
    } 4
    default <T> CompletableFuture<T> submitCompletable(Callable<T> task) {
        return FutureUtils.callAsync(task, this);
    } 5
}
1Execute the given task. Deprecated as of 5.3.16 since the common executors do not support start timeouts
2Submit a Runnable task for execution, receiving a Future representing that task. The Future will return a null result upon completion.
3Submit a Callable task for execution, receiving a Future representing that task. The Future will return the Callable's result upon completion.
4Submit a Runnable task for execution, receiving a CompletableFuture representing that task. The Future will return a null result upon completion.
5Submit a Callable task for execution, receiving a CompletableFuture representing that task. The Future will return the Callable's result upon completion.

Definition of AsyncListenableTaskExecutor

(Version: spring-framework 6.0.13)
package org.springframework.core.task;
   ........
@Deprecated(since = "6.0")
public interface AsyncListenableTaskExecutor extends AsyncTaskExecutor {
    @Deprecated
    ListenableFuture<?> submitListenable(Runnable task); 1
    @Deprecated
    <T> ListenableFuture<T> submitListenable(Callable<T> task); 2
}
1Submit a Runnable task for execution, receiving a ListenableFuture representing that task. The Future will return a null result upon completion.
2Submit a Callable task for execution, receiving a ListenableFuture representing that task. The Future will return the Callable's result upon completion.

AsyncListenableTaskExecutor has been deprecated in 6.0, in favor of AsyncTaskExecutor.submitCompletable(Runnable) and AsyncTaskExecutor.submitCompletable(Callable)

Definition of ListenableFuture

(Version: spring-framework 6.0.13)
package org.springframework.util.concurrent;
   ........
@Deprecated(since = "6.0")
public interface ListenableFuture<T> extends Future<T> {
    @Deprecated(since = "6.0")
    void addCallback(ListenableFutureCallback<? super T> callback); 1
    @Deprecated(since = "6.0")
    void addCallback(
            SuccessCallback<? super T> successCallback,
            FailureCallback failureCallback); 2
    default CompletableFuture<T> completable() {
        CompletableFuture<T> completable = new DelegatingCompletableFuture<>(this);
        addCallback(completable::complete, completable::completeExceptionally);
        return completable;
    } 3
}
1 Register the given ListenableFutureCallback. @param callback the callback to register @deprecated as of 6.0, in favor of CompletableFuture#whenComplete(BiConsumer)
2 Java 8 lambda-friendly alternative with success and failure callbacks. @param successCallback the success callback @param failureCallback the failure callback @since 4.1 @deprecated as of 6.0, in favor of CompletableFuture#whenComplete(BiConsumer)
3 Expose this ListenableFuture as a JDK CompletableFuture. @since 5.0

Definition of ListenableFutureCallback

(Version: spring-framework 6.0.13)
package org.springframework.util.concurrent;
   ........
@Deprecated(since = "6.0")
public interface ListenableFutureCallback<T> extends SuccessCallback<T>, FailureCallback {}

Definition of SuccessCallback

(Version: spring-framework 6.0.13)
package org.springframework.util.concurrent;
   ........
@Deprecated(since = "6.0")
@FunctionalInterface
public interface SuccessCallback<T> {
    void onSuccess(@Nullable T result); 1
}
1 Called when the ListenableFuture completes with success.

Note that Exceptions raised by this method are ignored. @param result the result

Definition of FailureCallback

(Version: spring-framework 6.0.13)
package org.springframework.util.concurrent;
   ........
@Deprecated(since = "6.0")
@FunctionalInterface
public interface FailureCallback {
    void onFailure(Throwable ex); 1
}
1 Called when the ListenableFuture completes with failure.

Note that Exceptions raised by this method are ignored. @param ex the failure

We are going to quickly go through implementations with examples.




In this tutorial we have introduced commonly used task executors. There are more. Please checkout reference document TaskExecutor types list. In the next tutorial we will give examples for Spring TaskExecutor implementation.



See Also