package com.abin.lee.quartz.all;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class UniversalQuartz implements Job{
public void execute(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("执行定时任务: "+System.currentTimeMillis());
}
}
package com.abin.lee.quartz.all;
import java.text.ParseException;
import java.util.Date;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
public class UniversalQuartzManager {
private static UniversalQuartzManager instance = null;
private Scheduler sched = null;
private SchedulerFactory factory = null;
private UniversalQuartzManager() {
_init();
}
private void _init() {
factory = new org.quartz.impl.StdSchedulerFactory();
try {
sched = factory.getScheduler();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public synchronized static UniversalQuartzManager getInstance() {
if (instance == null) {
instance = new UniversalQuartzManager();
}
return instance;
}
public void start() {
try {
sched.start();
// 在这里加载需要执行的业务逻辑类UniversalQuartz.class
JobDetail detail = new JobDetail("cmhksynchron", "cmhkSynch",
UniversalQuartz.class);
CronTrigger trigger = new CronTrigger();
// 手动添加了时间
trigger.setCronExpression("0/10 * * * * ? *");
// trigger.setCronExpression("0 0-59 16 * * ?");
trigger.setName("abintrigger");
trigger.setStartTime(new Date());
sched.scheduleJob(detail, trigger);
} catch (SchedulerException e) {
e.printStackTrace();
} catch (ParseException e2) {
e2.printStackTrace();
}
}
public void stop() {
try {
sched.shutdown();
sched = factory.getScheduler();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
public void refresh() {
// 未知的原因,程序执行这段判断进入stop(),在sched.shutdown();这一句报错,//所以暂时将其屏蔽,以待后来解决
// try {
// if(sched != null && sched.isStarted()) {
// this.stop();
// }
this.start();
// } catch (SchedulerException e) {
// e.printStackTrace();
// }
}
}
package com.abinl.lee.quartz.all.test;
import com.abin.lee.quartz.all.UniversalQuartzManager;
public class TestQuartz {
public static void main(String[] args) {
UniversalQuartzManager.getInstance().refresh();
}
}