停止 Spring 集成框架中的任务



我想在给定事件后停止KeepAliveReceiver任务。我测试了以下解决方案,但没有一个有效 1) 发送 keepAliveReceiver.stop() 来控制,但 2) 实现生命周期并调用 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>
  1. 控制网关发送一条带有空命令的消息;-)

  2. "杀死"您的<control-bus>并将其更改为

    <outbound-channel-adapter channel="stopSchedulerChannel" expression="@myScheduler.shutdown()">

  3. 并添加 <channel id="stopSchedulerChannel"> <dispatcher task-executor="executor"/> </channel>

  4. 并配置适当的执行器 Bean

你的问题是希望停止他自己的任务。从另一端<control-bus>仅允许在SmartLifecycle实现器上进行操作

最新更新