亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java 動態增加定時任務示例

發布時間:2020-09-22 21:19:34 來源:腳本之家 閱讀:178 作者:張錫斌 欄目:編程語言

整理文檔,java 動態增加定時任務示例,直接上代碼。

import org.apache.tools.ant.util.DateUtils;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Calendar;
import java.util.Date;


/**
 * @Description: 定時任務管理類
 * Created by zhangxb on 2017/2/10.
 */
public class QuartzManager {
  private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
  private static String JOB_GROUP_NAME = "EXTJWEB_JOBGROUP_NAME";
  private static String TRIGGER_GROUP_NAME = "EXTJWEB_TRIGGERGROUP_NAME";

  /**
   * @Description: 添加一個定時任務,使用默認的任務組名,觸發器名,觸發器組名
   *
   * @param jobName
   *      任務名
   * @param cls
   *      任務
   * @param time
   *      時間設置,參考quartz說明文檔
   *
   * @Title: QuartzManager.java
   */
  @SuppressWarnings("unchecked")
  public static void addJob(String jobName, Class cls, String time,Object params) {
    try {
      Scheduler sched = gSchedulerFactory.getScheduler();
      JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, cls);// 任務名,任務組,任務執行類
      jobDetail.getJobDataMap().put("params", params);
      // 觸發器
      CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// 觸發器名,觸發器組
      trigger.setCronExpression(time);// 觸發器時間設定
      sched.scheduleJob(jobDetail, trigger);
      // 啟動
      if (!sched.isShutdown()) {
        sched.start();
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * @Description: 添加一個定時任務
   *
   * @param jobName
   *      任務名
   * @param jobGroupName
   *      任務組名
   * @param triggerName
   *      觸發器名
   * @param triggerGroupName
   *      觸發器組名
   * @param jobClass
   *      任務
   * @param time
   *      時間設置,參考quartz說明文檔
   *
   */
  @SuppressWarnings("unchecked")
  public static void addJob(String jobName, String jobGroupName,
               String triggerName, String triggerGroupName, Class jobClass,
               String time,Object params) {
    try {
      Scheduler sched = gSchedulerFactory.getScheduler();
      JobDetail jobDetail = new JobDetail(jobName, jobGroupName, jobClass);// 任務名,任務組,任務執行類
      jobDetail.getJobDataMap().put("params",params);
      // 觸發器
      CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// 觸發器名,觸發器組
      trigger.setCronExpression(time);// 觸發器時間設定
      sched.scheduleJob(jobDetail, trigger);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * @Description: 修改一個任務的觸發時間(使用默認的任務組名,觸發器名,觸發器組名)
   *
   * @param jobName
   * @param time
   *
   */
  @SuppressWarnings("unchecked")
  public static void modifyJobTime(String jobName, String time) {
    try {
      Scheduler sched = gSchedulerFactory.getScheduler();
      CronTrigger trigger = (CronTrigger) sched.getTrigger(jobName,TRIGGER_GROUP_NAME);
      if (trigger == null) {
        return;
      }
      String oldTime = trigger.getCronExpression();
      if (!oldTime.equalsIgnoreCase(time)) {
        JobDetail jobDetail = sched.getJobDetail(jobName,JOB_GROUP_NAME);
        Class objJobClass = jobDetail.getJobClass();
        removeJob(jobName);
        addJob(jobName, objJobClass, time ,jobDetail.getJobDataMap());
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * @Description: 修改一個任務的觸發時間
   *
   */
  public static void modifyJobTime(String triggerName,
                   String triggerGroupName, String time) {
    try {
      Scheduler sched = gSchedulerFactory.getScheduler();
      CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName,triggerGroupName);
      if (trigger == null) {
        return;
      }
      String oldTime = trigger.getCronExpression();
      if (!oldTime.equalsIgnoreCase(time)) {
        CronTrigger ct = (CronTrigger) trigger;
        // 修改時間
        ct.setCronExpression(time);
        // 重啟觸發器
        sched.resumeTrigger(triggerName, triggerGroupName);
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * @Description: 移除一個任務(使用默認的任務組名,觸發器名,觸發器組名)
   *
   * @param jobName
   *
   */
  public static void removeJob(String jobName) {
    try {
      Scheduler sched = gSchedulerFactory.getScheduler();
      sched.pauseTrigger(jobName, TRIGGER_GROUP_NAME);// 停止觸發器
      sched.unscheduleJob(jobName, TRIGGER_GROUP_NAME);// 移除觸發器
      sched.deleteJob(jobName, JOB_GROUP_NAME);// 刪除任務
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * @Description: 移除一個任務
   *
   */
  public static void removeJob(String jobName, String jobGroupName,
                 String triggerName, String triggerGroupName) {
    try {
      Scheduler sched = gSchedulerFactory.getScheduler();
      sched.pauseTrigger(triggerName, triggerGroupName);// 停止觸發器
      sched.unscheduleJob(triggerName, triggerGroupName);// 移除觸發器
      sched.deleteJob(jobName, jobGroupName);// 刪除任務
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * @Description:啟動所有定時任務
   */
  public static void startJobs() {
    try {
      Scheduler sched = gSchedulerFactory.getScheduler();
      sched.start();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * @Description:關閉所有定時任務
   *
   */
  public static void shutdownJobs() {
    try {
      Scheduler sched = gSchedulerFactory.getScheduler();
      if (!sched.isShutdown()) {
        sched.shutdown();
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  public static String formatTime(Date date){
    //0/1 * * * * ?
    String time = DateUtils.format( date, "s m h d * ?");
    System.out.println(time);
    return time;
  }
}

測試類

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Description: 任務執行類
 * Created by zhangxb on 2017/2/10.
 */
public class QuartzJob implements Job {
  @Override
  public void execute(JobExecutionContext context) throws JobExecutionException {
    Object params =  context.getJobDetail().getJobDataMap().get("params");
    System.out.println(params);
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+ "★★★★★★★★★★★");
    System.out.println("【移除定時】開始...");
    QuartzManager.removeJob((String)params);
    System.out.println("【移除定時】成功");
  }
  public static void main(String[] args) {
    try {
      String job_name = "動態任務調度";
      System.out.println("【系統啟動】開始(每1秒輸出一次)...");
      Calendar cal = Calendar.getInstance();
      cal.add( Calendar.SECOND,10);
      QuartzManager.addJob(job_name, QuartzJob.class, QuartzManager.formatTime(cal.getTime()),job_name);
      Thread.sleep(11);
      cal.setTime( new Date());
      cal.add( Calendar.SECOND,10);
      QuartzManager.addJob(job_name+1, QuartzJob.class, QuartzManager.formatTime(cal.getTime()),job_name+1);
      Thread.sleep(11);
//      System.out.println("【修改時間】開始(每2秒輸出一次)...");
//      QuartzManager.modifyJobTime(job_name, "10/2 * * * * ?");
//      Thread.sleep(6000);
//      System.out.println("【移除定時】開始...");
//      QuartzManager.removeJob(job_name);
//      System.out.println("【移除定時】成功");
//
//      System.out.println("【再次添加定時任務】開始(每10秒輸出一次)...");
//      QuartzManager.addJob(job_name, QuartzJob.class, "*/10 * * * * ?");
//      Thread.sleep(60000);
//      System.out.println("【移除定時】開始...");
//      QuartzManager.removeJob(job_name);
//      System.out.println("【移除定時】成功");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

不過最后發現,還是沒法做動態延遲。后面用延時mq實現的。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

辽宁省| 扎赉特旗| 张家口市| 五家渠市| 北碚区| 东兴市| 屏山县| 莱阳市| 迁安市| 巴南区| 乌海市| 普安县| 嘉定区| 井研县| 龙江县| 芦山县| SHOW| 屏南县| 钦州市| 淳化县| 沁源县| 兰西县| 卢氏县| 荆门市| 诸城市| 白玉县| 高阳县| 灵武市| 甘洛县| 横峰县| 四子王旗| 宣武区| 兴义市| 松原市| 霸州市| 德令哈市| 元朗区| 吉首市| 栾城县| 太康县| 普格县|