Search in sources :

Example 56 with StdSchedulerFactory

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

the class JobStateExample method run.

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.");
}
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 57 with StdSchedulerFactory

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

the class RemoteClientExample method run.

public void run() throws Exception {
    Logger log = LoggerFactory.getLogger(RemoteClientExample.class);
    // First we must get a reference to a scheduler
    SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler sched = sf.getScheduler();
    // define the job and ask it to run
    JobDetail job = newJob(SimpleJob.class).withIdentity("remotelyAddedJob", "default").build();
    JobDataMap map = job.getJobDataMap();
    map.put("msg", "Your remotely added job has executed!");
    Trigger trigger = newTrigger().withIdentity("remotelyAddedTrigger", "default").forJob(job.getKey()).withSchedule(cronSchedule("/5 * * ? * *")).build();
    // schedule the job
    sched.scheduleJob(job, trigger);
    log.info("Remote job scheduled.");
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) TriggerBuilder.newTrigger(org.quartz.TriggerBuilder.newTrigger) Logger(org.slf4j.Logger) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory)

Example 58 with StdSchedulerFactory

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

the class MyCronTrigger method main.

public static void main(String[] args) throws SchedulerException {
    SchedulerFactory schedulerFactory = new StdSchedulerFactory();
    Scheduler scheduler = schedulerFactory.getScheduler();
    AnnualCalendar holidays = new AnnualCalendar();
    Calendar calendar = new GregorianCalendar(2018, 5, 13);
    holidays.setDayExcluded(calendar, true);
    scheduler.addCalendar("holidays", holidays, false, false);
    JobDetail jobDetail = JobBuilder.newJob(MyCronJob.class).withIdentity("cron", "crongroup").build();
    Trigger trigger = TriggerBuilder.newTrigger().withIdentity("cronTrigger", "cronTriggerGroup").withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ? ")).modifiedByCalendar("holidays").build();
    scheduler.scheduleJob(jobDetail, trigger);
    scheduler.start();
}
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) AnnualCalendar(org.quartz.impl.calendar.AnnualCalendar) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory)

Example 59 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory in project pancm_project by xuwujing.

the class RAMQuartz method main.

/**
 * The entry point of application.
 *
 * @param args the input arguments
 * @throws SchedulerException the scheduler exception
 */
public static void main(String[] args) throws SchedulerException {
    // 1.创建Scheduler的工厂
    SchedulerFactory sf = new StdSchedulerFactory();
    // 2.从工厂中获取调度器实例
    Scheduler scheduler = sf.getScheduler();
    // 3.创建JobDetail
    JobDetail jb = JobBuilder.newJob(RAMJob.class).withDescription(// job的描述
    "this is a ram job").withIdentity("ramJob", // job 的name和group
    "ramGroup").build();
    // 任务运行的时间,SimpleSchedle类型触发器有效
    // 3秒后启动任务
    long time = System.currentTimeMillis() + 3 * 1000L;
    Date statTime = new Date(time);
    // 4.创建Trigger
    // 使用SimpleScheduleBuilder或者CronScheduleBuilder
    Trigger t = TriggerBuilder.newTrigger().withDescription("").withIdentity("ramTrigger", "ramTriggerGroup").startAt(// 默认当前时间启动
    statTime).withSchedule(// 两秒执行一次
    CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build();
    // 5.注册任务和定时器
    scheduler.scheduleJob(jb, t);
    // 6.启动 调度器
    scheduler.start();
    LOG.info("启动时间 : " + new Date());
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) Scheduler(org.quartz.Scheduler) Date(java.util.Date) SchedulerFactory(org.quartz.SchedulerFactory) StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory)

Example 60 with StdSchedulerFactory

use of org.quartz.impl.StdSchedulerFactory in project ovirt-engine by oVirt.

the class DBSchedulerUtilQuartzImpl method setup.

public void setup(Properties props) {
    try {
        SchedulerFactory sf = new StdSchedulerFactory(props);
        sched = sf.getScheduler();
        if (sched != null) {
            sched.start();
            sched.getListenerManager().addJobListener(new FixedDelayJobListener(this), jobGroupEquals(Scheduler.DEFAULT_GROUP));
        } else {
            log.error("there is a problem with the underlying Scheduler: null returned");
        }
    } catch (SchedulerException se) {
        log.error("there is a problem with the underlying Scheduler: {}", se.getMessage());
        log.debug("Exception", se);
    }
}
Also used : StdSchedulerFactory(org.quartz.impl.StdSchedulerFactory) SchedulerException(org.quartz.SchedulerException) 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