I want to use spring boot's Asynchronous mechanism, here is my code.
@Slf4j
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
private static final int MAX_POOL_SIZE = 50;
private static final int CORE_POOL_SIZE = 20;
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setBeanName("taskExecutor");
taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
taskExecutor.setThreadNamePrefix("async-task-thread-pool");
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
taskExecutor.setAwaitTerminationSeconds(60 * 10);
taskExecutor.setRejectedExecutionHandler(
(r, executor) -> log.warn("current thread pool is full, reject to invoke."));
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return (ex, method, params) ->
{
log.error("invoke async method occurs error. method: {}, params: {}",
method.getName(), JSON.toJSONString(params), ex);
throw new RuntimeException();
};
}
}
And I add the @Async annotation on another class' method.
@Override
public Future<Result> getResult(String name);
But when I call the getResult() method, it report No bean named 'taskExecutor' available: No matching Executor bean found for qualifier 'taskExecutor' - neither qualifier match nor bean name match!.
And then I find this page:https://www.baeldung.com/spring-async. I follow this page's guide - remove the @EnableAsync annotation on Application.class and add to the AsyncConfig.class. But still got the same error message.It seems that setBeanName() method doesn't effect. Is my usage wrong?
By the way, I've read the api doc of EnableAsync, it writes
Note: In the above example the {@code ThreadPoolTaskExecutor} is not a fully managed Spring bean. Add the {@code @Bean} annotation to the {@code getAsyncExecutor()} method
if you want a fully managed bean. In such circumstances it is no longer necessary to manually call the {@code executor.initialize()} method as this will be invoked automatically when the bean is initialized.
What does the not a fully managed Spring bean mean? Which part of the bean not managed by Spring?