From 9df98dafab8031893ebc8528298bc24d484c3fa6 Mon Sep 17 00:00:00 2001 From: straumat Date: Thu, 4 Jan 2024 23:09:45 +0100 Subject: [PATCH] Scheduler configuration added to update statistics --- .../configuration/SchedulerConfiguration.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 backend/servers/explorer-web/src/main/java/org/royllo/explorer/web/configuration/SchedulerConfiguration.java diff --git a/backend/servers/explorer-web/src/main/java/org/royllo/explorer/web/configuration/SchedulerConfiguration.java b/backend/servers/explorer-web/src/main/java/org/royllo/explorer/web/configuration/SchedulerConfiguration.java new file mode 100644 index 000000000..38d139be9 --- /dev/null +++ b/backend/servers/explorer-web/src/main/java/org/royllo/explorer/web/configuration/SchedulerConfiguration.java @@ -0,0 +1,45 @@ +package org.royllo.explorer.web.configuration; + +import lombok.RequiredArgsConstructor; +import org.royllo.explorer.core.util.base.BaseConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.scheduling.TaskScheduler; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; + +/** + * Scheduler configuration. + */ +@Profile("!scheduler-disabled") +@Configuration +@EnableScheduling +@RequiredArgsConstructor +public class SchedulerConfiguration extends BaseConfiguration { + + /** Termination delay in milliseconds (10 000 ms = 10 seconds). */ + private static final int TERMINATION_DELAY_IN_MILLISECONDS = 10_000; + + /** + * Configure the task scheduler. + * + * @return task scheduler + */ + @Bean + public TaskScheduler taskScheduler() { + ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); + scheduler.setWaitForTasksToCompleteOnShutdown(true); + scheduler.setAwaitTerminationMillis(TERMINATION_DELAY_IN_MILLISECONDS); + scheduler.setThreadNamePrefix("royllo-process-"); + scheduler.setErrorHandler(throwable -> { + try { + logger.error("Error while processing requests: {}", throwable.getMessage()); + } catch (Exception exception) { + logger.error("Error while processing requests: {}", exception.getMessage()); + } + }); + return scheduler; + } + +}