Search in sources :

Example 26 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory in project dq-easy-cloud by dq-open-cloud.

the class SimpleTriggerExample method run.

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.");
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) Logger(org.slf4j.Logger) Date(java.util.Date) DateBuilder.futureDate(org.quartz.DateBuilder.futureDate) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory)

Example 27 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory in project dq-easy-cloud by dq-open-cloud.

the class CronTriggerExample method run.

public void run() throws Exception {
    Logger log = LoggerFactory.getLogger(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 = newJob(SimpleJob.class).withIdentity("job1", "group1").build();
    CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0/20 * * * * ?")).build();
    Date ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " 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 = newJob(SimpleJob.class).withIdentity("job2", "group1").build();
    trigger = newTrigger().withIdentity("trigger2", "group1").withSchedule(cronSchedule("15 0/2 * * * ?")).build();
    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " 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 = newJob(SimpleJob.class).withIdentity("job3", "group1").build();
    trigger = newTrigger().withIdentity("trigger3", "group1").withSchedule(cronSchedule("0 0/2 8-17 * * ?")).build();
    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " 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 = newJob(SimpleJob.class).withIdentity("job4", "group1").build();
    trigger = newTrigger().withIdentity("trigger4", "group1").withSchedule(cronSchedule("0 0/3 17-23 * * ?")).build();
    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " 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 = newJob(SimpleJob.class).withIdentity("job5", "group1").build();
    trigger = newTrigger().withIdentity("trigger5", "group1").withSchedule(cronSchedule("0 0 10am 1,15 * ?")).build();
    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " 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 = newJob(SimpleJob.class).withIdentity("job6", "group1").build();
    trigger = newTrigger().withIdentity("trigger6", "group1").withSchedule(cronSchedule("0,30 * * ? * MON-FRI")).build();
    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " 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 = newJob(SimpleJob.class).withIdentity("job7", "group1").build();
    trigger = newTrigger().withIdentity("trigger7", "group1").withSchedule(cronSchedule("0,30 * * ? * SAT,SUN")).build();
    ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " 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.getNumberOfJobsExecuted() + " jobs.");
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) Logger(org.slf4j.Logger) Date(java.util.Date) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory)

Example 28 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory in project dq-easy-cloud by dq-open-cloud.

the class MisfireExample method run.

public void run() throws Exception {
    Logger log = LoggerFactory.getLogger(MisfireExample.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...
    Date startTime = nextGivenSecondDate(null, 15);
    // statefulJob1 will run every three seconds
    // (but it will delay for ten seconds)
    JobDetail job = newJob(StatefulDumbJob.class).withIdentity("statefulJob1", "group1").usingJobData(StatefulDumbJob.EXECUTION_DELAY, 10000L).build();
    SimpleTrigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(startTime).withSchedule(simpleSchedule().withIntervalInSeconds(3).repeatForever()).build();
    Date ft = sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
    // statefulJob2 will run every three seconds
    // (but it will delay for ten seconds - and therefore purposely misfire after a few iterations)
    job = newJob(StatefulDumbJob.class).withIdentity("statefulJob2", "group1").usingJobData(StatefulDumbJob.EXECUTION_DELAY, 10000L).build();
    trigger = newTrigger().withIdentity("trigger2", "group1").startAt(startTime).withSchedule(simpleSchedule().withIntervalInSeconds(3).repeatForever().withMisfireHandlingInstructionNowWithExistingCount()).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 ----------------");
    // jobs don't start firing until start() has been called...
    sched.start();
    log.info("------- Started Scheduler -----------------");
    try {
        // sleep for ten minutes for triggers to file....
        Thread.sleep(600L * 1000L);
    } catch (Exception e) {
    // 
    }
    log.info("------- Shutting Down ---------------------");
    sched.shutdown(true);
    log.info("------- Shutdown Complete -----------------");
    SchedulerMetaData metaData = sched.getMetaData();
    log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) Logger(org.slf4j.Logger) DateBuilder.nextGivenSecondDate(org.quartz.DateBuilder.nextGivenSecondDate) Date(java.util.Date) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory)

Example 29 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory in project dq-easy-cloud by dq-open-cloud.

the class MySimpleTriggerRunner method main.

public static void main(String[] args) throws SchedulerException {
    // 构建任务
    JobDetail jobDetail = JobBuilder.newJob(MySimpleJob.class).withIdentity("job1", "group1").build();
    Date beginDate = DateBuilder.futureDate(15, DateBuilder.IntervalUnit.SECOND);
    Date endDate = DateBuilder.futureDate(60, DateBuilder.IntervalUnit.SECOND);
    // 构建触发器
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "tgroup").startAt(beginDate).endAt(endDate).withSchedule(SimpleScheduleBuilder.repeatSecondlyForever()).build();
    // 执行计划工厂
    SchedulerFactory schedulerFactory = new StdSchedulerFactory();
    // 获取执行计划
    Scheduler scheduler = schedulerFactory.getScheduler();
    // 将任务触发器放入设置到执行计划中
    scheduler.scheduleJob(jobDetail, trigger);
    // 开始
    scheduler.start();
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) Date(java.util.Date) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory)

Example 30 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory in project dq-easy-cloud by dq-open-cloud.

the class PlugInExample method run.

public void run() throws Exception {
    Logger log = LoggerFactory.getLogger(PlugInExample.class);
    // First we must get a reference to a scheduler
    SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler sched = null;
    try {
        sched = sf.getScheduler();
    } catch (NoClassDefFoundError e) {
        log.error(" Unable to load a class - most likely you do not have jta.jar on the classpath. If not present in the examples/lib folder, please " + "add it there for this sample to run.", e);
        return;
    }
    log.info("------- Initialization Complete -----------");
    log.info("------- (Not Scheduling any Jobs - relying on XML definitions --");
    log.info("------- Starting Scheduler ----------------");
    // start the schedule
    sched.start();
    log.info("------- Started Scheduler -----------------");
    log.info("------- Waiting five minutes... -----------");
    // wait five minutes to give our jobs a chance to run
    try {
        Thread.sleep(300L * 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.");
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) Scheduler(org.quartz.Scheduler) Logger(org.slf4j.Logger) SchedulerMetaData(org.quartz.SchedulerMetaData) SchedulerFactory(org.quartz.SchedulerFactory) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory)

Aggregations

StdSchedulerFactory (org.quartz.impl.StdSchedulerFactory)66 SchedulerFactory (org.quartz.SchedulerFactory)29 SchedulerException (org.quartz.SchedulerException)22 Scheduler (org.quartz.Scheduler)19 JobDetail (org.quartz.JobDetail)17 Logger (org.slf4j.Logger)14 Date (java.util.Date)11 Properties (java.util.Properties)11 Trigger (org.quartz.Trigger)11 JobKey (org.quartz.JobKey)7 JobDataMap (org.quartz.JobDataMap)6 TriggerBuilder.newTrigger (org.quartz.TriggerBuilder.newTrigger)5 InputStream (java.io.InputStream)4 Test (org.junit.Test)4 DateBuilder.nextGivenSecondDate (org.quartz.DateBuilder.nextGivenSecondDate)4 SimpleTrigger (org.quartz.SimpleTrigger)4 ParseException (java.text.ParseException)3 CronTrigger (org.quartz.CronTrigger)3 SimpleThreadPool (org.quartz.simpl.SimpleThreadPool)3 QuartzManager (com.example.quartz.QuartzManager)2