자바 동적 증가 정시 작업
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 로 이 루어 졌 습 니 다.
다음으로 전송:https://www.cnblogs.com/foreverzxb/p/6491190.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.