Quartz 시스템 (1): 정시 작업 스케줄 링 실현
14709 단어 quartz
:
1、
2、 , ,
3、ScheduleJob
4、
5、quartz 1.8 spring 3.0
public class QuartzManager {
private static Logger logger = Logger.getLogger(QuartzManager.class);
private static SchedulerFactory gSchedulerFactory = new StdSchedulerFactory();
private static String JOB_GROUP_NAME = "BIGDATA_JOBGROUP_NAME";
private static String TRIGGER_GROUP_NAME = "BIGDATA_TRIGGERGROUP_NAME";
/**
* @Description: , , ,
*
* @param jobName
*
* @param cls
*
* @param time
* , quartz
* @throws Throwable
*/
@SuppressWarnings("unchecked")
public static void addJob(ScheduleJob job) throws Exception {
try {
addJob(job.getJobid(), JOB_GROUP_NAME, job.getJobid(), TRIGGER_GROUP_NAME,job, job.getCronExpression());
}catch(SchedulerException e){
e.printStackTrace();
if(e.getMessage().contains("the given trigger will never fire")){
throw new SchedulerException(" [ ]!");
}
}catch (Exception e) {
e.printStackTrace();
throw new Exception(" :"+e.getMessage());
}
}
/**
* @Description:
*
* @param jobName
*
* @param jobGroupName
*
* @param triggerName
*
* @param triggerGroupName
*
* @param jobClass
*
* @param time
* , quartz
* @throws ClassNotFoundException
* @throws ParseException
* @throws Exception
*/
@SuppressWarnings("unchecked")
private static void addJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName, ScheduleJob job,
String time) throws SchedulerException, ClassNotFoundException, ParseException {
logger.info("------- ----");
Scheduler sched = gSchedulerFactory.getScheduler();
logger.info("------------- :"+job.getJobclass());
if(job.getJobclass()==null){
throw new SchedulerException(" !");
}
Class c = Class.forName(job.getJobclass());
JobDetail jobDetail = new JobDetail(jobName, jobGroupName, c);// , ,
jobDetail.getJobDataMap().put("scheduleJob", job); //
//
CronTrigger trigger = new CronTrigger(triggerName, triggerGroupName);// ,
SimpleDateFormat sdf = new SimpleDateFormat();
if(job.getStartTime()!=null){
String startt= job.getStartTime();
if(startt.length()==4){
//yyyy
sdf.applyPattern("yyyy");
}else if(startt.length()==7){
//yyyy-MM
sdf.applyPattern("yyyy-MM");
}else if(startt.length()==10){
//yyyy-MM-dd
sdf.applyPattern("yyyy-MM-dd");
}else{
//
}
trigger.setStartTime(sdf.parse(startt));
}
if(job.getEndTime()!=null){
String endt= job.getEndTime();
if(endt.length()==4){
//yyyy
sdf.applyPattern("yyyy");
}else if(endt.length()==7){
//yyyy-MM
sdf.applyPattern("yyyy-MM");
}else if(endt.length()==10){
//yyyy-MM-dd
sdf.applyPattern("yyyy-MM-dd");
}else{
//
}
trigger.setEndTime(sdf.parse(endt));
}
trigger.setCronExpression(time);//
sched.scheduleJob(jobDetail, trigger);
//
if (!sched.isStarted()) {
sched.start();
}
}
public static void addJob(String jobName,Class clz,
String time) throws Exception {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
logger.info("------------- :"+clz);
JobDetail jobDetail = new JobDetail(jobName, JOB_GROUP_NAME, clz);// , ,
//
CronTrigger trigger = new CronTrigger(jobName, TRIGGER_GROUP_NAME);// ,
trigger.setCronExpression(time);//
sched.scheduleJob(jobDetail, trigger);
//
if (!sched.isStarted()) {
sched.start();
}
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* @Description: ( , , )
*
* @param jobName
* @param time
* @throws Exception
*
*/
@SuppressWarnings("unchecked")
public static void modifyJobTime(String jobName, String time) throws Exception {
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);
}
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* @Description:
*
* @param triggerName
* @param triggerGroupName
* @param time
* @throws Exception
*/
public static void modifyJobTime(String triggerName,
String triggerGroupName, String time) throws Exception {
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 Exception(e);
}
}
/**
* @Description: ( , , )
*
* @param jobName
* @throws Exception
*
*/
public static void removeJob(String jobName) throws Exception {
removeJob(jobName, JOB_GROUP_NAME, jobName, TRIGGER_GROUP_NAME);
}
/**
* @Description:
*
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
* @throws Exception
*
*/
public static void removeJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) throws Exception {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(triggerName, triggerGroupName);//
sched.unscheduleJob(triggerName, triggerGroupName);//
sched.deleteJob(jobName, jobGroupName);//
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* @Description:
*
* @param jobName
* @param jobGroupName
* @param triggerName
* @param triggerGroupName
* @throws Exception
*
*/
public static void pauseJob(String jobName) throws Exception {
pauseJob(jobName, JOB_GROUP_NAME, jobName, TRIGGER_GROUP_NAME);
}
public static void pauseJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) throws Exception {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseTrigger(triggerName, triggerGroupName);//
// sched.pauseJob(jobName, jobGroupName);
//
if (!sched.isStarted()) {
logger.info(" , , ");
sched.start();
}
} catch (Exception e) {
throw new Exception(e);
}
}
/**
*
* @param triggerName
* @param triggerGroupName
* @param time
* @throws Exception
*/
public static void resumeStopJob(String jobName) throws Exception {
resumeStopJob(jobName, JOB_GROUP_NAME, jobName, TRIGGER_GROUP_NAME);
}
public static void resumeStopJob(String jobName, String jobGroupName,
String triggerName, String triggerGroupName) throws Exception {
try {
logger.info(" !");
Scheduler sched = gSchedulerFactory.getScheduler();
// CronTrigger trigger = (CronTrigger) sched.getTrigger(triggerName,
// triggerGroupName);
// JobDetail job = sched.getJobDetail(jobName, jobGroupName);
sched.resumeTrigger(triggerName, triggerGroupName);
// sched.resumeJob(jobName, jobGroupName);
// if(job!=null){
// logger.info(" !");
//// sched.resumeJob(jobName, jobName);
// }
if (!sched.isStarted()) {
logger.info(" , , ");
sched.start();
}
logger.info(" !"+jobName);
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* @throws Exception
* @Description:
*
*/
public static void startJobs() throws Exception {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.start();
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* @throws Exception
* @Description:
*
*/
public static void shutdownJobs() throws Exception {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
if (!sched.isShutdown()) {
sched.shutdown();
}
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* @throws Exception
* @Description:
*
*/
public static void shutdownJob(String jobname) throws Exception {
try {
logger.info(" !");
Scheduler sched = gSchedulerFactory.getScheduler();
sched.deleteJob(jobname, JOB_GROUP_NAME);
logger.info(" !"+jobname);
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* @throws Exception
* @Description:
*
*/
public static void pauseAllJobs() throws Exception {
try {
Scheduler sched = gSchedulerFactory.getScheduler();
sched.pauseAll();
} catch (Exception e) {
throw new Exception(e);
}
}
public static void main(String[] args) {
try {
String job_name = " ";
System.out.println("【 】 ( 1 )...");
QuartzManager.addJob(job_name, QuartzJobFactory.class, "0/1 * * * * ?");
Thread.sleep(5000);
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, QuartzJobFactory.class, "*/10 * * * * ?");
Thread.sleep(60000);
System.out.println("【 】 ...");
QuartzManager.removeJob(job_name);
System.out.println("【 】 ");
} catch (Exception e) {
e.printStackTrace();
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Spring 통합 Quartz의 간단한 구성 방법그러나 실제 업무에서 직접 그것을 사용하는 것은 매우 드물다.일반적으로spring-quartz 구성 요소를 사용하며, 직접 설정을 통해spring 프레임워크를 자동으로 조립합니다 다음은spring 프레임워크 통합qu...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.