Code: Select all
@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();
};
}
}
< /code>
Und ich füge die @aSync -Annotation für eine andere Klasse 'Methode hinzu. < /p>
@Override
public Future getResult(String name);
< /code>
Aber wenn ich die GetResult () < /code> -Methode aufrufe, meldet es keine Bean mit dem Namen 'Taskexecutor' verfügbar: Keine passende Ausführerin Bean, die für Qualifier 'Taskexecutor' - weder Qualifier -Match noch Bean -Name -Match! Seite: https: //www.baeldung.com/spring-async. Ich folge dieser Seite dieser Seite - entfernen Sie die @enableAsync
hinzu, wenn Sie eine vollständig verwaltete Bohne wünschen. Unter solchen Umständen ist es nicht mehr erforderlich, die Methode {@code executor.initialize () manuell aufzurufen, da diese automatisch aufgenommen wird, wenn die Bean initialisiert wird. Welcher Teil der Bohne wird nicht im Frühling verwaltet?