In xxL-job_info, the update_match_state and update_match_step_state actuators control the event status and event phase status respectively.

The event information required by the xxL-job is inserted when the event is created

@apiOperation (value=" add ", notes=" add ")
@PostMapping("/save")
@Authority("match:info:add")
@tracker (value = "update ",operation =" update ")
public CommonResult save(@RequestBody String jsonStr ){
    MatchInfoAndStepVO matchInfoAndStepVO = JSONObject.parseObject(jsonStr, MatchInfoAndStepVO.class);
    MatchInformationRequest matchInformationRequest=matchInfoAndStepVO.getMatchInformationRequest();
    String msg = matchInformationService.save(matchInformationRequest, matchInfoAndStepVO);
    try {
        this.updateMatchByXxl(matchInfoAndStepVO);
    }catch (ParseException e){
        throw new BizException("Event scheduled task CRON expression parsing exception");
    }
Copy the code

With the updateMatchByXxl() method, the code is as follows, the race status info registration start time is TriggerNextTime, and the earliest race status stage start time is TriggerNextTime

private void updateMatchByXxl(MatchInfoAndStepVO matchInfoAndStepVO) throws ParseException {
    List<MatchInformation> matchInformations = matchInformationMapper.findByMatchName(matchInfoAndStepVO.getMatchInformationRequest().getMatchName());
    if(matchInformations ! =null && matchInformations.size() == 1){
        MatchInformation matchInformation = matchInformations.get(0);
        long registerStartTime = matchInformation.getRegisterStartTime().getTime();
        String jobDesc = "Update tournament Status Task"+matchInformation.getMatchId();
        // Update the event
        insertIntoXxlJob(matchInformation.getMatchId(),registerStartTime,jobDesc,Constant.UPDATE_MATCH_STATE);
        / / update the step
        ArrayList<Long> stepStateTriggerList = getStepStateTriggerList(matchInfoAndStepVO);
        Long minTime = Collections.min(stepStateTriggerList);
        insertIntoXxlJob(matchInformation.getMatchId(),minTime,"Update tournament Status"+matchInformation.getMatchId(), Constant.UPDATE_MATCH_STEP_STATE); }}Copy the code

InsertIntoXxlJob insertIntoXxlJob insertIntoXxlJob insertIntoXxlJob insertIntoXxlJob insertIntoXxlJob insertIntoXxlJob insertIntoXxlJob insertIntoXxlJob

private void insertIntoXxlJob(Integer matchId,Long nextTime,String jobDesc,String handler) throws ParseException {
    XxlJobInfo xxlJobInfo = new XxlJobInfo();
    xxlJobInfo.setJobGroup(1);
    xxlJobInfo.setJobCron(CronUtil.getCronByOnce(new Date(nextTime)));
    xxlJobInfo.setJobDesc(jobDesc);
    xxlJobInfo.setAddTime(LocalDateTime.now());
    xxlJobInfo.setUpdateTime(LocalDateTime.now());
    xxlJobInfo.setAuthor("JW");
    xxlJobInfo.setExecutorRouteStrategy("FIRST");
    xxlJobInfo.setExecutorHandler(handler);
    xxlJobInfo.setExecutorParam(matchId.toString());
    xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
    xxlJobInfo.setExecutorTimeout(1000);
    xxlJobInfo.setExecutorFailRetryCount(2);
    xxlJobInfo.setGlueType("BEAN");
    xxlJobInfo.setGlueRemark("GLUE code initialization");
    xxlJobInfo.setGlueUpdatetime(LocalDateTime.now());
    xxlJobInfo.setTriggerStatus(1);
    xxlJobInfo.setTriggerLastTime(0L);
    xxlJobInfo.setTriggerNextTime(nextTime);
    xxlJobInfoMapper.insert(xxlJobInfo);
}
Copy the code

The following is the INFO database

Two Handle actuators are shown below

@XxlJob(Constant.UPDATE_MATCH_STATE)
public ReturnT<String> updateMatchState(String param) throws Exception{
    XxlJobLogger.log("Start updating tournament status.");
    platformJobManager.updateMatchState(param);
    XxlJobLogger.log("Event status update completed.");
    return ReturnT.SUCCESS;
}

@XxlJob(Constant.UPDATE_MATCH_STEP_STATE)
public ReturnT<String> updateMatchStepState(String param) throws Exception{
    XxlJobLogger.log("Start updating tournament status");
    platformJobManager.updateMatchStepState(param);
    XxlJobLogger.log("Event phase status update completed.");
    return ReturnT.SUCCESS;
Copy the code

Take the event state handle as an example, query the info information of the corresponding event and actuator in the INFO, query the time of registration start, end, start and end of the game, and then compare with the current time, which can be divided into five stages. Determine the status, generate cron (a representation of a point in time), TriggerNextTime, TriggerStatus(1) (because the status field defaults to 0, or 0 if not set when updating the info insert, the tool stops and does not start at the next stage).

MatchState handle the following

@Override
public void updateMatchState(String param) throws ParseException {
    MatchInformation matchInformation = matchInformationMapper.selectByPrimaryKey(Integer.valueOf(param));
    XxlJobInfo xxlJobInfo = xxlJobInfoMapper.selectOne(new LambdaQueryWrapper<XxlJobInfo>().eq(XxlJobInfo::getExecutorParam,param)
    .eq(XxlJobInfo::getExecutorHandler,Constant.UPDATE_MATCH_STATE));
    if (matchInformation == null){
        log.error("Match_id = {}",Integer.valueOf(param));
        return;
    }
    long registerStartTime = matchInformation.getRegisterStartTime().getTime();
    long registerEndTime = matchInformation.getRegisterEndTime().getTime();
    long matchStartTime = matchInformation.getMatchStartTime().getTime();
    long matchEndTime = matchInformation.getMatchEndTime().getTime();
    long now = System.currentTimeMillis();
    if (now < registerStartTime){
        matchInformation.setMatchState(1);
        xxlJobInfo.setJobCron(CronUtil.getCronByOnce(new Date(registerStartTime)));
        xxlJobInfo.setTriggerNextTime(registerStartTime);
        xxlJobInfo.setTriggerStatus(1);
    }
    if (now >= registerStartTime && now < registerEndTime){
        matchInformation.setMatchState(2);
        xxlJobInfo.setJobCron(CronUtil.getCronByOnce(new Date(registerEndTime)));
        xxlJobInfo.setTriggerNextTime(registerEndTime);
        xxlJobInfo.setTriggerStatus(1);
    }
    if (now >= registerEndTime && now < matchStartTime){
        matchInformation.setMatchState(3);
        xxlJobInfo.setJobCron(CronUtil.getCronByOnce(new Date(matchStartTime)));
        xxlJobInfo.setTriggerNextTime(matchStartTime);
        xxlJobInfo.setTriggerStatus(1);
    }
    if (now >= matchStartTime && now < matchEndTime){
        matchInformation.setMatchState(4);
        xxlJobInfo.setJobCron(CronUtil.getCronByOnce(new Date(matchEndTime)));
        xxlJobInfo.setTriggerNextTime(matchEndTime);
        xxlJobInfo.setTriggerStatus(1);
    }
    if (now >= matchEndTime){
        matchInformation.setMatchState(5);
        xxlJobInfo.setJobCron(CronUtil.getCronByOnce(new Date(matchEndTime)));
        xxlJobInfo.setTriggerNextTime(matchEndTime);
        xxlJobInfo.setTriggerStatus(0);
    }
    xxlJobInfo.setUpdateTime(LocalDateTime.now());
    xxlJobInfo.setTriggerLastTime(xxlJobInfo.getTriggerNextTime());
    matchInformationMapper.updateById(matchInformation);
    xxlJobInfoMapper.updateById(xxlJobInfo);
}
Copy the code

MatchStepState handle the following

@Override
public void updateMatchStepState(String param) throws ParseException {
    List<MatchStep> matchStep = matchStepMapper.selectList(new LambdaQueryWrapper<MatchStep>().eq(MatchStep::getMatchId,Integer.valueOf(param)));
    XxlJobInfo xxlJobInfo = xxlJobInfoMapper.selectOne(new LambdaQueryWrapper<XxlJobInfo>().eq(XxlJobInfo::getExecutorParam,param)
            .eq(XxlJobInfo::getExecutorHandler,Constant.UPDATE_MATCH_STEP_STATE));
    if (matchStep == null || matchStep.size() == 0){
        log.error("Match_id = {}",Integer.valueOf(param));
        return;
    }
    long now = System.currentTimeMillis();
    for (MatchStep step : matchStep){
        long startTime = step.getStartTime().getTime();
        long endTime = step.getEndTime().getTime();
        if (step.getMatchStep().equals(3)) {continue;
        }
        if (now < startTime){
            step.setMatchStepState(1);
            xxlJobInfo.setJobCron(CronUtil.getCronByOnce(new Date(startTime)));
            xxlJobInfo.setTriggerNextTime(startTime);
            xxlJobInfo.setTriggerStatus(1);
        }
        if (now >= startTime && now < endTime){
            step.setMatchStepState(2);
            xxlJobInfo.setJobCron(CronUtil.getCronByOnce(new Date(endTime)));
            xxlJobInfo.setTriggerNextTime(endTime);
            xxlJobInfo.setTriggerStatus(1);
        }
        if (now >= endTime){
            step.setMatchStepState(3);
            xxlJobInfo.setJobCron(CronUtil.getCronByOnce(new Date(endTime)));
            xxlJobInfo.setTriggerNextTime(endTime);
            // Stop the trigger after the last stage
            if(matchStep.size() - 1 == matchStep.indexOf(step)){
                xxlJobInfo.setTriggerStatus(0);
            }else {
                xxlJobInfo.setTriggerStatus(1);
            }
        }
        xxlJobInfo.setUpdateTime(LocalDateTime.now());
        xxlJobInfo.setTriggerLastTime(xxlJobInfo.getTriggerNextTime());
        matchStepMapper.updateById(step);
        xxlJobInfoMapper.updateById(xxlJobInfo);
    }
Copy the code

The general logic is: When the event is created and updated, the corresponding information of the xxL-info database will be updated at the same time (cron and TriggerNextTime are the next status update time, and the two Handles control the event status and event stage status respectively). When the corresponding time is reached, Handle actuators in the xxl-job component perform their respective tasks to update the status and refresh the info required for the next update.

The following changes are for the event

@apiOperation (value=" modify ", notes=" modify ")
@PostMapping("/update")
@Authority("match:info:update")
@tracker (value = "update ",operation =" update ") @tracker (value = "update ",operation =" update ")
public CommonResult update(@RequestBody String jsonStr){
    MatchInfoAndStepVO matchInfoAndStepVO = JSONObject.parseObject(jsonStr, MatchInfoAndStepVO.class);
    MatchInformationRequest matchInformationRequest=matchInfoAndStepVO.getMatchInformationRequest();
    List<MatchStepRequest> matchStepRequests = matchInfoAndStepVO.getMatchStepRequests();
    matchInformationService.update(matchInformationRequest,matchStepRequests);
    try {
        this.updateXxl(matchInfoAndStepVO);
    }catch (ParseException e){
        throw new BizException("Event scheduled task CRON expression parsing exception");
    }
    return ResultUtil.success(ResultCode.SUCCESS);
}
Copy the code

Updatexxl, query the modified event information, compare the updated event time with the next update time, and modify the next update time.

/** * Updates the time when a scheduled task is triggered */
private void updateXxl(MatchInfoAndStepVO matchInfoAndStepVO) throws ParseException {
    MatchInformation matchInformation = null;
    if(matchInfoAndStepVO.getMatchInformationRequest().getMatchId() ! =null){
        matchInformation = matchInformationMapper.selectByPrimaryKey(matchInfoAndStepVO.getMatchInformationRequest().getMatchId());
    }else {
        matchInformation = matchInformationMapper.findByMatchName(matchInfoAndStepVO.getMatchInformationRequest().getMatchName()).get(0);
    }
    ArrayList<Long> matchStateTriggerList = getMatchStateTriggerList(matchInfoAndStepVO);
    ArrayList<Long> stepStateTriggerList = getStepStateTriggerList(matchInfoAndStepVO);

    long now = System.currentTimeMillis();

    XxlJobInfo match = xxlJobInfoMapper.selectOne(newLambdaQueryWrapper<XxlJobInfo>().eq(XxlJobInfo::getExecutorParam,matchInformation.getMatchId()).eq(XxlJobInfo::getExecut orHandler,Constant.UPDATE_MATCH_STATE)); XxlJobInfo matchStep = xxlJobInfoMapper.selectOne(newLambdaQueryWrapper<XxlJobInfo>().eq(XxlJobInfo::getExecutorParam,matchInformation.getMatchId()).eq(XxlJobInfo::getExecut orHandler,Constant.UPDATE_MATCH_STEP_STATE));long thisMatchTime = match.getTriggerLastTime();
    long thisMatchStepTime =matchStep.getTriggerNextTime();

    for (Long aLong : matchStateTriggerList) {
        if(now < aLong && aLong < match.getTriggerNextTime()){ match.setTriggerNextTime(aLong); }}if(match.getTriggerNextTime() ! = thisMatchTime){ match.setJobCron(CronUtil.getCronByOnce(new Date(match.getTriggerNextTime())));
        xxlJobInfoMapper.updateById(match);
    }

    for (Long aLong : stepStateTriggerList) {
        if(now < aLong && aLong < matchStep.getTriggerNextTime()){ matchStep.setTriggerNextTime(aLong); }}if(matchStep.getTriggerNextTime() ! = thisMatchStepTime){ matchStep.setJobCron(CronUtil.getCronByOnce(new Date(matchStep.getTriggerNextTime())));
        xxlJobInfoMapper.updateById(matchStep);
    }
Copy the code