Shows a dozen different ways of using Simple Triggers to schedule your jobs
展示使用 Simple Triggers来安排工作的十种不同方法
--------------------------------------------------------------------------------------------------------------
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package org.quartz.examples.example2;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
/**
* <p>
* This is just a simple job that gets fired off many times by example 1
* </p>
*
* @author Bill Kratzer
*/
public class SimpleJob implements Job {
private static Logger _log = LoggerFactory.getLogger(SimpleJob.class);
/**
* Empty constructor for job initialization
*/
public SimpleJob() {
}
/**
* <p>
* Called by the <code>{@link org.quartz.Scheduler}</code> when a
* <code>{@link org.quartz.Trigger}</code> fires that is associated with
* the <code>Job</code>.
* </p>
*
* @throws JobExecutionException
* if there is an exception while executing the job.
*/
public void execute(JobExecutionContext context)
throws JobExecutionException {
// This job simply prints out its job name and the
// date and time that it is running
JobKey jobKey = context.getJobDetail().getKey();
_log.info("SimpleJob says: " + jobKey + " executing at " + new Date());
}
}
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
/*
* All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
*/
package org.quartz.examples.example2;
import static org.quartz.DateBuilder.futureDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.JobKey.jobKey;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;
import org.quartz.DateBuilder;
import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
/**
* This Example will demonstrate all of the basics of scheduling capabilities of Quartz using Simple Triggers.
*
* @author Bill Kratzer
*/
public class SimpleTriggerExample {
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 static void main(String[] args) throws Exception {
SimpleTriggerExample example = new SimpleTriggerExample();
example.run();
}
}
--------------------------------------------------------------------------------------------------------------
Executing result:
16:53:32.635 INFO org.quartz.examples.example2.SimpleTriggerExample 49 run - ------- Initializing -------------------
16:53:32.682 INFO org.quartz.impl.StdSchedulerFactory 1172 instantiate - Using default implementation for ThreadExecutor
16:53:32.686 INFO org.quartz.simpl.SimpleThreadPool 268 initialize - Job execution threads will use class loader of thread: main
16:53:32.703 INFO org.quartz.core.SchedulerSignalerImpl 61 <init> - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
16:53:32.704 INFO org.quartz.core.QuartzScheduler 240 <init> - Quartz Scheduler v.2.2.3 created.
16:53:32.705 INFO org.quartz.simpl.RAMJobStore 155 initialize - RAMJobStore initialized.
16:53:32.706 INFO org.quartz.core.QuartzScheduler 305 initialize - Scheduler meta-data: Quartz Scheduler (v2.2.3) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
NOT STARTED.
Currently in standby mode.
Number of jobs executed: 0
Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.
16:53:32.707 INFO org.quartz.impl.StdSchedulerFactory 1327 instantiate - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
16:53:32.708 INFO org.quartz.impl.StdSchedulerFactory 1331 instantiate - Quartz scheduler version: 2.2.3
16:53:32.709 INFO org.quartz.examples.example2.SimpleTriggerExample 55 run - ------- Initialization Complete --------
16:53:32.709 INFO org.quartz.examples.example2.SimpleTriggerExample 57 run - ------- Scheduling Jobs ----------------
16:53:32.716 INFO org.quartz.examples.example2.SimpleTriggerExample 72 run - group1.job1 will run at: Sun Aug 12 16:53:45 CST 2018 and repeat: 0 times, every 0 seconds
16:53:32.716 INFO org.quartz.examples.example2.SimpleTriggerExample 81 run - group1.job2 will run at: Sun Aug 12 16:53:45 CST 2018 and repeat: 0 times, every 0 seconds
16:53:32.716 INFO org.quartz.examples.example2.SimpleTriggerExample 96 run - group1.job3 will run at: Sun Aug 12 16:53:45 CST 2018 and repeat: 10 times, every 10 seconds
16:53:32.717 INFO org.quartz.examples.example2.SimpleTriggerExample 111 run - group1.job3 will [also] run at: Sun Aug 12 16:53:45 CST 2018 and repeat: 2 times, every 10 seconds
16:53:32.717 INFO org.quartz.examples.example2.SimpleTriggerExample 126 run - group1.job4 will run at: Sun Aug 12 16:53:45 CST 2018 and repeat: 5 times, every 10 seconds
16:53:32.718 INFO org.quartz.examples.example2.SimpleTriggerExample 136 run - group1.job5 will run at: Sun Aug 12 16:58:32 CST 2018 and repeat: 0 times, every 0 seconds
16:53:32.719 INFO org.quartz.examples.example2.SimpleTriggerExample 146 run - group1.job6 will run at: Sun Aug 12 16:53:45 CST 2018 and repeat: -1 times, every 40 seconds
16:53:32.720 INFO org.quartz.examples.example2.SimpleTriggerExample 149 run - ------- Starting Scheduler ----------------
16:53:32.721 INFO org.quartz.core.QuartzScheduler 575 start - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
16:53:32.721 INFO org.quartz.examples.example2.SimpleTriggerExample 155 run - ------- Started Scheduler -----------------
16:53:32.723 INFO org.quartz.examples.example2.SimpleTriggerExample 169 run - group1.job7 will run at: Sun Aug 12 16:53:45 CST 2018 and repeat: 20 times, every 300 seconds
16:53:32.723 INFO org.quartz.examples.example2.SimpleTriggerExample 177 run - 'Manually' triggering job8...
16:53:32.723 INFO org.quartz.examples.example2.SimpleTriggerExample 180 run - ------- Waiting 30 seconds... --------------
16:53:32.727 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job8 executing at Sun Aug 12 16:53:32 CST 2018
16:53:45.001 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job1 executing at Sun Aug 12 16:53:45 CST 2018
16:53:45.002 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job2 executing at Sun Aug 12 16:53:45 CST 2018
16:53:45.019 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:53:45 CST 2018
16:53:45.019 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job4 executing at Sun Aug 12 16:53:45 CST 2018
16:53:45.023 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job6 executing at Sun Aug 12 16:53:45 CST 2018
16:53:45.161 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job7 executing at Sun Aug 12 16:53:45 CST 2018
16:53:45.166 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:53:45 CST 2018
16:53:55.002 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:53:55 CST 2018
16:53:55.003 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job4 executing at Sun Aug 12 16:53:55 CST 2018
16:53:55.012 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:53:55 CST 2018
16:54:02.724 INFO org.quartz.examples.example2.SimpleTriggerExample 192 run - ------- Rescheduling... --------------------
16:54:02.728 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job7 executing at Sun Aug 12 16:54:02 CST 2018
16:54:02.733 INFO org.quartz.examples.example2.SimpleTriggerExample 201 run - job7 rescheduled to run at: Sun Aug 12 16:53:45 CST 2018
16:54:02.735 INFO org.quartz.examples.example2.SimpleTriggerExample 203 run - ------- Waiting five minutes... ------------
16:54:05.003 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:54:05 CST 2018
16:54:05.005 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job4 executing at Sun Aug 12 16:54:05 CST 2018
16:54:05.007 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:54:05 CST 2018
16:54:15.003 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:54:15 CST 2018
16:54:15.003 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job4 executing at Sun Aug 12 16:54:15 CST 2018
16:54:25.002 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:54:25 CST 2018
16:54:25.005 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job4 executing at Sun Aug 12 16:54:25 CST 2018
16:54:25.011 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job6 executing at Sun Aug 12 16:54:25 CST 2018
16:54:35.001 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:54:35 CST 2018
16:54:35.009 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job4 executing at Sun Aug 12 16:54:35 CST 2018
16:54:45.003 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:54:45 CST 2018
16:54:55.002 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:54:55 CST 2018
16:55:05.002 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:55:05 CST 2018
16:55:05.007 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job6 executing at Sun Aug 12 16:55:05 CST 2018
16:55:15.003 INFO org.quartz.examples.example2.SimpleJob 62 execute - SimpleJob says: group1.job3 executing at Sun Aug 12 16:55:15 CST 201