}
@Override
public synchronized void open(String taskId) {
try {
TaskDefinition taskDefinition = taskMap.get(taskId);
/* 查看任务是否已启动 */
if (Schedule.RUNNING == this.status
&& (taskDefinition == null || !taskDefinition.isClosed())){
return;
}
log.debug("openHeart = "+config.getOpenHeart()+" , schedule status = "+this.status);
/*
* 如果调度器使用心跳监控,则任务必须在所有调度器等待就绪后才能开启
*
* 所以将状态改为已暂停
*/
if (config.getOpenHeart()
&& this.status != RUNNING
&& TaskDefinition.CLOSED != taskDefinition.getStatus()) {
taskDefinition.setStatus(TaskDefinition.PAUSED);
return;
}
/* 启动任务 */
Handler handler = taskDefinition.getHandler();
if(handler == null){
log.error("There is no handler to process!");
return;
}
handler.setConfig(config);
ScheduledFuture<?> future = service.scheduleWithFixedDelay(handler, 0, taskDefinition.getFrequency(), TimeUnit.MILLISECONDS);
taskDefinition.setFuture(future);
taskDefinition.setStatus(TaskDefinition.OPENED); // 修改业务状态为运行中
log.info("Task {"+taskId+"} is opened!");
} catch (Exception e) {
log.error("Failed to open task", e);
}
}