Quartz 정시 작업 도구 클래스
package com.jeeplus.common.utils;
import java.util.Map;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;
/**
* Quartz util
*
* @author xc
*
*/
public class QuartzManager {
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory(); // SchedulerFactory
private static String JOB_GROUP_NAME = "FH_JOBGROUP_NAME"; //
private static String TRIGGER_GROUP_NAME = "FH_TRIGGERGROUP_NAME"; //
/**
* , , ,
*
* @param jobName
*
* @param cls
*
* @param time
* , quartz
*/
public static void addJob(String jobName, Class extends Job> cls, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler(); // SchedulerFactory Scheduler
JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME).build(); // Job ,
CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger() // TriggerBuilder
.withIdentity(jobName, TRIGGER_GROUP_NAME) //
.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
sched.start(); //
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* , , , ( )
*
* @param jobName
*
* @param cls
*
* @param time
* , quartz
*/
public static void addJob(String jobName, Class extends Job> cls, String time, Map parameter) {
try {
Scheduler sched = gSchedulerFactory.getScheduler(); // SchedulerFactory Scheduler
JobDetail jobDetail = JobBuilder.newJob(cls).withIdentity(jobName, JOB_GROUP_NAME).build(); // Job ,
jobDetail.getJobDataMap().put("parameterList", parameter); //
CronTrigger trigger = (CronTrigger) TriggerBuilder.newTrigger() // TriggerBuilder
.withIdentity(jobName, TRIGGER_GROUP_NAME) //
.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
sched.start(); //
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*
*
* @param jobName
*
* @param jobGroupName
*
* @param triggerName
*
* @param triggerGroupName
*
* @param jobClass
*
* @param time
* , quartz
*/
public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName,
Class extends Job> jobClass, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, jobGroupName).build();// , ,
CronTrigger trigger = (CronTrigger) TriggerBuilder //
.newTrigger().withIdentity(triggerName, triggerGroupName)
.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
sched.start(); //
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* ( )
*
* @param jobName
*
* @param jobGroupName
*
* @param triggerName
*
* @param triggerGroupName
*
* @param jobClass
*
* @param time
* , quartz
*/
public static void addJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName,
Class extends Job> jobClass, String time, Map parameter) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob((Class extends Job>) jobClass).withIdentity(jobName, jobGroupName)
.build();// , ,
jobDetail.getJobDataMap().put("parameterList", parameter); //
CronTrigger trigger = (CronTrigger) TriggerBuilder //
.newTrigger().withIdentity(triggerName, triggerGroupName)
.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
sched.scheduleJob(jobDetail, trigger);
if (!sched.isShutdown()) {
sched.start(); //
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* ( , , )
*
* @param jobName
*
* @param time
*
*/
public static void modifyJobTime(String jobName, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler(); // SchedulerFactory Scheduler
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME); // TriggerKey
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); // TriggerKey CronTrigger
if (trigger == null) {
return;
}
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME); // JobKey
JobDetail jobDetail = sched.getJobDetail(jobKey);
Class extends Job> objJobClass = jobDetail.getJobClass();
removeJob(jobName);
addJob(jobName, objJobClass, time);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*
*
* @param triggerName
*
* @param triggerGroupName
*
* @param time
*
*/
public static void modifyJobTime(String triggerName, String triggerGroupName, String time) {
try {
Scheduler sched = gSchedulerFactory.getScheduler(); // SchedulerFactory Scheduler
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); // TriggerKey
CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerKey); // TriggerKey CronTrigger
if (trigger == null)
return;
CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(trigger.getCronExpression());
String oldTime = trigger.getCronExpression();
if (!oldTime.equalsIgnoreCase(time)) {
trigger = (CronTrigger) trigger.getTriggerBuilder() // trigger
.withIdentity(triggerKey).withSchedule(scheduleBuilder)
.withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
sched.rescheduleJob(triggerKey, trigger); // trigger job
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* ( , , )
*
* @param jobName
*
*/
public static void removeJob(String jobName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(jobName, TRIGGER_GROUP_NAME); // TriggerKey
JobKey jobKey = JobKey.jobKey(jobName, JOB_GROUP_NAME); // JobKey
sched.pauseTrigger(triggerKey); //
sched.unscheduleJob(triggerKey);//
sched.deleteJob(jobKey); //
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*
*
* @param jobName
*
* @param jobGroupName
*
* @param triggerName
*
* @param triggerGroupName
*
*/
public static void removeJob(String jobName, String jobGroupName, String triggerName, String triggerGroupName) {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
TriggerKey triggerKey = TriggerKey.triggerKey(triggerName, triggerGroupName); // TriggerKey
JobKey jobKey = JobKey.jobKey(jobName, jobGroupName); // JobKey
sched.pauseTrigger(triggerKey); //
sched.unscheduleJob(triggerKey);//
sched.deleteJob(jobKey); //
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*
*/
public static void startJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
*
*/
public static void shutdownJobs() {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JPA + QueryDSL 계층형 댓글, 대댓글 구현(2)이번엔 전편에 이어서 계층형 댓글, 대댓글을 다시 리팩토링해볼 예정이다. 이전 게시글에서는 계층형 댓글, 대댓글을 구현은 되었지만 N+1 문제가 있었다. 이번에는 그 N+1 문제를 해결해 볼 것이다. 위의 로직은 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.