取消由弹簧容器管理的计时器

时间:2014-09-05 19:51:00

标签: java spring timer scheduled-tasks timertask

我有一个由scheduled timer task管理的Spring container。一旦kill it抛出exception RuntimeException,我就无法找到Timer object的方法。它一直在运行。有没有办法访问code并从 <bean id="myTimer" class="com.aspect.MyTimer"></bean> <bean id="timerTaskFactoryBean" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"> <property name="targetObject" ref="myTimer"></property> <property name="targetMethod" value="doSomething"></property> </bean> <bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask" ref="timerTaskFactoryBean"></property> <property name="period" value="5000"></property> </bean> <bean class="org.springframework.scheduling.timer.TimerFactoryBean" id="timerBean"> <property name="scheduledTimerTasks"> <list> <ref local="timerTask"></ref> </list> </property> </bean> 取消它?

这是我的代码:

Java class

public class MyTimer { public void doSomething(){ System.out.println("I am running..."); try{ throw new RuntimeException(); } catch(RuntimeException e){ ScheduledTimerTask timer = (ScheduledTimerTask) App.appContext.getBeanFactory().getBean("timerTask"); timer.getTimerTask().cancel(); System.out.println("Timer cancelled due to..." +e); } }

{{1}}

}

1 个答案:

答案 0 :(得分:0)

我猜你需要通过容器访问bean并取消它。