开发者

Automatically Cancel Spring Task on Exception

开发者 https://www.devze.com 2023-03-16 00:32 出处:网络
I have a Spring Task defined on in spring app context xml: <task:scheduler id=\"myScheduler\" pool-size=\"1\"/>

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 开发者_StackOverflow社区to stop further execution of the tasks in case of an Exception, either in xml or in code by catching the Exception?


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
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消