In Spring 6.0 AsyncListenableTaskExecutor was deprecated in favor of AsyncTaskExecutor.submitCompletable(Runnable) and AsyncTaskExecutor.submitCompletable(Callable).
That means we don't need AsyncListenableTaskExecutor to receive task completion callback as we can achieve the same by using core Java CompletableFuture
Example
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncListenableTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
public class AsyncTaskExecutorCompletableFutureExample {
public static void main(String[] args) throws Exception {
ApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);
MyBean bean = context.getBean(MyBean.class);
bean.runTasks();
}
@Configuration
public static class MyConfig {
@Bean
MyBean myBean() {
return new MyBean();
}
@Bean
AsyncListenableTaskExecutor taskExecutor() {
SimpleAsyncTaskExecutor t = new SimpleAsyncTaskExecutor();
t.setConcurrencyLimit(100);
return t;
}
}
private static class MyBean {
@Autowired
private AsyncListenableTaskExecutor executor;
public void runTasks() throws Exception {
for (int i = 0; i < 10; i++) {
CompletableFuture<String> completableFuture =
executor.submitCompletable(getTask(i));
completableFuture.thenAccept(task -> System.out.println("Completed: " + task));
}
}
private Callable<String> getTask(int i) {
return () -> {
System.out.printf("running task %d. Thread: %s%n",
i,
Thread.currentThread().getName());
return String.format("Task finished %d", i);
};
}
}
}