Search in sources :

Example 51 with StdSchedulerFactory

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

the class SimpleExample method run.

public void run() throws Exception {
    Logger log = LoggerFactory.getLogger(SimpleExample.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 -----------");
    // computer a time that is on the next round minute
    Date runTime = evenMinuteDate(new Date());
    log.info("------- Scheduling Job  -------------------");
    // define the job and tie it to our HelloJob class
    JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
    // Trigger the job to run on the next round minute
    Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
    // Tell worker to schedule the job using our trigger
    sched.scheduleJob(job, trigger);
    log.info(job.getKey() + " will run at: " + runTime);
    // Start up the scheduler (nothing can actually run until the
    // scheduler has been started)
    sched.start();
    log.info("------- Started Scheduler -----------------");
    // wait long enough so that the scheduler as an opportunity to
    // run the job!
    log.info("------- Waiting 65 seconds... -------------");
    try {
        // wait 65 seconds to show job
        Thread.sleep(65L * 1000L);
    // executing...
    } catch (Exception e) {
    // 
    }
    // shut down the scheduler
    log.info("------- Shutting Down ---------------------");
    sched.shutdown(true);
    log.info("------- Shutdown Complete -----------------");
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Scheduler(org.quartz.Scheduler) Logger(org.slf4j.Logger) Date(java.util.Date) DateBuilder.evenMinuteDate(org.quartz.DateBuilder.evenMinuteDate) SchedulerFactory(org.quartz.SchedulerFactory) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory)

Example 52 with StdSchedulerFactory

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

the class JobExceptionExample method run.

public void run() throws Exception {
    Logger log = LoggerFactory.getLogger(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...
    Date startTime = nextGivenSecondDate(null, 15);
    // badJob1 will run every 10 seconds
    // this job will throw an exception and refire
    // immediately
    JobDetail job = newJob(BadJob1.class).withIdentity("badJob1", "group1").usingJobData("denominator", "0").build();
    SimpleTrigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(startTime).withSchedule(simpleSchedule().withIntervalInSeconds(10).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");
    // badJob2 will run every five seconds
    // this job will throw an exception and never
    // refire
    job = newJob(BadJob2.class).withIdentity("badJob2", "group1").build();
    trigger = newTrigger().withIdentity("trigger2", "group1").startAt(startTime).withSchedule(simpleSchedule().withIntervalInSeconds(5).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 ----------------");
    // jobs don't start firing until start() has been called...
    sched.start();
    log.info("------- Started Scheduler -----------------");
    try {
        // sleep for 30 seconds
        Thread.sleep(30L * 1000L);
    } catch (Exception e) {
    // 
    }
    log.info("------- Shutting Down ---------------------");
    sched.shutdown(false);
    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 53 with StdSchedulerFactory

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

the class JDBCJobStoreRunner method run.

public void run(boolean inClearJobs, boolean inScheduleJobs) throws Exception {
    // First we must get a reference to a scheduler
    SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler sched = sf.getScheduler();
    if (inClearJobs) {
        _log.warn("***** Deleting existing jobs/triggers *****");
        sched.clear();
    }
    _log.info("------- Initialization Complete -----------");
    if (inScheduleJobs) {
        _log.info("------- Scheduling Jobs ------------------");
        String schedId = sched.getSchedulerInstanceId();
        int count = 2;
        JobDetail job = // put triggers in group
        newJob(SimpleRecoveryJob.class).withIdentity("job_" + count, schedId).requestRecovery().build();
        SimpleTrigger trigger = newTrigger().withIdentity("triger_" + count, schedId).startAt(futureDate(1, IntervalUnit.SECOND)).withSchedule(simpleSchedule().withRepeatCount(20).withIntervalInSeconds(5)).build();
        _log.info(job.getKey() + " will run at: " + trigger.getNextFireTime() + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
        sched.scheduleJob(job, trigger);
        count++;
        job = // put triggers in group named after
        newJob(SimpleRecoveryJob.class).withIdentity("job_" + count, schedId).requestRecovery().build();
        trigger = newTrigger().withIdentity("triger_" + count, schedId).startAt(futureDate(2, IntervalUnit.SECOND)).withSchedule(simpleSchedule().withRepeatCount(20).withIntervalInSeconds(5)).build();
        _log.info(job.getKey() + " will run at: " + trigger.getNextFireTime() + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
        sched.scheduleJob(job, trigger);
        count++;
        job = // put triggers in group named
        newJob(SimpleRecoveryStatefulJob.class).withIdentity("job_" + count, schedId).requestRecovery().build();
        trigger = newTrigger().withIdentity("triger_" + count, schedId).startAt(futureDate(1, IntervalUnit.SECOND)).withSchedule(simpleSchedule().withRepeatCount(20).withIntervalInSeconds(3)).build();
        _log.info(job.getKey() + " will run at: " + trigger.getNextFireTime() + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
        sched.scheduleJob(job, trigger);
        count++;
        job = // put triggers in group named after
        newJob(SimpleRecoveryJob.class).withIdentity("job_" + count, schedId).requestRecovery().build();
        trigger = newTrigger().withIdentity("triger_" + count, schedId).startAt(futureDate(1, IntervalUnit.SECOND)).withSchedule(simpleSchedule().withRepeatCount(20).withIntervalInSeconds(4)).build();
        _log.info(job.getKey() + " will run at: " + trigger.getNextFireTime() + " & repeat: " + trigger.getRepeatCount() + "/" + trigger.getRepeatInterval());
        sched.scheduleJob(job, trigger);
        count++;
        job = // put triggers in group named after
        newJob(SimpleRecoveryJob.class).withIdentity("job_" + count, schedId).requestRecovery().build();
        trigger = newTrigger().withIdentity("triger_" + count, schedId).startAt(futureDate(1, IntervalUnit.SECOND)).withSchedule(simpleSchedule().withRepeatCount(20).withIntervalInMilliseconds(4500L)).build();
        _log.info(job.getKey() + " will run at: " + trigger.getNextFireTime() + " & repeat: " + trigger.getRepeatCount() + "/" + trigger.getRepeatInterval());
        sched.scheduleJob(job, trigger);
    }
    // jobs don't start firing until start() has been called...
    _log.info("------- Starting Scheduler ---------------");
    sched.start();
    _log.info("------- Started Scheduler ----------------");
    _log.info("------- Waiting for one hour... ----------");
    try {
        Thread.sleep(3600L * 1000L);
    } catch (Exception e) {
    // 
    }
    _log.info("------- Shutting Down --------------------");
    sched.shutdown();
    _log.info("------- Shutdown Complete ----------------");
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) JobDetail(org.quartz.JobDetail) SimpleRecoveryJob(com.easy.cloud.core.worker.examples.example15.SimpleRecoveryJob) Scheduler(org.quartz.Scheduler) SimpleTrigger(org.quartz.SimpleTrigger) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerFactory(org.quartz.SchedulerFactory)

Example 54 with StdSchedulerFactory

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

the class CalendarExample method run.

public void run() throws Exception {
    final Logger log = LoggerFactory.getLogger(CalendarExample.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 -------------------");
    // Add the holiday calendar to the schedule
    AnnualCalendar holidays = new AnnualCalendar();
    // fourth of July (July 4)
    Calendar fourthOfJuly = new GregorianCalendar(2018, 6, 14);
    holidays.setDayExcluded(fourthOfJuly, true);
    // halloween (Oct 31)
    Calendar halloween = new GregorianCalendar(2005, 9, 31);
    holidays.setDayExcluded(halloween, true);
    // christmas (Dec 25)
    Calendar christmas = new GregorianCalendar(2005, 11, 25);
    holidays.setDayExcluded(christmas, true);
    // tell the schedule about our holiday calendar
    sched.addCalendar("holidays", holidays, false, false);
    // schedule a job to run hourly, starting on halloween
    // at 10 am
    Date runDate = dateOf(0, 0, 10, 31, 10);
    runDate = new Date();
    JobDetail job = newJob(SimpleJob.class).withIdentity("job1", "group1").build();
    SimpleTrigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runDate).withSchedule(SimpleScheduleBuilder.repeatSecondlyForever()).modifiedByCalendar("holidays").build();
    // schedule the job and print the first run date
    Date firstRunTime = sched.scheduleJob(job, trigger);
    // print out the first execution date.
    // Note: Since Halloween (Oct 31) is a holiday, then
    // we will not run until the next day! (Nov 1)
    log.info(job.getKey() + " will run at: " + firstRunTime + " and repeat: " + trigger.getRepeatCount() + " times, every " + trigger.getRepeatInterval() / 1000 + " seconds");
    // All of the jobs have been added to the scheduler, but none of the jobs
    // will run until the scheduler has been started
    log.info("------- Starting Scheduler ----------------");
    sched.start();
    // wait 5 seconds:
    // note: nothing will run
    log.info("------- Waiting 30 seconds... --------------");
    try {
        // wait 30 seconds to show jobs
        Thread.sleep(5L * 1000L);
    // executing...
    } 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) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) AnnualCalendar(org.quartz.impl.calendar.AnnualCalendar) GregorianCalendar(java.util.GregorianCalendar) Logger(org.slf4j.Logger) AnnualCalendar(org.quartz.impl.calendar.AnnualCalendar) Date(java.util.Date) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory)

Example 55 with StdSchedulerFactory

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

the class PriorityExample method run.

public void run() throws Exception {
    Logger log = LoggerFactory.getLogger(PriorityExample.class);
    log.info("------- Initializing ----------------------");
    // First we must get a reference to a scheduler
    SchedulerFactory sf = new StdSchedulerFactory("org/quartz/examples/example14/quartz_priority.properties");
    Scheduler sched = sf.getScheduler();
    log.info("------- Initialization Complete -----------");
    log.info("------- Scheduling Jobs -------------------");
    JobDetail job = newJob(TriggerEchoJob.class).withIdentity("TriggerEchoJob").build();
    // All three triggers will fire their first time at the same time,
    // ordered by their priority, and then repeat once, firing in a
    // staggered order that therefore ignores priority.
    // 
    // We should see the following firing order:
    // 1. Priority10Trigger15SecondRepeat
    // 2. Priority5Trigger10SecondRepeat
    // 3. Priority1Trigger5SecondRepeat
    // 4. Priority1Trigger5SecondRepeat
    // 5. Priority5Trigger10SecondRepeat
    // 6. Priority10Trigger15SecondRepeat
    // Calculate the start time of all triggers as 5 seconds from now
    Date startTime = futureDate(5, IntervalUnit.SECOND);
    // First trigger has priority of 1, and will repeat after 5 seconds
    Trigger trigger1 = newTrigger().withIdentity("Priority1Trigger5SecondRepeat").startAt(startTime).withSchedule(simpleSchedule().withRepeatCount(1).withIntervalInSeconds(5)).withPriority(1).forJob(job).build();
    // Second trigger has default priority of 5 (default), and will repeat after 10 seconds
    Trigger trigger2 = newTrigger().withIdentity("Priority5Trigger10SecondRepeat").startAt(startTime).withSchedule(simpleSchedule().withRepeatCount(1).withIntervalInSeconds(10)).forJob(job).build();
    // Third trigger has priority 10, and will repeat after 15 seconds
    Trigger trigger3 = newTrigger().withIdentity("Priority10Trigger15SecondRepeat").startAt(startTime).withSchedule(simpleSchedule().withRepeatCount(1).withIntervalInSeconds(15)).withPriority(10).forJob(job).build();
    // Tell worker to schedule the job using our trigger
    sched.scheduleJob(job, trigger1);
    sched.scheduleJob(trigger2);
    sched.scheduleJob(trigger3);
    // Start up the scheduler (nothing can actually run until the
    // scheduler has been started)
    sched.start();
    log.info("------- Started Scheduler -----------------");
    // wait long enough so that the scheduler as an opportunity to
    // fire the triggers
    log.info("------- Waiting 30 seconds... -------------");
    try {
        Thread.sleep(30L * 1000L);
    // executing...
    } catch (Exception e) {
    // 
    }
    // shut down the scheduler
    log.info("------- Shutting Down ---------------------");
    sched.shutdown(true);
    log.info("------- Shutdown Complete -----------------");
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) JobDetail(org.quartz.JobDetail) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Trigger(org.quartz.Trigger) Scheduler(org.quartz.Scheduler) Logger(org.slf4j.Logger) Date(java.util.Date) DateBuilder.futureDate(org.quartz.DateBuilder.futureDate) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerFactory(org.quartz.SchedulerFactory)

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