feat: There is no way the, 动态定时任务

This commit is contained in:
super_liu
2022-05-27 13:23:43 +08:00
parent e7179fc7d6
commit bfc84ab47d
8 changed files with 371 additions and 36 deletions
@@ -0,0 +1,96 @@
package com.adc.da.slrs.tsTask.controller;
import com.adc.da.search.conf.SchedulerConf;
import com.adc.da.slrs.tsTask.service.ITbTaskService;
import oracle.ucp.proxy.annotation.Post;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.adc.da.slrs.tsTask.entity.TbTask;
import io.swagger.annotations.Api;
import com.adc.da.base.web.BaseController;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* 动态定时任务表 前端控制器
* </p>
*
* @author super_liu
* @since 2022-05-27
*/
@RestController
@Api(tags = "动态定时任务表")
@RequestMapping("/tsTask/tb-task")
public class TbTaskController extends BaseController<TbTask> {
@Autowired
private SchedulerConf schedulerConf;
@Autowired
private ITbTaskService iTbTaskService;
/**
* 启动一个动态定时任务
* http://localhost:10085/tbTask/start/2
*/
@GetMapping("start/{taskId}")
public String start(@PathVariable("taskId") String taskId){
schedulerConf.start(taskId);
return "操作成功";
}
/**
* 停止一个动态定时任务
* http://localhost:10085/tbTask/stop/2
*/
@GetMapping("stop/{taskId}")
public String stop(@PathVariable("taskId") String taskId){
schedulerConf.stop(taskId);
return "操作成功";
}
/**
* 更新一个动态定时任务
* http://localhost:10085/tbTask/save?taskId=2&taskExp=0/2 * * * * ?&taskClass=cn.huanzi.qch.springboottimer.task.MyRunnable3
*/
@PostMapping("save")
public String save(TbTask task) throws IllegalAccessException {
//先更新表数据
TbTask tbTask = iTbTaskService.getById(task.getId());
//null值忽略
List<String> ignoreProperties = new ArrayList<>(7);
//反射获取Class的属性(Field表示类中的成员变量)
for (Field field : task.getClass().getDeclaredFields()) {
//获取授权
field.setAccessible(true);
//属性名称
String fieldName = field.getName();
//属性的值
Object fieldValue = field.get(task);
//找出值为空的属性,我们复制的时候不进行赋值
if(null == fieldValue){
ignoreProperties.add(fieldName);
}
}
//org.springframework.beans BeanUtils.copyProperties(A,B):A中的值付给B
BeanUtils.copyProperties(task, tbTask,ignoreProperties.toArray(new String[0]));
iTbTaskService.updateById(tbTask);
schedulerConf.tasks.clear();
//停止旧任务
schedulerConf.stop(tbTask.getId());
//重新启动
schedulerConf.start(tbTask.getId());
return "操作成功";
}
}
@@ -0,0 +1,18 @@
package com.adc.da.slrs.tsTask.dao;
import com.adc.da.slrs.tsTask.entity.TbTask;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
/**
* <p>
* 动态定时任务表 Mapper 接口
* </p>
*
* @author super_liu
* @since 2022-05-27
*/
@Repository
public interface TbTaskDao extends BaseMapper<TbTask> {
}
@@ -0,0 +1,57 @@
package com.adc.da.slrs.tsTask.entity;
import com.adc.da.base.entity.BaseEntity;
import java.time.LocalDateTime;
import java.util.Date;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
/**
* <p>
* 动态定时任务表
* </p>
*
* @author super_liu
* @since 2022-05-27
*/
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@ApiModel(value="TbTask对象", description="动态定时任务表")
public class TbTask extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "定时任务id")
@TableId("ID")
private String id;
@ApiModelProperty(value = "定时任务名称")
private String taskName;
@ApiModelProperty(value = "定时任务描述")
private String taskDesc;
@ApiModelProperty(value = "定时任务Cron表达式")
private String taskExp;
@ApiModelProperty(value = "定时任务状态,0停用 1启用")
private Integer taskStatus;
@ApiModelProperty(value = "定时任务的Runnable任务类完整路径")
private String taskClass;
@ApiModelProperty(value = "更新时间")
private Date updateTime;
@ApiModelProperty(value = "创建时间")
private Date createTime;
}
@@ -0,0 +1,16 @@
package com.adc.da.slrs.tsTask.service;
import com.adc.da.slrs.tsTask.entity.TbTask;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* <p>
* 动态定时任务表 服务类
* </p>
*
* @author super_liu
* @since 2022-05-27
*/
public interface ITbTaskService extends IService<TbTask> {
}
@@ -0,0 +1,20 @@
package com.adc.da.slrs.tsTask.service.impl;
import com.adc.da.slrs.tsTask.entity.TbTask;
import com.adc.da.slrs.tsTask.dao.TbTaskDao;
import com.adc.da.slrs.tsTask.service.ITbTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
/**
* <p>
* 动态定时任务表 服务实现类
* </p>
*
* @author super_liu
* @since 2022-05-27
*/
@Service
public class TbTaskServiceImpl extends ServiceImpl<TbTaskDao, TbTask> implements ITbTaskService {
}