/** * This example will spawn a large number of jobs to run * * @author James House, Bill Kratzer */ public void run() throws Exception { Logger log = LoggerFactory.getLogger(RemoteServerExample.class); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- 初始化完成 -----------"); log.info("------- (Not Scheduling any Jobs - relying on a remote client to schedule jobs --"); log.info("------- 开始调度工作 ----------------"); // start the schedule sched.start(); log.info("------- 调度工作开始 -----------------"); log.info("------- 等待 ten minutes... ------------"); // wait five minutes to give our jobs a chance to run try { Thread.sleep(600L * 1000L); } catch (Exception e) { } // shut down the scheduler log.info("------- 关闭 ---------------------"); sched.shutdown(true); log.info("------- 关闭完成 -----------------"); SchedulerMetaData metaData = sched.getMetaData(); log.info("执行" + metaData.getNumberOfJobsExecuted() + "工作。"); }
/** * This example will spawn a large number of jobs to run * * @author James House, Bill Kratzer */ public void run() throws Exception { Logger log = LoggerFactory.getLogger(RemoteServerExample.class); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete -----------"); log.info("------- (Not Scheduling any Jobs - relying on a remote client to schedule jobs --"); log.info("------- Starting Scheduler ----------------"); // start the schedule sched.start(); log.info("------- Started Scheduler -----------------"); log.info("------- Waiting ten minutes... ------------"); // wait five minutes to give our jobs a chance to run try { Thread.sleep(600L * 1000L); } catch (Exception e) { // } // shut down the scheduler log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); }
public void schedulerShutdown() { try { getLog().debug("----------------- Scheduler " + scheduler.getSchedulerName() + " shutdown"); SchedulerMetaData metaData = scheduler.getMetaData(); getLog().info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); } catch (SchedulerException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void run() throws Exception { Log log = LogFactory.getLog(CronTriggerExample.class); log.info("------- Initializing -------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete --------"); log.info("------- Scheduling Jobs ----------------"); // jobs can be scheduled before sched.start() has been called // job 1 will run every 20 seconds JobDetail job = new JobDetail("job1", "group1", SimpleJob.class); CronTrigger trigger = new CronTrigger("trigger1", "group1", "job1", "group1", "0/20 * * * * ?"); sched.addJob(job, true); Date ft = sched.scheduleJob(trigger); log.info( job.getFullName() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression()); // job 2 will run every other minute (at 15 seconds past the minute) job = new JobDetail("job2", "group1", SimpleJob.class); trigger = new CronTrigger("trigger2", "group1", "job2", "group1", "15 0/2 * * * ?"); sched.addJob(job, true); ft = sched.scheduleJob(trigger); log.info( job.getFullName() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression()); // job 3 will run every other minute but only between 8am and 5pm job = new JobDetail("job3", "group1", SimpleJob.class); trigger = new CronTrigger("trigger3", "group1", "job3", "group1", "0 0/2 8-17 * * ?"); sched.addJob(job, true); ft = sched.scheduleJob(trigger); log.info( job.getFullName() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression()); // job 4 will run every three minutes but only between 5pm and 11pm job = new JobDetail("job4", "group1", SimpleJob.class); trigger = new CronTrigger("trigger4", "group1", "job4", "group1", "0 0/3 17-23 * * ?"); sched.addJob(job, true); ft = sched.scheduleJob(trigger); log.info( job.getFullName() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression()); // job 5 will run at 10am on the 1st and 15th days of the month job = new JobDetail("job5", "group1", SimpleJob.class); trigger = new CronTrigger("trigger5", "group1", "job5", "group1", "0 0 10am 1,15 * ?"); sched.addJob(job, true); ft = sched.scheduleJob(trigger); log.info( job.getFullName() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression()); // job 6 will run every 30 seconds but only on Weekdays (Monday through // Friday) job = new JobDetail("job6", "group1", SimpleJob.class); trigger = new CronTrigger("trigger6", "group1", "job6", "group1", "0,30 * * ? * MON-FRI"); sched.addJob(job, true); ft = sched.scheduleJob(trigger); log.info( job.getFullName() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression()); // job 7 will run every 30 seconds but only on Weekends (Saturday and // Sunday) job = new JobDetail("job7", "group1", SimpleJob.class); trigger = new CronTrigger("trigger7", "group1", "job7", "group1", "0,30 * * ? * SAT,SUN"); sched.addJob(job, true); ft = sched.scheduleJob(trigger); log.info( job.getFullName() + " has been scheduled to run at: " + ft + " and repeat based on expression: " + trigger.getCronExpression()); log.info("------- Starting Scheduler ----------------"); // All of the jobs have been added to the scheduler, but none of the // jobs // will run until the scheduler has been started sched.start(); log.info("------- Started Scheduler -----------------"); log.info("------- Waiting five minutes... ------------"); try { // wait five minutes to show jobs Thread.sleep(300L * 1000L); // executing... } catch (Exception e) { } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.numJobsExecuted() + " jobs."); }
public void run() throws Exception { Log log = LogFactory.getLog(JobExceptionExample.class); log.info("------- Initializing ----------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete ------------"); log.info("------- Scheduling Jobs -------------------"); // jobs can be scheduled before start() has been called // get a "nice round" time a few seconds in the future... long ts = TriggerUtils.getNextGivenSecondDate(null, 15).getTime(); // badJob1 will run every three seconds // this job will throw an exception and refire // immediately JobDetail job = new JobDetail("badJob1", "group1", BadJob1.class); SimpleTrigger trigger = new SimpleTrigger( "trigger1", "group1", new Date(ts), null, SimpleTrigger.REPEAT_INDEFINITELY, 3000L); Date ft = sched.scheduleJob(job, trigger); log.info( job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // badJob2 will run every three seconds // this job will throw an exception and never // refire job = new JobDetail("badJob2", "group1", BadJob2.class); trigger = new SimpleTrigger( "trigger2", "group1", new Date(ts), null, SimpleTrigger.REPEAT_INDEFINITELY, 3000L); ft = sched.scheduleJob(job, trigger); log.info( job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); log.info("------- Starting Scheduler ----------------"); // jobs don't start firing until start() has been called... sched.start(); log.info("------- Started Scheduler -----------------"); try { // sleep for 60 seconds Thread.sleep(60L * 1000L); } catch (Exception e) { } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.numJobsExecuted() + " jobs."); }
public void run() throws Exception { Logger log = LoggerFactory.getLogger(SimpleTriggerExample.class); log.info("------- Initializing -------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete --------"); log.info("------- Scheduling Jobs ----------------"); // jobs can be scheduled before sched.start() has been called // get a "nice round" time a few seconds in the future... long ts = TriggerUtils.getNextGivenSecondDate(null, 5).getTime(); sched.start(); log.info("------- Started Scheduler -----------------"); // jobs can also be scheduled after start() has been called... // job7 will repeat 20 times, repeat every 3 sec JobDetail job = new JobDetail("job7", "group1", SimpleJob.class); SimpleTrigger trigger = new SimpleTrigger("trigger7", "group1", "job7", "group1", new Date(ts), null, 30, 2000L); Date ft = sched.scheduleJob(job, trigger); log.info( job.getFullName() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); log.info("------- Waiting 15 seconds... --------------"); try { Thread.sleep(15L * 1000L); } catch (Exception e) { } // jobs can be re-scheduled... // job 7 will run immediately and repeat 10 times for every second log.info("------- Rescheduling... --------------------"); trigger = new SimpleTrigger("trigger7", "group1", "job7", "group1", new Date(), null, 10, 1000L); ft = sched.rescheduleJob("trigger7", "group1", trigger); log.info("job7 rescheduled to run at: " + ft); log.info("------- Waiting five minutes... ------------"); try { Thread.sleep(300L * 1000L); } catch (Exception e) { } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); // display some stats about the schedule that just ran SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); }
public void run() throws Exception { Logger log = LoggerFactory.getLogger(SimpleTriggerExample.class); log.info("------- Initializing -------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete --------"); log.info("------- Scheduling Jobs ----------------"); // jobs can be scheduled before sched.start() has been called // get a "nice round" time a few seconds in the future... Date startTime = DateBuilder.nextGivenSecondDate(null, 15); // job1 will only fire once at date/time "ts" JobDetail job = newJob(SimpleJob.class).withIdentity("job1", "group1").build(); SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1").startAt(startTime).build(); // schedule it to run! Date ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job2 will only fire once at date/time "ts" job = newJob(SimpleJob.class).withIdentity("job2", "group1").build(); trigger = (SimpleTrigger) newTrigger().withIdentity("trigger2", "group1").startAt(startTime).build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job3 will run 11 times (run once and repeat 10 more times) // job3 will repeat every 10 seconds job = newJob(SimpleJob.class).withIdentity("job3", "group1").build(); trigger = newTrigger() .withIdentity("trigger3", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(10)) .build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // the same job (job3) will be scheduled by a another trigger // this time will only repeat twice at a 70 second interval trigger = newTrigger() .withIdentity("trigger3", "group2") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(2)) .forJob(job) .build(); ft = sched.scheduleJob(trigger); log.info( job.getKey() + " will [also] run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job4 will run 6 times (run once and repeat 5 more times) // job4 will repeat every 10 seconds job = newJob(SimpleJob.class).withIdentity("job4", "group1").build(); trigger = newTrigger() .withIdentity("trigger4", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(5)) .build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job5 will run once, five minutes in the future job = newJob(SimpleJob.class).withIdentity("job5", "group1").build(); trigger = (SimpleTrigger) newTrigger() .withIdentity("trigger5", "group1") .startAt(futureDate(5, IntervalUnit.MINUTE)) .build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // job6 will run indefinitely, every 40 seconds job = newJob(SimpleJob.class).withIdentity("job6", "group1").build(); trigger = newTrigger() .withIdentity("trigger6", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(40).repeatForever()) .build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); log.info("------- Starting Scheduler ----------------"); // All of the jobs have been added to the scheduler, but none of the jobs // will run until the scheduler has been started sched.start(); log.info("------- Started Scheduler -----------------"); // jobs can also be scheduled after start() has been called... // job7 will repeat 20 times, repeat every five minutes job = newJob(SimpleJob.class).withIdentity("job7", "group1").build(); trigger = newTrigger() .withIdentity("trigger7", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInMinutes(5).withRepeatCount(20)) .build(); ft = sched.scheduleJob(job, trigger); log.info( job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // jobs can be fired directly... (rather than waiting for a trigger) job = newJob(SimpleJob.class).withIdentity("job8", "group1").storeDurably().build(); sched.addJob(job, true); log.info("'Manually' triggering job8..."); sched.triggerJob(jobKey("job8", "group1")); log.info("------- Waiting 30 seconds... --------------"); try { // wait 33 seconds to show jobs Thread.sleep(30L * 1000L); // executing... } catch (Exception e) { } // jobs can be re-scheduled... // job 7 will run immediately and repeat 10 times for every second log.info("------- Rescheduling... --------------------"); trigger = newTrigger() .withIdentity("trigger7", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInMinutes(5).withRepeatCount(20)) .build(); ft = sched.rescheduleJob(trigger.getKey(), trigger); log.info("job7 rescheduled to run at: " + ft); log.info("------- Waiting five minutes... ------------"); try { // wait five minutes to show jobs Thread.sleep(300L * 1000L); // executing... } catch (Exception e) { } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); // display some stats about the schedule that just ran SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); }
public void run() throws Exception { Logger log = LoggerFactory.getLogger(JobStateExample.class); log.info("------- Initializing -------------------"); // First we must get a reference to a scheduler SchedulerFactory sf = new StdSchedulerFactory(); Scheduler sched = sf.getScheduler(); log.info("------- Initialization Complete --------"); log.info("------- Scheduling Jobs ----------------"); // get a "nice round" time a few seconds in the future.... Date startTime = nextGivenSecondDate(null, 10); // job1 will only run 5 times (at start time, plus 4 repeats), every 10 seconds JobDetail job1 = newJob(ColorJob.class).withIdentity("job1", "group1").build(); SimpleTrigger trigger1 = newTrigger() .withIdentity("trigger1", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(4)) .build(); // pass initialization parameters into the job job1.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Green"); job1.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1); // schedule the job to run Date scheduleTime1 = sched.scheduleJob(job1, trigger1); log.info( job1.getKey() + " will run at: " + scheduleTime1 + " and repeat: " + trigger1.getRepeatCount() + " times, every " + trigger1.getRepeatInterval() / 1000 + " seconds"); // job2 will also run 5 times, every 10 seconds JobDetail job2 = newJob(ColorJob.class).withIdentity("job2", "group1").build(); SimpleTrigger trigger2 = newTrigger() .withIdentity("trigger2", "group1") .startAt(startTime) .withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(4)) .build(); // pass initialization parameters into the job // this job has a different favorite color! job2.getJobDataMap().put(ColorJob.FAVORITE_COLOR, "Red"); job2.getJobDataMap().put(ColorJob.EXECUTION_COUNT, 1); // schedule the job to run Date scheduleTime2 = sched.scheduleJob(job2, trigger2); log.info( job2.getKey().toString() + " will run at: " + scheduleTime2 + " and repeat: " + trigger2.getRepeatCount() + " times, every " + trigger2.getRepeatInterval() / 1000 + " seconds"); log.info("------- Starting Scheduler ----------------"); // All of the jobs have been added to the scheduler, but none of the jobs // will run until the scheduler has been started sched.start(); log.info("------- Started Scheduler -----------------"); log.info("------- Waiting 60 seconds... -------------"); try { // wait five minutes to show jobs Thread.sleep(60L * 1000L); // executing... } catch (Exception e) { // } log.info("------- Shutting Down ---------------------"); sched.shutdown(true); log.info("------- Shutdown Complete -----------------"); SchedulerMetaData metaData = sched.getMetaData(); log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); }
public void cronScheduler() throws SchedulerException { logger.info("SimpleScheduler.simpleScheduler start..."); // 日期格式化 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); // 获得 Scheduler对象 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); // TODO job1 每20s运行一次 JobDetail job = newJob(CronJob.class).withIdentity("job1", "group1").build(); // cronTrigger CronTrigger cronTrigger = newTrigger() .withIdentity("trigger1", "group1") .withSchedule(cronSchedule("0/20 * * * * ?")) .build(); Date sd = scheduler.scheduleJob(job, cronTrigger); logger.info( job.getKey().getName() + " 将在: " + sdf.format(sd) + " 运行. \r基于Cron表达式 : " + cronTrigger.getCronExpression() + "(含义:每20s运行一次)"); // TODO job2偶数分钟每隔15秒 job = newJob(CronJob.class).withIdentity("job2", "group1").build(); // cronTrigger cronTrigger = newTrigger() .withIdentity("trigger2", "group1") .withSchedule(cronSchedule("15 0/2 * * * ?")) .build(); sd = scheduler.scheduleJob(job, cronTrigger); logger.info( job.getKey().getName() + " 将在: " + sdf.format(sd) + " 运行. \r基于Cron表达式 : " + cronTrigger.getCronExpression() + "(含义:偶数分钟每隔15秒运行一次)"); // TODO job3 9-18时,每偶数分钟 job = newJob(CronJob.class).withIdentity("job3", "group1").build(); // cronTrigger cronTrigger = newTrigger() .withIdentity("trigger3", "group1") .withSchedule(cronSchedule("0 0/2 9-1 * * ?")) .build(); sd = scheduler.scheduleJob(job, cronTrigger); logger.info( job.getKey().getName() + " 将在: " + sdf.format(sd) + " 运行. \r基于Cron表达式 : " + cronTrigger.getCronExpression() + "(含义:9-18时,每偶数分钟运行一次)"); // TODO job4 9-12时,每3分钟 job = newJob(CronJob.class).withIdentity("job4", "group1").build(); // cronTrigger cronTrigger = newTrigger() .withIdentity("trigger4", "group1") .withSchedule(cronSchedule("0 0/3 9-12 * * ?")) .build(); sd = scheduler.scheduleJob(job, cronTrigger); logger.info( job.getKey().getName() + " 将在: " + sdf.format(sd) + " 运行. \r基于Cron表达式 : " + cronTrigger.getCronExpression() + "(含义:9-12时,每3分钟运行一次)"); // TODO job5 每个月的1号,15号上午10点运行 job = newJob(CronJob.class).withIdentity("job5", "group1").build(); // cronTrigger cronTrigger = newTrigger() .withIdentity("trigger5", "group1") .withSchedule(cronSchedule("0 0 10am 1,15 * ?")) .build(); sd = scheduler.scheduleJob(job, cronTrigger); logger.info( job.getKey().getName() + " 将在: " + sdf.format(sd) + " 运行. \r基于Cron表达式 : " + cronTrigger.getCronExpression() + "(含义:每个月的1号,15号上午10点各运行一次)"); // TODO job6 周一到周五,每隔30秒运行一次 job = newJob(CronJob.class).withIdentity("job6", "group1").build(); // cronTrigger cronTrigger = newTrigger() .withIdentity("trigger6", "group1") .withSchedule(cronSchedule("0,30 * * ? * MON-FRI")) .build(); sd = scheduler.scheduleJob(job, cronTrigger); logger.info( job.getKey().getName() + " 将在: " + sdf.format(sd) + " 运行. \r基于Cron表达式 : " + cronTrigger.getCronExpression() + "(含义:周一到周五,每隔30秒运行一次)"); // TODO job7周六周日,每隔30秒运行一次 job = newJob(CronJob.class).withIdentity("job7", "group1").build(); // cronTrigger cronTrigger = newTrigger() .withIdentity("trigger7", "group1") .withSchedule(cronSchedule("0,30 * * ? * SAT,SUN")) .build(); sd = scheduler.scheduleJob(job, cronTrigger); logger.info( job.getKey().getName() + " 将在: " + sdf.format(sd) + " 运行. \r基于Cron表达式 : " + cronTrigger.getCronExpression() + "(含义:周六,周日 每30秒运行 )"); // 所有的任务都被加入到了 scheduler中 ,但只有 schedulers.start(); 时才开始执行 scheduler.start(); logger.info("=========================启动调度(调用start()方法)========================="); logger.info("系统启动的时间 :" + sdf.format(new Date())); try { logger.info("等待5分钟..."); Thread.sleep(300L * 1000L); } catch (Exception e) { } scheduler.shutdown(true); logger.info("=========================调度已关闭========================="); SchedulerMetaData schedulerMetaData = scheduler.getMetaData(); logger.info("Summary:" + schedulerMetaData.getSummary()); }