Skip to content

Error handling in scheduled tasks [SPR-7723] #12379

@spring-projects-issues

Description

@spring-projects-issues

Andriy Kulchytskyy opened SPR-7723 and commented

ThreadPoolTaskScheduler has errorHandler property for handling exceptions however when using @Scheduled annotation exceptions are not catched.

<task:annotation-driven scheduler="myThreadPoolTaskScheduler"  />

<bean id="myThreadPoolTaskScheduler" class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler">
        <property name="poolSize" value="10" />
        <property name="errorHandler" ref="scheduledTaskErrorHandler" />
</bean>

<bean id="scheduledTaskErrorHandler" class="com.intteh.spc.services.scheduled.ScheduledTaskErrorHandler" />

my error handler:

 public class ScheduledTaskErrorHandler implements ErrorHandler{

	private static Logger log = Logger.getLogger(ScheduledTaskErrorHandler.class); 
	
	@Override
	public void handleError(Throwable t) {
		log.debug("Error occurred while executing scheduled task");
	}
}

test exception:

public class NewsletterProcessQueueScheduledService {
...

	@Scheduled(fixedDelay=1*60*1000)
	private void processQueue() throws Exception{
		throw new Exception("test");
	}
}

console output:

ERROR: org.springframework.scheduling.support.MethodInvokingRunnable - Invocation of method 'processQueue' on target class [class com.companyname.services.scheduled.NewsletterProcessQueueScheduledService] failed
java.lang.Exception: test
	at com.companyname.services.scheduled.NewsletterProcessQueueScheduledService.processQueue(NewsletterProcessQueueScheduledService.java:20)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
	at org.springframework.scheduling.support.MethodInvokingRunnable.run(MethodInvokingRunnable.java:65)
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:51)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
	at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317)
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)

The error is logged, but my handling code is not executed.

class DelegatingErrorHandlingRunnable has correct handling code

public class DelegatingErrorHandlingRunnable implements Runnable {
   ...
    public void run() {
        try {
            this.delegate.run();
        }
        catch (Throwable ex) {
            this.errorHandler.handleError(ex);
        }
    }
}

but underlying class MethodInvokingRunnable suppress all exceptions

public class MethodInvokingRunnable extends ArgumentConvertingMethodInvoker
	public void run() {
		try {
			invoke();
		}
		catch (InvocationTargetException ex) {
			logger.error(getInvocationFailureMessage(), ex.getTargetException());
			// Do not throw exception, else the main loop of the scheduler might stop!
		}
		catch (Throwable ex) {
			logger.error(getInvocationFailureMessage(), ex);
			// Do not throw exception, else the main loop of the scheduler might stop!
		}
	}

Affects: 3.0.4

Reference URL: http://forum.springsource.org/showthread.php?t=97327

Referenced from: commits 0d70e08

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions