在Spring Integration框架中停止任务

时间:2013-09-20 07:50:50

标签: spring task spring-integration

我想在给定事件后停止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 个答案:

答案 0 :(得分:1)

  1. 使用空命令发送至 controlGateway 消息; - )

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

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

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

  4. 并配置适当的执行程序 bean
  5. 你的问题是希望停止自己的任务。另一方<control-bus>仅允许SmartLifecycle实施者

    上的操作