Quartz使用示例总结
任务调度在目前的JAVA应用程序中运用的十分普遍,故掌握QUARTZ是必备的技能
闲话少说,上官网:http://www.quartz-scheduler.org/ 下载最新1.80资源包
commons-logging log4j sff4j 等jar包要添加进去,quartz的jar包则肯定必须
首先写一个类实现Job接口
public class QuartzDemo implements Job{ @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("执行我......."); } }
public class Test { private static final SimpleTrigger CronTrigger = null; public static void main(String[] args){ } public void test1(){ //通过SchedulerFactory来获取一个调度器 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler; try { scheduler = schedulerFactory.getScheduler(); //引进作业程序 JobDetail jobDetail = new JobDetail("jobDetail-s1", "jobDetailGroup-s1", QuartzDemo.class); //new一个触发器 SimpleTrigger simpleTrigger = new SimpleTrigger("simpleTrigger", "triggerGroup-s1"); //设置作业启动时间 long ctime = System.currentTimeMillis(); simpleTrigger.setStartTime(new Date(ctime)); //设置作业执行间隔 simpleTrigger.setRepeatInterval(1000); //设置作业执行次数 simpleTrigger.setRepeatCount(10); //设置作业执行优先级默认为5 //simpleTrigger.setPriority(10); //作业和触发器设置到调度器中 scheduler.scheduleJob(jobDetail, simpleTrigger); //启动调度器 scheduler.start(); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void test2(){ try { SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(); Scheduler sched = schedFact.getScheduler(); sched.start(); JobDetail jobDetail = new JobDetail( " Income Report " , " Report Generation " , QuartzDemo.class ); jobDetail.getJobDataMap().put( " type " , " FULL " ); CronTrigger trigger = new CronTrigger( " Income Report " , " Report Generation " ); /**/ /* 每1分钟执行一次 */ trigger.setCronExpression( "0 33 16 * * ?" ); sched.scheduleJob(jobDetail, trigger); } catch (Exception e) { e.printStackTrace(); } } public void test3(){ //通过SchedulerFactory来获取一个调度器 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler; try { scheduler = schedulerFactory.getScheduler(); //引进作业程序 JobDetail jobDetail = new JobDetail("jobDetail-s1", "jobDetailGroup-s1", QuartzDemo.class); //new一个触发器 CronTrigger simpleTrigger = new CronTrigger("trigger", "group", "job", "group", "16 26/1 8-17 * * ?"); // new SimpleTrigger("simpleTrigger", "triggerGroup-s1"); //设置作业启动时间 //Calendar excelCal = Calendar.getInstance(); //excelCal.add(Calendar.DAY_OF_MONTH, 1); ///excelCal.set(Calendar.HOUR_OF_DAY, 16); //excelCal.set(Calendar.SECOND, 0); //excelCal.add(Calendar.MINUTE, 9); // long ctime = System.currentTimeMillis(); // simpleTrigger.setStartTime(excelCal.getTime()); //设置作业执行间隔 // simpleTrigger.setRepeatInterval(1000); //设置作业执行次数 // simpleTrigger.setRepeatCount(10); //设置作业执行优先级默认为5 //simpleTrigger.setPriority(10); //作业和触发器设置到调度器中 scheduler.scheduleJob(jobDetail, simpleTrigger); //启动调度器 scheduler.start(); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
需要注意的几点是经常会出现org.quartz.core.ErrorLogger : An error occured instantiating job to be executed.....这种异常,解决办法是
1)Job类必须有默认的无参构造方法,当然不覆盖的话类本身就是无参的构造方法 2)Job的scope必须是Public类型的,因为quartz根据反射机制实例化类,如果不是public的,无法对其暴露 3) Job类不能是内部类,原因同上,所以最好单独建类
后附加上我网上搜集的
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!