任务同步

任务同步主要是将数据库中的启动状态的任务同步到时间轮中等待调度。

任务同步分为两个阶段,一个是任务全量同步,另一个是任务增量同步。

任务全量同步

初始化器装载时间轮这个过程其实就是任务的全量同步。

任务增量同步

任务的增量同步就是当你启动一个任务的时候,手动将任务放入到时间轮这就是任务的增量同步。

@Service
public class OpenJobServiceImpl extends ServiceImpl<OpenJobMapper, OpenJobDO> implements OpenJobService, ScheduleTaskService {

    private final TimeWheel timeWheel;
    private final OpenJobMapper openJobMapper;
    private final OpenJobClientService openJobClientService;
    private final ScheduleTaskExecutor scheduleTaskExecutor;

    public OpenJobServiceImpl(TimeWheel timeWheel,
                              OpenJobMapper openJobMapper,
                              OpenJobClientService openJobClientService,
                              ScheduleTaskExecutor scheduleTaskExecutor) {
        this.timeWheel = timeWheel;
        this.openJobClientService = openJobClientService;
        this.openJobMapper = openJobMapper;
        this.scheduleTaskExecutor = scheduleTaskExecutor;
    }

    //~~~

    @Override
    public boolean start(Long id) {
        OpenJobDO openJobDO = openJobMapper.selectById(id);
        openJobDO.setStatus(CommonStatusEnum.YES.getValue());
        openJobDO.setUpdateTime(LocalDateTime.now());
        openJobMapper.updateById(openJobDO);
        ScheduleTask scheduleTask = createScheduleTask(openJobDO);
        timeWheel.put(scheduleTask.getTaskId(), scheduleTask.getCronExpression());
        return Boolean.TRUE;
    }

    //~~~
}