Spring Boot 菜鳥教程 14 動態(tài)修改定時任務cron參數(shù)

動態(tài)修改定時任務cron參數(shù)

  • 不需要重啟應用就可以動態(tài)的改變Cron表達式的值
  • 不能使用@Scheduled(cron = "${jobs.cron}")實現(xiàn)

動態(tài)定時任務類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;

/**
 * @author JE哥
 * @email 1272434821@qq.com
 * @description:動態(tài)修改定時任務cron參數(shù)
 */
@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;

  @Autowired
  private UserRepository userRepository;

  @Override
  public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    taskRegistrar.addTriggerTask(new Runnable() {
      @Override
      public void run() {
    if (!cron.equals(DEFAULT_CRON)) {
      User user = new User("je_ge", 20);
      userRepository.save(user);
    }
    // 定時任務的業(yè)務邏輯
    System.out.println("動態(tài)修改定時任務cron參數(shù),當前時間:" + dateFormat.format(new Date()));
      }
    }, new Trigger() {
      @Override
      public Date nextExecutionTime(TriggerContext triggerContext) {
    // 定時任務觸發(fā),可修改定時任務的執(zhí)行周期
    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;

// 更新動態(tài)任務時間
@RequestMapping("/updateDynamicScheduledTask")
@ResponseBody
public AjaxResult updateDynamicScheduledTask() {
  dynamicScheduledTask.setCron("0/10 * * * * ?");
  return new AjaxResult().success();
}

user.jsp頁面添加按鈕方法

updateDynamicScheduledTask : function() {//動態(tài)修改定時任務
    $.get("/user/updateDynamicScheduledTask", function(data) {
        if (data.meta.success) {//動態(tài)修改定時任務成功
            $.messager.alert('成功提示', "請重新刷新數(shù)據(jù),有插入新的數(shù)據(jù)", 'info');
        } else {
            $.messager.alert('錯誤提示', data.meta.message, 'error');
        }
    }, 'json');
}

<a href="javascript:void(0)" class="easyui-linkbutton c8" iconCls="icon-search" data-url="updateDynamicScheduledTask">動態(tài)修改定時任務</a>

其他關(guān)聯(lián)項目

源碼地址

https://github.com/je-ge/spring-boot

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容