1
나는 에 의해 관리되는 scheduled timer task
입니다. 얼마나 exception
RuntimeException
를 던지면 나는 kill it
에 방법을 생각할 수 없다. 그것은 영원히 계속됩니다. 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);
}
}
}
나는 길을 찾았는데, 이것이 이상적인지 확실하지 않습니다. 내 코드 업데이트 됨 –