csviri
csviri

Reputation: 1229

Automatically Cancel Spring Task on Exception

I have a Spring Task defined on in spring app context xml:

<task:scheduler id="myScheduler" pool-size="1"/>

<task:scheduled-tasks scheduler="myScheduler">
    <task:scheduled ref="MyClass" method="myMethod" fixed-delay="3000"/>
</task:scheduled-tasks>

So, how can I implement to stop further execution of the tasks in case of an Exception, either in xml or in code by catching the Exception?

Upvotes: 2

Views: 4021

Answers (1)

abalogh
abalogh

Reputation: 8281

I don't think this is solvable using scheduled-tasks, may be wrong of course.

There's an alternative though, config:

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

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

<bean id="scheduledTaskErrorHandler" class="boo.ScheduledTaskErrorHandler" />

And the errorHandler:

public class ScheduledTaskErrorHandler implements ErrorHandler {

@Override
public void handleError(Throwable t) {
        // do something, like shutdown the scheduler
}
}

Upvotes: 2

Related Questions