Commit b327b9cb by 邓实川 Committed by chenzg

动态定时任务优化

parent 4d4cf9f2
package cn.timer.api.bean.sche;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@Table(name = "schedule_task")
@ApiModel(value = "任务调度表")
public class ScheduleTask extends Model<ScheduleTask>{
private static final long serialVersionUID = 9109546668048881081L;
@Id
@GeneratedValue
@TableId(type = IdType.AUTO)
private Integer id;
private String cron;
private String className;
private String methodName;
}
package cn.timer.api.bean.quartz;
import java.math.BigInteger;
import lombok.Data;
@Data
public class JobAndTrigger {
private String JOB_NAME;
private String JOB_GROUP;
private String JOB_CLASS_NAME;
private String TRIGGER_NAME;
private String TRIGGER_GROUP;
private BigInteger REPEAT_INTERVAL;
private BigInteger TIMES_TRIGGERED;
private String CRON_EXPRESSION;
private String TIME_ZONE_ID;
}
//package cn.timer.api.controller.quartz;
//
//import java.util.HashMap;
//import java.util.Map;
//
//import org.quartz.CronScheduleBuilder;
//import org.quartz.CronTrigger;
//import org.quartz.JobBuilder;
//import org.quartz.JobDetail;
//import org.quartz.JobKey;
//import org.quartz.Scheduler;
//import org.quartz.SchedulerException;
//import org.quartz.TriggerBuilder;
//import org.quartz.TriggerKey;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//
//import com.github.pagehelper.PageInfo;
//
//import cn.timer.api.bean.quartz.JobAndTrigger;
//import cn.timer.api.config.quartz.TestJob;
//import cn.timer.api.dao.quartz.JobAndTriggerMapper;
//import cn.timer.api.utils.Result;
//import cn.timer.api.utils.ResultUtil;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//
//@Api(tags = "99.0 Quartz")
//@RestController
//@RequestMapping(value = "/quartz", produces = { "application/json" })
//public class JobController {
//
// @Autowired
// private Scheduler scheduler;
//
// @PostMapping(value = "/addjob")
// @ApiOperation(value = "新增任务", httpMethod = "POST", notes = "接口发布说明")
// public Result<Void> addjob(@RequestParam(value = "jobClassName") String jobClassName,
// @RequestParam(value = "jobGroupName") String jobGroupName,
// @RequestParam(value = "cronExpression") String cronExpression) throws Exception {
// addJob(jobClassName, jobGroupName, cronExpression);
// return ResultUtil.success("新增定时任务成功");
// }
//
// public void addJob(String jobClassName, String jobGroupName, String cronExpression) throws Exception {
//
// // 启动调度器
// scheduler.start();
//
// // 构建job信息
// JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
// .withIdentity(jobClassName, jobGroupName).build();
//
// // 表达式调度构建器(即任务执行的时间)
// CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
//
// // 按新的cronExpression表达式构建一个新的trigger
// CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobClassName, jobGroupName)
// .withSchedule(scheduleBuilder).build();
//
// try {
// scheduler.scheduleJob(jobDetail, trigger);
//
// } catch (SchedulerException e) {
// System.out.println("创建定时任务失败" + e);
// throw new Exception("创建定时任务失败");
// }
// }
//
// @PostMapping(value = "/pausejob")
// @ApiOperation(value = "暂停任务", httpMethod = "POST", notes = "接口发布说明")
// public Result<Void> pausejob(@RequestParam(value = "jobClassName") String jobClassName,
// @RequestParam(value = "jobGroupName") String jobGroupName) throws Exception {
// jobPause(jobClassName, jobGroupName);
// return ResultUtil.success("暂停定时任务成功");
// }
//
// public void jobPause(String jobClassName, String jobGroupName) throws Exception {
// scheduler.pauseJob(JobKey.jobKey(jobClassName, jobGroupName));
// }
//
// @PostMapping(value = "/resumejob")
// @ApiOperation(value = "恢复任务", httpMethod = "POST", notes = "接口发布说明")
// public Result<Void> resumejob(@RequestParam(value = "jobClassName") String jobClassName,
// @RequestParam(value = "jobGroupName") String jobGroupName) throws Exception {
// jobresume(jobClassName, jobGroupName);
// return ResultUtil.success("恢复定时任务成功");
// }
//
// public void jobresume(String jobClassName, String jobGroupName) throws Exception {
// scheduler.resumeJob(JobKey.jobKey(jobClassName, jobGroupName));
// }
//
// @PostMapping(value = "/reschedulejob")
// @ApiOperation(value = "重新设置任务", httpMethod = "POST", notes = "接口发布说明")
// public Result<Void> rescheduleJob(@RequestParam(value = "jobClassName") String jobClassName,
// @RequestParam(value = "jobGroupName") String jobGroupName,
// @RequestParam(value = "cronExpression") String cronExpression) throws Exception {
// jobreschedule(jobClassName, jobGroupName, cronExpression);
// return ResultUtil.success("重设定时任务成功");
// }
//
// public void jobreschedule(String jobClassName, String jobGroupName, String cronExpression) throws Exception {
// try {
// TriggerKey triggerKey = TriggerKey.triggerKey(jobClassName, jobGroupName);
// // 表达式调度构建器
// CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
//
// CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
//
// // 按新的cronExpression表达式重新构建trigger
// trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
//
// // 按新的trigger重新设置job执行
// scheduler.rescheduleJob(triggerKey, trigger);
// } catch (SchedulerException e) {
// System.out.println("更新定时任务失败" + e);
// throw new Exception("更新定时任务失败");
// }
// }
//
// @PostMapping(value = "/deletejob")
// @ApiOperation(value = "删除任务", httpMethod = "POST", notes = "接口发布说明")
// public Result<Void> deletejob(@RequestParam(value = "jobClassName") String jobClassName,
// @RequestParam(value = "jobGroupName") String jobGroupName) throws Exception {
// jobdelete(jobClassName, jobGroupName);
// return ResultUtil.success("删除成功");
// }
//
// public void jobdelete(String jobClassName, String jobGroupName) throws Exception {
// scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName, jobGroupName));
// scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName, jobGroupName));
// scheduler.deleteJob(JobKey.jobKey(jobClassName, jobGroupName));
// }
//
//}
package cn.timer.api.dao.sche;
import org.springframework.stereotype.Repository;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import cn.timer.api.bean.sche.ScheduleTask;
@Repository
public interface ScheduleTaskMapper extends BaseMapper<ScheduleTask>{
}
package cn.timer.api.utils.schedule;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import cn.timer.api.bean.sche.ScheduleTask;
public class CronUtil {
// 默认cron 10秒一次
private static String cron = "0/10 * * * * ?";
// 数据库cron
public static String getCron(String className, String methodName) {
ScheduleTask task = ScheduleTask.builder().build().selectOne(new LambdaQueryWrapper<ScheduleTask>()
.eq(ScheduleTask::getClassName, className).eq(ScheduleTask::getMethodName, methodName)); // 数据库查询
System.err.println(task);
if (task != null && task.getCron() != null) {
cron = task.getCron();
}
return cron;
}
}
\ No newline at end of file
......@@ -7,6 +7,7 @@ import java.util.List;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
......@@ -28,22 +29,24 @@ import cn.timer.api.utils.aliyun.AliyunSMS;
@Component
@Lazy(false)
@EnableScheduling
public class RemindUtil extends SpringDynamicCronTask{
public class RemindUtil implements SchedulingConfigurer {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
static String className = new Exception().getStackTrace()[0].getClassName();
static String methodName = null;
// 数据库动态更改定时配置
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(() -> {
//TODO 任务逻辑
reportCurrentTime();
// 任务逻辑
methodName = reportCurrentTime();
}, triggerContext -> {
CronTrigger cronTrigger = new CronTrigger(CronUtil.getCron()); //TODO
return cronTrigger.nextExecutionTime(triggerContext);
CronTrigger cronTrigger = new CronTrigger(CronUtil.getCron(className, methodName)); // cron配置
return cronTrigger.nextExecutionTime(triggerContext); // 下次执行任务的时间
});
}
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
/**
* 间隔时间提醒 30min/次
*/
......@@ -55,18 +58,13 @@ public class RemindUtil extends SpringDynamicCronTask{
/**
* 每天固定时间提醒
*/
@Scheduled(cron = "0 0 8 * * ?") // 每天8点扫一下看有没要提醒的,有就发一个
// @Scheduled(cron = "0 8 15 * * ?") // 测试合同提醒
public static void reportCurrentTime() {
// @Scheduled(cron = "0 0 8 * * ?") // 每天8点扫一下看有没要提醒的,有就发一个
public static String reportCurrentTime() {
List<HtzzAssoHtgx> htgxs = HtzzAssoHtgx.builder().build().selectAll();
for (HtzzAssoHtgx htgx : htgxs) {
QueryWrapper<HtzzAdminZzda> q = new QueryWrapper<HtzzAdminZzda>();
q.select("id", "zjmc", "txkg_type", "yxdqr").eq("id", htgx.getHtid()).eq("is_delete", 0).eq("txkg_type", 0);
HtzzAdminZzda zzda = HtzzAdminZzda.builder().build().selectOne(q);
if (zzda != null) {
String name = htgx.getName(); // 员工姓名
String phone = htgx.getPhone(); // 员工手机
......@@ -87,14 +85,13 @@ public class RemindUtil extends SpringDynamicCronTask{
} else if (betweenDay == 30) {
AliyunSMS.remind(name, htname, time, phone); // 少于30天短信提醒
}
} else {
zzda.setTxkgType(1); // 关闭提醒
zzda.updateById();
}
}
}
return new Exception().getStackTrace()[0].getMethodName();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment