日期:2014-05-17 浏览次数:20929 次
package servlet;
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;
import org.quartz.impl.StdSchedulerFactory;
public class UniversalQuartzManager {
private static UniversalQuartzManager instance = null;
private Scheduler sched = null;
private SchedulerFactory factory = null;
private UniversalQuartzManager() {
_init();
}
private void _init() {
factory = new 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();
// 手动添加了时间
System.out.println("UniversalQuartzManager");
trigger.setCronExpression("2 * * * * ?");
// 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.shutdo