我想在给定事件后停止 KeepAliveReceiver 任务。我测试了以下解决方案,但没有一个有效 1) 发送 keepAliveReceiver.stop() 来控制,但是,2) 实现 Lifecycle 并调用 stop() 3) 停止调度程序。任何想法如何从正在运行的任务中停止任务?
@MessageEndpoint
public class KeepAliveReceiver implements Runnable, LifeCycle {
private int limit;
@Autowired
private ControlBusGateway controlGateway; // sending messages to control Channel
@Autowired
private ThreadPoolTaskScheduler myScheduler;
@Override
public void run() {
...
if ( event ) {
LOGGER.debug( "FAILOVER! Starting messageReceiveRouter. ");
controlGateway.send( new GenericMessage<String>( "@keepAliveReceiver.stop()" ) );
// not allowed
myScheduler.shutdown();
// not working, the scheduler keeps starting the keepAliveReceiver
this.stop();
//not working
}
}
@Override
public void stop() {
LOGGER.debug( "STOPPED!");
}
和调度程序的xml定义:
<task:scheduler id="myScheduler" pool-size="10" />
<task:scheduled-tasks>
<task:scheduled ref="keepAliveReceiver" method="run" fixed-rate="500" />
</task:scheduled-tasks>