余姚网站推广策划案,做网站顺序,友情链接交换的作用在于,安徽海川建设公司网站动态修改定时任务cron参数 不需要重启应用就可以动态的改变Cron表达式的值不能使用Scheduled(cron ${jobs.cron})实现动态定时任务类DynamicScheduledTask
package com.jege.spring.boot.task;import java.text.SimpleDateFormat;
import java.util.Date;import …动态修改定时任务cron参数 不需要重启应用就可以动态的改变Cron表达式的值不能使用Scheduled(cron ${jobs.cron})实现动态定时任务类DynamicScheduledTask
package com.jege.spring.boot.task;import java.text.SimpleDateFormat;
import java.util.Date;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;import com.jege.spring.boot.data.jpa.entity.User;
import com.jege.spring.boot.data.jpa.repository.UserRepository;/*** 动态修改定时任务cron参数*/
Component
public class DynamicScheduledTask implements SchedulingConfigurer {private static final SimpleDateFormat dateFormat new SimpleDateFormat(HH:mm:ss);private static final String DEFAULT_CRON 0/5 * * * * ?;private String cron DEFAULT_CRON;Autowiredprivate UserRepository userRepository;Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.addTriggerTask(new Runnable() {Overridepublic void run() {if (!cron.equals(DEFAULT_CRON)) {User user new User(je_ge, 20);userRepository.save(user);}// 定时任务的业务逻辑System.out.println(动态修改定时任务cron参数当前时间 dateFormat.format(new Date()));}}, new Trigger() {Overridepublic Date nextExecutionTime(TriggerContext triggerContext) {// 定时任务触发可修改定时任务的执行周期CronTrigger trigger new CronTrigger(cron);Date nextExecDate trigger.nextExecutionTime(triggerContext);return nextExecDate;}});}public void setCron(String cron) {this.cron cron;}
}启动类Application添加EnableScheduling标注
EnableScheduling控制器UserController
Autowired
DynamicScheduledTask dynamicScheduledTask;// 更新动态任务时间
RequestMapping(/updateDynamicScheduledTask)
ResponseBody
public AjaxResult updateDynamicScheduledTask() {dynamicScheduledTask.setCron(0/10 * * * * ?);return new AjaxResult().success();
}user.jsp页面添加按钮方法
updateDynamicScheduledTask : function() {//动态修改定时任务$.get(/user/updateDynamicScheduledTask, function(data) {if (data.meta.success) {//动态修改定时任务成功$.messager.alert(成功提示, 请重新刷新数据有插入新的数据, info);} else {$.messager.alert(错误提示, data.meta.message, error);}}, json);
}a hrefjavascript:void(0) classeasyui-linkbutton c8 iconClsicon-search data-urlupdateDynamicScheduledTask动态修改定时任务/a如果感觉不错的话记得点赞哟