0

我想在给定事件后停止 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>
4

1 回答 1

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

您的问题是希望停止自己的任务。从另一端<control-bus>只允许对SmartLifecycle实现者进行操作

于 2013-09-20T12:19:15.503 回答