最近在学习quartz的job功能,自己写了一个简单的类!
1.schedulerManager 类
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.CronScheduleBuilder.cronSchedule;
//String time 格式 "0/20 * * * * ?" will run every 20 seconds
public class schedulerManager {
private SchedulerFactory sf= new StdSchedulerFactory();
private Scheduler sched= null;
public schedulerManager() throws SchedulerException {
// TODO Auto-generated constructor stub
sched=sf.getScheduler();
}
public Scheduler getScheduler(){
Scheduler sched= this.sched;
return sched;
}
public void StartJob() throws SchedulerException{
sched.start();
}
public void EndJob() throws SchedulerException{
sched.shutdown( true);
}
public int addJob(String jobName,String jobGroups,Job job,String time){
//必须进行job排除同名同组的 0:添加成功 1:job同名同组 2:异常
int i=0;
JobKey jobkey= new JobKey(jobName,jobGroups);
try{
if(sched.checkExists(jobkey)){
i=1;
return i;
}} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println(e.getMessage());
}
JobDetail jobdetail=(JobDetail)newJob(job.getClass())
.withIdentity(jobName, jobGroups)
.build();
CronTrigger trigger=newTrigger()
.withIdentity(jobName, jobGroups)
.withSchedule(cronSchedule(time))
.build();
try{
sched.scheduleJob(jobdetail, trigger);} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println(e.getMessage());
i=2;
}
return i;
}
public int removeJob(String jobName,String jobGroups){
// 0:remove成功 1:无此job 2:异常
int i=0;
JobKey jobkey= new JobKey(jobName,jobGroups);
try{
if(!sched.checkExists(jobkey)){
i=1;
return i;
}
sched.pauseJob(jobkey);
sched.deleteJob(jobkey);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
i=2;
}
return i;
}
public int ModifyJob(String jobName,String jobGroups,String time){
// 0:remove成功 1:无此job 2:异常
int i=0;
JobKey jobkey= new JobKey(jobName,jobGroups);
try{
if(!sched.checkExists(jobkey)){
i=1;
return i;
}
CronTrigger trigger=newTrigger()
.withIdentity(jobName, jobGroups)
.withSchedule(cronSchedule(time))
.build();
sched.rescheduleJob(trigger.getKey(), trigger);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.out.println(e.getMessage());
i=2;
}
return i;
}
}
2.test测试类
import org.quartz.Job;
import org.quartz.SchedulerException;
public class test {
/**
* @param args
* @throws SchedulerException
*/
public static void main(String[] args) throws SchedulerException {
// TODO Auto-generated method stub
schedulerManager zz= new schedulerManager();
HelloJob newhe= new HelloJob();
zz.addJob( "zz01", "zzgroup01", newhe, "0/2 * * * * ?");
int i=zz.addJob( "zz02", "zzgroup01", newhe, "0/2 * * * * ?");
System.out.println(i);
zz.StartJob();
try{
Thread.sleep(30L * 1000L);
} catch (Exception e) {
// TODO: handle exception
}
zz.EndJob();
}
}