手记

ExamInitializeTask

package com.huihe.exam.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huihe.exam.core.Result;
import com.huihe.exam.mapper.ScheduleMapper;
import com.huihe.exam.service.OnlineTestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("com.huihe.exam.task.ExamInitializeTask")
@Scope("prototype")
public class ExamInitializeTask extends AbstractTask {

    private final Logger log = LoggerFactory.getLogger(ExamInitializeTask.class);

    @Autowired
    OnlineTestService service;
    @Autowired
    ScheduleMapper mapper;

    public ExamInitializeTask(){
        super();
    }

    @Override
    public void run(){
        try {
            JSONObject param = JSON.parseObject(getTask().getParam());
            Result result = service.cacheQuestion(param.getString("id"), param.getString("examType"));
            if(result.getCode() == 200){
                success();
            } else {
                error();
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            error();
        }
    }

    @Override
    public void doing(){
        log.info("【{}:{}】任务开始执行...", this.getTask().getTaskName(), this.getTask().getId());
        if(this.getTask().getExecTm() == 1){
            mapper.setTaskFlag(TASK_DOING, this.getTask().getId());
        } else if(this.getTask().getExecTm() > 1) {
            mapper.setTaskFlag(TASK_WAITING, this.getTask().getId());
        }
    }

    @Override
    public void success(){
        SchedulerTask task = this.getTask();
        String taskId = task.getId();
        log.info("【{}:{}】任务执行成功...", this.getTask().getTaskName(), this.getTask().getId());
        mapper.setTaskFlag(TASK_SUCCESS, taskId);
        stop();
    }

    @Override
    public void error(){
        log.info("【{}:{}】任务执行出错...", this.getTask().getTaskName(), this.getTask().getId());
        mapper.setTaskFlag(TASK_ERROR, this.getTask().getId());
        stop();
    }
}


1人推荐
随时随地看视频
慕课网APP