How to customize the exception handling for @Scheduled
annotation from spring ?
I have Cron jobs which will be triggered in the server (Tomcat 6) and when any exceptions occur I need to do some handling.
- Spring version 3.2
- Tomcat Server 6
How to customize the exception handling for @Scheduled
annotation from spring ?
I have Cron jobs which will be triggered in the server (Tomcat 6) and when any exceptions occur I need to do some handling.
You could implement and register an ErrorHandler
for the ThreadPoolTaskScheduler
that is used for your scheduling annotations.
<task:annotation-driven scheduler="yourThreadPoolTaskScheduler" />
<bean id="yourThreadPoolTaskScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
<property name="poolSize" value="5" />
<property name="errorHandler" ref="yourScheduledTaskErrorHandler" />
</bean>
<bean id="yourScheduledTaskErrorHandler"
class="com.example.YourScheduledTaskErrorHandler"/>
<task:scheduler>
, the lines above replace it. You replace your Task Scheduler with this Task:Annotation Driven + 2 beans. Afterwards, change your <task:scheduled-tasks scheduler="yourThreadPoolTaskScheduler">
to point to this new scheduler name. –
Encumbrance If you want to use Java Config you will need to create configuration implementing SchedulingConfigurer
@EnableScheduling
@Configuration
class SchedulingConfiguration implements SchedulingConfigurer {
private final Logger logger = LoggerFactory.getLogger(getClass());
private final ThreadPoolTaskScheduler taskScheduler;
SchedulingConfiguration() {
taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setErrorHandler(t -> logger.error("Exception in @Scheduled task. ", t));
taskScheduler.setThreadNamePrefix("@scheduled-");
taskScheduler.initialize();
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler);
}
}
You can modify error handler for your needs. Here I only log a message.
Don't forget to call taskScheduler.initialize();. Without it you'll get:
java.lang.IllegalStateException: ThreadPoolTaskScheduler not initialized
You could implement and register an ErrorHandler
for the ThreadPoolTaskScheduler
that is used for your scheduling annotations.
<task:annotation-driven scheduler="yourThreadPoolTaskScheduler" />
<bean id="yourThreadPoolTaskScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
<property name="poolSize" value="5" />
<property name="errorHandler" ref="yourScheduledTaskErrorHandler" />
</bean>
<bean id="yourScheduledTaskErrorHandler"
class="com.example.YourScheduledTaskErrorHandler"/>
<task:scheduler>
, the lines above replace it. You replace your Task Scheduler with this Task:Annotation Driven + 2 beans. Afterwards, change your <task:scheduled-tasks scheduler="yourThreadPoolTaskScheduler">
to point to this new scheduler name. –
Encumbrance Why not wrap your business logic and do a simple try catch in your @schedule method. Then you can log or take whatever action is necessary for failure cases.
@Scheduled(cron = "${schedulerRate}")
public void scheduledJob() {
try {
businessLogicService.doBusinessLogic();
} catch (Exception e) {
log.error(e);
}
}
© 2022 - 2024 — McMap. All rights reserved.
@Scheduled
method? – Birchfield