Quartz (2) - 動態操作
code:
package quartz;
import javassist.expr.NewArray;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.triggers.CronTriggerImpl;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
public class jobScheduler {
private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
// 新增
public static void addJob(String name, String groupName, Class<? extends Job> cls, String cron, Map<String ,Object> param) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
JobDetail jobDetail = new JobDetailImpl(name, groupName, cls);
jobDetail.getJobDataMap().putAll(param); // 帶參
CronTrigger trigger = new CronTriggerImpl(name, groupName, name, groupName,cron);
scheduler.scheduleJob(jobDetail, trigger);
// 啟動
if (!scheduler.isShutdown()) {
scheduler.start();
}
} catch (SchedulerException | ParseException e) {
e.printStackTrace();
}
}
// 查詢狀態
public static String state(String name, String groupName) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
CronTrigger trigger = new CronTriggerImpl(name, groupName, name, groupName);
Trigger.TriggerState state = scheduler.getTriggerState(trigger.getKey());
return state.toString();
} catch (SchedulerException e) {
e.printStackTrace();
}
return "";
}
// 修改執行時間
public static void modifyTime(String name, String groupName, String cron) {
// 方式1 移除 - 重新新增
try {
Scheduler scheduler = schedulerFactory.getScheduler();
JobDetail jobDetail = scheduler.getJobDetail(new JobKey(name, groupName));
remove(name, groupName);
addJob(name, groupName, jobDetail.getJobClass(), cron, jobDetail.getJobDataMap());
} catch (SchedulerException e) {
e.printStackTrace();
}
// 方式2 修改 CronTrigger
try {
Scheduler scheduler = schedulerFactory.getScheduler();
CronTrigger cronTrigger = (CronTriggerImpl) scheduler.getTrigger(new TriggerKey(name, groupName));
((CronTriggerImpl) cronTrigger).setCronExpression(cron);
scheduler.resumeTrigger(cronTrigger.getKey());
scheduler.rescheduleJob(cronTrigger.getKey(), cronTrigger); // 不重新啟動的話,更改不生效。
} catch (SchedulerException | ParseException e) {
e.printStackTrace();
}
}
// 停止一個job任務
public static void pauseJob(String name, String groupName){
try {
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.pauseJob(new JobKey(name, groupName));
} catch (SchedulerException e) {
e.printStackTrace();
}
}
// 重啟動
public static void restart(String name, String groupName){
try {
Scheduler scheduler = schedulerFactory.getScheduler();
CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(new TriggerKey(name, groupName));
scheduler.rescheduleJob(cronTrigger.getKey(), cronTrigger);
} catch (SchedulerException e) {
e.printStackTrace();
}
}
// 移除
public static void remove(String name, String groupName) {
try {
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.pauseTrigger(new TriggerKey(name, groupName)); // 停止觸發器
scheduler.unscheduleJob(new TriggerKey(name, groupName)); // 移除觸發器
scheduler.deleteJob(new JobKey(name, groupName)); // 刪除job
} catch (SchedulerException e) {
e.printStackTrace();
}
}
// 關閉所有
public static void shutdownJobs() {
try {
Scheduler sched = schedulerFactory.getScheduler();
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
Map<String ,Object> param = new HashMap<>();
param.put("param", "paramA");
param.put("id", 12);
addJob("test1", "test1", warnJob.class, "0 30 09 22 06 ? 2018", param);
addJob("test2", "test2", warnJob.class, "*/5 * * * * ?", param);
try {
String state_1 = state("test1", "test1");
String state_2 = state("test2", "test2");
String state_3 = state("test3", "test3");
System.out.println("state: " + state_1 + " | " + state_2 + " | " + state_3);
Thread.sleep(1000L * 5);
System.out.println("modify : ");
modifyTime("test2", "test2", "*/1 * * * * ?");
Thread.sleep(1000L * 5);
System.out.println("pause : ");
pauseJob("test2", "test2");
Thread.sleep(1000L * 5);
System.out.println("restart : ");
restart("test2", "test2" );
Thread.sleep(1000L * 5);
shutdownJobs();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Job class:
package quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
public class warnJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("現在時間 :" + new Date() + " | " + jobExecutionContext.getJobDetail().getJobDataMap().get("param")
+ " | " + jobExecutionContext.getJobDetail().getJobDataMap().get("id"));
}
}
任務暫停重啟後,會把任務暫停期間的執行次數補回來,暫時未解決。
相關文章
- springboot整合Quartz實現動態配置定時任務Spring Bootquartz
- Spring+quartz實現動態化定時任務 (轉)Springquartz
- SpringBoot整合Quartz實現動態修改定時任務間隔Spring Bootquartz
- Quartz.NET 2.x 文件翻譯 - Lesson 1:使用Quartzquartz
- .NET Core 基於Quartz的UI視覺化操作元件 GZY.Quartz.MUI 簡介quartzUI視覺化元件
- 2. Vue語法--插值操作&動態繫結屬性 詳解Vue
- spring-boot-route(二十一)quartz實現動態定時任務Springbootquartz
- JavaScript動態操作select下拉選單JavaScript
- SpringDataJpa (二)-動態查詢&多表操作Spring
- Quartz.NET 2.x 文件翻譯 - Lesson 12:Quartz的其他各種特性quartz
- JQuery1:動態操作HTML和CSSjQueryHTMLCSS
- Python騷操作:動態定義函式Python函式
- Java 動態性(4) – 位元組碼操作Java
- 動態IPvps,選用動態IPvps的意義,使用動態IPvps的說明及連線操作
- GZY.Quartz.MUI(基於Quartz的UI視覺化操作元件) 2.6.0釋出 相容.Net8.0quartzUI視覺化元件
- Quartz - Spring和Quartz的整合quartzSpring
- Quartz - Quartz簡單入門quartz
- 1.18 JQuery1:動態操作HTML和CSSjQueryHTMLCSS
- 動態語言常用操作-python、JavaScript、flutter(dartf)PythonJavaScriptFlutterDart
- Dynamic disk動態磁碟的操作和總結
- Quartz.NET 2.x 文件翻譯 - Lesson 2:Jobs 和 Triggersquartz
- 動態IPvps,租用動態IPvps的意義以及連線辦法及操作步驟
- BZOJ 3589 動態樹(子樹操作,鏈查詢)
- Mastercam 2017 動態指標建立工作平面的操作AST指標
- Protobuf 動態載入 .proto 檔案並操作 Message
- mybatis Selective動態判斷屬性值新增或修改操作,batch批次操作MyBatis
- Quartz初探quartz
- springboot專案整合quartz動態建立,停止,恢復,刪除定時任務,整合swagger3.0Spring BootquartzSwagger
- win10動態桌布steam怎麼操作_win10動態桌布steam設定方法Win10
- Autofac 和 Quartz.Net 自動注入的整合quartz
- db2常用動態效能檢視DB2
- 2-7 陣列:動態陣列陣列
- 動態規劃專項訓練 2動態規劃
- Quartz 2D程式設計指南 (一) —— 簡介(一)quartz程式設計
- Quartz.NET 2.x 文件翻譯 - Lesson 8:SchedulerListenersquartz
- Quartz.NET 2.x 文件翻譯 - Lesson 9:JobStoresquartz
- Quartz.NET 2.x 文件翻譯-文章目錄quartz
- 使用 JS 來動態操作 css ,你知道幾種方法?JSCSS