一、实体类
1、ScheduleJobs
@TableName("schedule_jobs")
@Data
public class ScheduleJobs extends BaseEntity {
/**
* 任务调度参数key
*/
public static final String JOB_PARAM_KEY = "JOB_PARAM_KEY";
/**
* spring bean名称
*/
@NotBlank(message = "bean名称不能为空")
private String beanName;
/**
* 方法名
*/
@NotBlank(message = "方法名称不能为空")
private String methodName;
/**
* 参数
*/
private String params;
/**
* cron表达式
*/
@NotBlank(message = "cron表达式不能为空")
private String cronExpression;
/**
* 任务状态
*/
private Integer status;
/**
* 备注
*/
private String remark;
}
2、ScheduleJobLog
@TableName("schedule_job_log")
@Data
public class ScheduleJobLog extends BaseEntity {
/**
* 任务id
*/
private String jobId;
/**
* spring bean名称
*/
private String beanName;
/**
* 方法名
*/
private String methodName;
/**
* 参数
*/
private String params;
/**
* 任务状态 0:成功 1:失败
*/
private Integer status;
/**
* 失败信息
*/
private String error;
/**
* 耗时(单位:毫秒)
*/
private Integer times;
}
二、数据层类
1、ScheduleJobMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hrz.task.entity.ScheduleJobs;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.Map;
@Mapper
@Repository
public interface ScheduleJobMapper extends BaseMapper {
/**
* 批量更新状态
*/
int updateBatch(Map map);
}
mapper文件:
update task_schedule_jobs set status = #{status} where id in
#{id}
2、ScheduleJobLogMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hrz.task.entity.ScheduleJobLog;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface ScheduleJobLogMapper extends BaseMapper {
}
三、业务层类
1、ScheduleJobsService
import com.baomidou.mybatisplus.extension.service.IService;
import com.hrz.common.core.mapper.PageUtils;
import com.hrz.common.exception.HrzException;
import com.hrz.task.entity.ScheduleJobs;
import java.util.Map;
public interface ScheduleJobsService extends IService {
PageUtils queryPage(Map params) throws HrzException;
/**
* 保存定时任务
*/
void saveScheduleJob(ScheduleJobs scheduleJob) throws HrzException;
/**
* 更新定时任务
*/
void update(ScheduleJobs scheduleJob) throws HrzException;
/**
* 批量删除定时任务
*/
void deleteBatch(String[] jobIds) throws HrzException;
/**
* 批量更新定时任务状态
*/
int updateBatch(String[] jobIds, int status);
/**
* 立即执行
*/
void run(String[] jobIds) throws HrzException;
/**
* 暂停运行
*/
void pause(String[] jobIds) throws HrzException;
/**
* 恢复运行
*/
void resume(String[] jobIds) throws HrzException;
}
2、ScheduleJobLogService
import com.baomidou.mybatisplus.extension.service.IService;
import com.hrz.common.core.mapper.PageUtils;
import com.hrz.common.exception.HrzException;
import com.hrz.task.entity.ScheduleJobLog;
import java.util.Map;
public interface ScheduleJobLogService extends IService {
/**
* 分页查询
* @param params
* @return
* @throws HrzException
*/
PageUtils queryPage(Map params) throws HrzException;
/**
* 添加日志
* @param scheduleJobLog
* @return
*/
boolean saveLog(ScheduleJobLog scheduleJobLog);
}
3、ScheduleJobsServiceImpl
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrz.common.constant.GlobalConstant;
import com.hrz.common.core.mapper.PageUtils;
import com.hrz.common.core.mapper.Query;
import com.hrz.common.exception.HrzException;
import com.hrz.task.entity.ScheduleJobs;
import com.hrz.task.mapper.ScheduleJobMapper;
import com.hrz.task.service.ScheduleJobsService;
import com.hrz.task.utils.ScheduleUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.util.*;
@Slf4j
@Service
public class ScheduleJobsServiceImpl extends ServiceImpl implements ScheduleJobsService {
//调度器对象
@Autowired
private Scheduler scheduler;
/**
* 项目启动时,初始化定时器
*/
@PostConstruct
public void init() throws HrzException {
//查询所有
List scheduleJobsList = this.list();
for (ScheduleJobs scheduleJobs : scheduleJobsList) {
log.info("调度器:" + scheduler);
CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJobs.getId());
//如果不存在,则创建
if (cronTrigger == null) {
ScheduleUtils.createScheduleJob(scheduler, scheduleJobs);
} else {
ScheduleUtils.updateScheduleJob(scheduler, scheduleJobs);
}
}
}
@Override
public PageUtils queryPage(Map params) throws HrzException {
String beanName = (String) params.get("beanName");
//分页查询
IPage ipage = baseMapper.selectPage(
new Query(params).getPage(),
new QueryWrapper().like(StringUtils.isNotBlank(beanName), "bean_name", beanName));
return new PageUtils(ipage);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void saveScheduleJob(ScheduleJobs scheduleJobs) throws HrzException {
scheduleJobs.setCreateTime(new Date());
scheduleJobs.setStatus(GlobalConstant.ScheduleStatus.NORMAL.getValue());
this.save(scheduleJobs);
ScheduleUtils.createScheduleJob(scheduler, scheduleJobs);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void update(ScheduleJobs scheduleJob) throws HrzException {
ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
this.updateById(scheduleJob);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteBatch(String[] jobIds) throws HrzException {
for (String jobId : jobIds) {
ScheduleUtils.deleteScheduleJob(scheduler, jobId);
}
//删除数据
baseMapper.deleteBatchIds(Arrays.asList(jobIds));
}
@Override
public int updateBatch(String[] jobIds, int status) {
Map map = new HashMap<>();
map.put("list", jobIds);
map.put("status", status);
return baseMapper.updateBatch(map);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void run(String[] jobIds) throws HrzException {
for (String jobId : jobIds) {
ScheduleUtils.run(scheduler, baseMapper.selectById(jobId));
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void pause(String[] jobIds) throws HrzException {
for (String jobId : jobIds) {
ScheduleUtils.pauseJob(scheduler, jobId);
}
//批量更新
updateBatch(jobIds, GlobalConstant.ScheduleStatus.PAUSE.getValue());
}
@Override
@Transactional(rollbackFor = Exception.class)
public void resume(String[] jobIds) throws HrzException {
for (String jobId : jobIds) {
ScheduleUtils.resumeJob(scheduler, jobId);
}
updateBatch(jobIds, GlobalConstant.ScheduleStatus.NORMAL.getValue());
}
}
4、ScheduleJobLogServiceImpl
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrz.common.core.mapper.PageUtils;
import com.hrz.common.core.mapper.Query;
import com.hrz.common.exception.HrzException;
import com.hrz.common.utils.Snowflake;
import com.hrz.task.entity.ScheduleJobLog;
import com.hrz.task.mapper.ScheduleJobLogMapper;
import com.hrz.task.service.ScheduleJobLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
@Slf4j
@Service("scheduleJobLogService")
public class ScheduleJobLogServiceImpl extends ServiceImpl implements ScheduleJobLogService {
@Autowired
private ScheduleJobLogMapper scheduleJobLogMapper;
@Override
public PageUtils queryPage(Map params) throws HrzException {
String jobId = (String)params.get("jobId");
//分页查询
IPage ipage = scheduleJobLogMapper.selectPage(
new Query(params).getPage(),
new QueryWrapper().like(StringUtils.isNotBlank(jobId),"job_id", jobId));
return new PageUtils(ipage);
}
@Override
public boolean saveLog(ScheduleJobLog scheduleJobLog) {
try {
scheduleJobLog.setId(Snowflake.getId());
scheduleJobLog.setCreateTime(new Date());
if(this.save(scheduleJobLog)) {
return true;
}
return false;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
四、控制层
1、ScheduleJobController
import com.hrz.common.base.Result;
import com.hrz.common.core.mapper.PageUtils;
import com.hrz.common.exception.HrzException;
import com.hrz.common.utils.Snowflake;
import com.hrz.task.entity.ScheduleJobs;
import com.hrz.task.service.ScheduleJobsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.Map;
@RestController
@RequestMapping(value ="/schedule",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Slf4j
public class ScheduleJobController {
@Autowired
private ScheduleJobsService scheduleJobsService;
/**
* 定时任务列表
*/
@RequestMapping(value ="/list", method = RequestMethod.GET)
public Result list(@RequestParam Map params) {
Result result = Result.fail();
PageUtils page = null;
try {
page = scheduleJobsService.queryPage(params);
result.setData(page);
return result.resultSuccess();
} catch (HrzException e) {
e.printStackTrace();
return result;
}
}
/**
* 定时任务信息
*/
@RequestMapping(value ="/info",method = RequestMethod.GET)
public Result info(String id) {
Result result = Result.fail();
ScheduleJobs scheduleJobs = scheduleJobsService.getById(id);
result.setData(scheduleJobs);
return result.resultSuccess();
}
/**
* 保存定时任务
*/
@RequestMapping(value ="/save",method = RequestMethod.POST)
public Result save(@RequestBody ScheduleJobs scheduleJobs) throws Exception {
Result result = Result.fail();
scheduleJobs.setId(Snowflake.getId());
scheduleJobs.setCreateTime(new Date());
scheduleJobsService.save(scheduleJobs);
result.setData(scheduleJobs);
return result.resultSuccess();
}
/**
* 修改定时任务
*/
@RequestMapping(value ="/update",method = RequestMethod.POST)
public Result update(@RequestBody ScheduleJobs scheduleJobs){
Result result = Result.fail();
log.info(scheduleJobs.getId()+"任务ID");
try {
scheduleJobsService.update(scheduleJobs);
return result.resultSuccess();
} catch (HrzException e) {
e.printStackTrace();
return result;
}
}
/**
* 删除定时任务
*/
@RequestMapping(value ="/delete",method = RequestMethod.POST)
public Result delete(@RequestBody String[] ids){
Result result = Result.fail();
try {
scheduleJobsService.deleteBatch(ids);
return result.resultSuccess();
} catch (HrzException e) {
e.printStackTrace();
return result;
}
}
/**
* 立即执行任务
*/
@RequestMapping(value ="/run",method = RequestMethod.POST)
public Result run(@RequestBody String[] ids){
Result result = Result.fail();
try {
scheduleJobsService.run(ids);
return result.resultSuccess();
} catch (HrzException e) {
e.printStackTrace();
return result;
}
}
/**
* 暂停定时任务
*/
@RequestMapping(value ="/pause",method = RequestMethod.POST)
public Result pause(@RequestBody String[] ids){
Result result = Result.fail();
try {
scheduleJobsService.pause(ids);
return result.resultSuccess();
} catch (HrzException e) {
e.printStackTrace();
return result;
}
}
/**
* 恢复定时任务
*/
@RequestMapping(value ="/resume",method = RequestMethod.POST)
public Result resume(@RequestBody String[] ids){
Result result = Result.fail();
try {
scheduleJobsService.resume(ids);
return result.resultSuccess();
} catch (HrzException e) {
e.printStackTrace();
return result;
}
}
}
2、ScheduleJobLogController
import com.hrz.common.base.Result;
import com.hrz.common.core.mapper.PageUtils;
import com.hrz.common.exception.HrzException;
import com.hrz.task.entity.ScheduleJobLog;
import com.hrz.task.service.ScheduleJobLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping(value ="/scheduleLog",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ScheduleJobLogController {
@Autowired
private ScheduleJobLogService scheduleJobLogService;
/**
* 定时任务日志列表
*/
@RequestMapping("/list")
public Result list(@RequestParam Map params) {
Result result = Result.fail();
PageUtils page = null;
try {
page = scheduleJobLogService.queryPage(params);
return result.resultSuccess();
} catch (HrzException e) {
e.printStackTrace();
return result;
}
}
/**
* 定时任务日志信息
*/
@GetMapping("/info")
public Result info(String id) {
Result result = Result.fail();
ScheduleJobLog scheduleJobLog = scheduleJobLogService.getById(id);
return result.resultSuccess();
}
}
五、视图层
···
1、任务调度
2、调度日志
···