quartz定时器
import java.text.ParseException; 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; /*** * * @author 刘毅 * @date 2010-1-23 * @param 定时器简单封装(待运行的类需实现job) */ public class QuartzMethod { public SchedulerFactory sf = new StdSchedulerFactory(); public Scheduler sched; public JobDetail job; public CronTrigger cron; /** * 设置定时器信息(工作名,组名,类名) * */ public void setQuartzInfo(String taskName,String taskGroup,Object obj){ try { sched = sf.getScheduler(); job = new JobDetail(taskName,taskGroup,(Class<?>) obj); } catch (SchedulerException e) { e.printStackTrace(); } } /** * 工作名,组名,时间 * */ public void setCronTrigger(String triggerName,String taskGroup,String setTime){ try { cron = new CronTrigger(triggerName,triggerName,setTime); } catch (ParseException e) { e.printStackTrace(); } } /** * 设置休眠时间 * */ public void setSleep(long setSleepTime){ try { Thread.sleep(setSleepTime); } catch (InterruptedException e) { e.printStackTrace(); } } /** * 运行定时器 * */ public void run(){ try { sched.scheduleJob(job, cron); sched.start(); } catch (SchedulerException e) { e.printStackTrace(); } } } public static void main(String[] args) { QuartzMethod qm = new QuartzMethod(); qm.setQuartzInfo("job", "groutp",SimpleJob.class); qm.setCronTrigger("job1","groutp","*/2 * * * * ?"); qm.run(); }