feat: There is no way the, 动态定时任务
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package com.adc.da.search.conf;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: super_liu
|
||||
* @date: 2022年05月27日 10:17
|
||||
*/
|
||||
|
||||
import com.adc.da.slrs.tsTask.dao.TbTaskDao;
|
||||
import com.adc.da.slrs.tsTask.entity.TbTask;
|
||||
import com.adc.da.slrs.tsTask.service.ITbTaskService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.support.CronTrigger;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
/**
|
||||
* 动态定时器
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SchedulerConf {
|
||||
|
||||
//数据库的任务
|
||||
public static ConcurrentHashMap<String, TbTask> tasks = new ConcurrentHashMap<>(10);
|
||||
|
||||
//正在运行的任务
|
||||
public static ConcurrentHashMap<String, ScheduledFuture> runTasks = new ConcurrentHashMap<>(10);
|
||||
|
||||
//线程池任务调度
|
||||
private ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
|
||||
|
||||
@Autowired
|
||||
TbTaskDao tbTaskDao;
|
||||
|
||||
@Autowired
|
||||
ITbTaskService iTbTaskService;
|
||||
|
||||
/**
|
||||
* 初始化线程池任务调度
|
||||
*/
|
||||
@Autowired
|
||||
public SchedulerConf(){
|
||||
this.threadPoolTaskScheduler.setPoolSize(10);
|
||||
this.threadPoolTaskScheduler.setThreadNamePrefix("task-thread-");
|
||||
this.threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
|
||||
this.threadPoolTaskScheduler.initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有数据库里的定时任务
|
||||
*/
|
||||
private void getAllTbTask(){
|
||||
//查询所有,并put到tasks
|
||||
SchedulerConf.tasks.clear();
|
||||
List<TbTask> list = iTbTaskService.list();
|
||||
list.forEach((task)-> SchedulerConf.tasks.put(task.getId(),task));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据定时任务id,启动定时任务
|
||||
*/
|
||||
public void start(String taskId){
|
||||
try {
|
||||
//如果为空,重新获取
|
||||
if(SchedulerConf.tasks.size() <= 0){
|
||||
this.getAllTbTask();
|
||||
}
|
||||
TbTask tbTask = SchedulerConf.tasks.get(taskId);
|
||||
|
||||
//获取并实例化Runnable任务类
|
||||
Class<?> clazz = Class.forName(tbTask.getTaskClass());
|
||||
Runnable runnable = (Runnable)clazz.newInstance();
|
||||
|
||||
//Cron表达式
|
||||
CronTrigger cron = new CronTrigger(tbTask.getTaskExp());
|
||||
|
||||
//执行,并put到runTasks
|
||||
SchedulerConf.runTasks.put(taskId, Objects.requireNonNull(this.threadPoolTaskScheduler.schedule(runnable, cron)));
|
||||
|
||||
this.updateTaskStatus(taskId,1);
|
||||
|
||||
log.info("{},任务启动!",taskId);
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
||||
log.error("{},任务启动失败...",taskId);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据定时任务id,停止定时任务
|
||||
*/
|
||||
public void stop(String taskId){
|
||||
SchedulerConf.runTasks.get(taskId).cancel(true);
|
||||
|
||||
SchedulerConf.runTasks.remove(taskId);
|
||||
|
||||
this.updateTaskStatus(taskId,0);
|
||||
|
||||
log.info("{},任务停止...",taskId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据库动态定时任务状态
|
||||
*/
|
||||
private void updateTaskStatus(String taskId,int status){
|
||||
TbTask task = iTbTaskService.getById(taskId);
|
||||
task.setTaskStatus(status);
|
||||
task.setUpdateTime(new Date());
|
||||
iTbTaskService.updateById(task);
|
||||
}
|
||||
}
|
||||
@@ -2,37 +2,21 @@ package com.adc.da.search.sync;
|
||||
|
||||
import com.adc.da.search.bean.SearchCenter;
|
||||
import com.adc.da.search.server.ResetSearchCenterService;
|
||||
import com.adc.da.slrs.sarBussionessStand.dao.SarBussionessStandDao;
|
||||
import com.adc.da.slrs.sarBussionessStand.entity.SarBussionessStand;
|
||||
import com.adc.da.slrs.sarBussionessStandState.entity.SarBussionessStandState;
|
||||
import com.adc.da.slrs.sarBussionessStandState.service.ISarBussionessStandStateService;
|
||||
import com.adc.da.utils.util.DateUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: super_liu
|
||||
* @date: 2022年01月25日 3:34
|
||||
*/
|
||||
@EnableScheduling
|
||||
@Component
|
||||
|
||||
@Slf4j
|
||||
public class restSearchSync {
|
||||
public class restSearchSync implements Runnable {
|
||||
Logger logger = LoggerFactory.getLogger(restSearchSync.class);
|
||||
|
||||
@Autowired
|
||||
@@ -41,31 +25,50 @@ public class restSearchSync {
|
||||
/**
|
||||
* 根据配置文件设置是否开启定时器
|
||||
*/
|
||||
|
||||
@Value("${isNotScheduled}")
|
||||
private boolean isNotScheduled; //是否开启定时器
|
||||
|
||||
|
||||
// 每天0点1分执行 重置ES 自动更新、新增
|
||||
// @Scheduled(cron="0 0 1 1 * ?")
|
||||
// @Scheduled(cron = "0 1 0 * * ?")
|
||||
@Async
|
||||
public void StandScheduledJobMonthBegin(){
|
||||
if(isNotScheduled){
|
||||
try{
|
||||
Thread.sleep(2000);
|
||||
logger.info("每天0点1分执行 重置ES 自动更新、新增:"+Thread.currentThread().getName() + " cron=0 1 0 * * ? --- " + new Date()+"---START-01");
|
||||
/**
|
||||
* 动态调用重置ES7天内标准数据
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
try{
|
||||
Thread.sleep(2000);
|
||||
logger.info("每天0点1分执行 重置ES 自动更新、新增:"+Thread.currentThread().getName() + " cron=0 1 0 * * ? --- " + new Date()+"---START-01");
|
||||
|
||||
SearchCenter searchCenter = new SearchCenter();
|
||||
// ALL 执行全部
|
||||
searchCenter.setExecType("ALL");
|
||||
resetSearchCenterService.syncResetALLSearchCenter(searchCenter);
|
||||
SearchCenter searchCenter = new SearchCenter();
|
||||
// ALL 执行全部
|
||||
searchCenter.setExecType("ALL");
|
||||
resetSearchCenterService.syncResetALLSearchCenter(searchCenter);
|
||||
|
||||
logger.info("每天0点1分执行 重置ES 自动更新、新增:"+Thread.currentThread().getName() + " cron=0 1 0 * * ? --- " + new Date()+"---End-01");
|
||||
}catch(Exception e){
|
||||
logger.info(e.getMessage());
|
||||
}
|
||||
logger.info("每天0点1分执行 重置ES 自动更新、新增:"+Thread.currentThread().getName() + " cron=0 1 0 * * ? --- " + new Date()+"---End-01");
|
||||
}catch(Exception e){
|
||||
logger.info(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 每天0点1分执行 重置ES 自动更新、新增
|
||||
// @Scheduled(cron="0 0 1 1 * ?")
|
||||
// @Scheduled(cron = "0 1 0 * * ?")
|
||||
// @Async
|
||||
// public void StandScheduledJobMonthBegin(){
|
||||
// if(isNotScheduled){
|
||||
// try{
|
||||
// Thread.sleep(2000);
|
||||
// logger.info("每天0点1分执行 重置ES 自动更新、新增:"+Thread.currentThread().getName() + " cron=0 1 0 * * ? --- " + new Date()+"---START-01");
|
||||
//
|
||||
// SearchCenter searchCenter = new SearchCenter();
|
||||
// // ALL 执行全部
|
||||
// searchCenter.setExecType("ALL");
|
||||
// resetSearchCenterService.syncResetALLSearchCenter(searchCenter);
|
||||
//
|
||||
// logger.info("每天0点1分执行 重置ES 自动更新、新增:"+Thread.currentThread().getName() + " cron=0 1 0 * * ? --- " + new Date()+"---End-01");
|
||||
// }catch(Exception e){
|
||||
// logger.info(e.getMessage());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.adc.da.slrs.tsTask.dao.TbTaskDao">
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user