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"));
}
}
任務暫停重啟後,會把任務暫停期間的執行次數補回來,暫時未解決。
相關文章
- Quartz實現動態定時任務quartz
- 動態改變quartz的觸發器策略?quartz觸發器
- Quartz 2Dquartz
- Spring 整合 Quartz 實現動態定時任務Springquartz
- 動態遊標的操作~~~
- Spring+quartz實現動態化定時任務 (轉)Springquartz
- springboot整合Quartz實現動態配置定時任務Spring Bootquartz
- iOS之Quartz2DiOSquartz
- Quartz.NET 2.x 文件翻譯 - Lesson 1:使用Quartzquartz
- Net作業排程(五)—quartz.net動態新增job設計quartz
- SpringBoot整合Quartz實現動態修改定時任務間隔Spring Bootquartz
- 2. Vue語法--插值操作&動態繫結屬性 詳解Vue
- .NET Core 基於Quartz的UI視覺化操作元件 GZY.Quartz.MUI 簡介quartzUI視覺化元件
- 動態Lambda(2):優化優化
- spring-boot-route(二十一)quartz實現動態定時任務Springbootquartz
- Quartz.NET 2.x 文件翻譯 - Lesson 12:Quartz的其他各種特性quartz
- Java 動態性(4) – 位元組碼操作Java
- JQuery1:動態操作HTML和CSSjQueryHTMLCSS
- SpringDataJpa (二)-動態查詢&多表操作Spring
- JavaScript動態操作select下拉選單JavaScript
- 查詢(2)--動態查詢
- spring2.0 quartz 靜態定時任務Springquartz
- Quartz - Quartz簡單入門quartz
- GZY.Quartz.MUI(基於Quartz的UI視覺化操作元件) 2.6.0釋出 相容.Net8.0quartzUI視覺化元件
- Spring4+Springmvc+quartz實現多執行緒動態定時排程SpringMVCquartz執行緒
- Python騷操作:動態定義函式Python函式
- Dynamic disk動態磁碟的操作和總結
- javascript動態操作table表格程式碼例項JavaScript
- 繪製下載進度(Quartz2D)quartz
- Quartz - Spring和Quartz的整合quartzSpring
- RPi2BDDNS動態域名DNS
- Struts2 動態方法呼叫(十三)
- 動態IPvps,選用動態IPvps的意義,使用動態IPvps的說明及連線操作
- 1.18 JQuery1:動態操作HTML和CSSjQueryHTMLCSS
- JavaScript操作下拉框動態顯示內容JavaScript
- 動態語言常用操作-python、JavaScript、flutter(dartf)PythonJavaScriptFlutterDart
- Quartz初探quartz
- Quartz原理quartz