Basic use of Quartz, use of Quartz

Source: Internet
Author: User

Basic use of Quartz, use of Quartz

1. Quartz Overview: in simple terms, it is a java timing task framework. It is not so complicated to explain, just like the timer at the front end. Before learning about it, familiarize yourself with several concepts.

2. Basic Concepts

2.1 Job: indicates the specific content of a Job to be executed. This interface only has one method void execute (JobExecutionContext context)

2.2 JobDetail: JobDetail indicates a specific executable scheduling program. A Job is the content to be executed by this executable program. In addition, JobDetail also includes the Job Scheduling Scheme and strategy.

2.3 Trigger indicates the configuration of a scheduling parameter.

2.4 schedgger represents a scheduling container. A scheduling container can register multiple JobDetail and Trigger. When the combination of Trigger and JobDetail can be scheduled by the Scheduler container.

Explanation:
L compile a job to implement the business and ask specific things to do.
L use JobDetail to wrap a job, which is a task object and can be scheduled
L use Trigger (Trigger object) to customize when to call a task object.
L use Scheduler to combine task objects and trigger objects, much like a big manager.

3. Operating Principle

1. sched is a scheduler container (Headquarters), which can contain many JobDetail and trigger,
After the container is started, every JobDetail in the container is automatically executed according to the trigger step by step.
2. JobDetail is an executable job, which may be stateful.
3. Trigger indicates the configuration of a scheduling parameter.
4. When JobDetail and Trigger are registered on the scheduler container, the assembled jobs (JobDetail and
A pair of triggers.
5. scheduler is a container with a thread pool used to schedule and execute each job in parallel.
High container Efficiency

3. case:
3.1 simple task

3.1.1: Introduce relevant jar packages

3.1.2: compile related tasks

The Code is as follows:

1 package cn. itcast. test; 2 3 import org. junit. test; 4 import org. quartz. jobBuilder; 5 import org. quartz. jobDetail; 6 import org. quartz. schedorg; 7 import org. quartz. schedulerException; 8 import org. quartz. simpleScheduleBuilder; 9 import org. quartz. trigger; 10 import org. quartz. triggerBuilder; 11 import org. quartz. impl. stdSchedulerFactory; 12 13 import cn. itcast. service. helloJob; 14 15 public class Testquartz {16 17 // simple task test 18 @ Test19 public void testSimpleTask () {20 21 try {22 23 Scheduler schedtask = StdSchedulerFactory. getdefaschscheduler (); 24 scheduler. start (); 25 26 // define the job and tie it to our HelloJob class27 JobDetail job = JobBuilder. newJob (HelloJob. class) 28. withIdentity ("job1", "group1") 29. build (); 30 31 // Trigger the job to run now, and then repeat every 40 seconds32 Trigger trigger = TriggerBuilder. newTrigger () 33. withIdentity ("trigger1", "group1") 34. startNow () 35. withSchedule (SimpleScheduleBuilder. simpleSchedule () 36. withIntervalInSeconds (3) 37. repeatForever () 38. build (); 39 40 // Tell quartz to schedule the job using our trigger41 scheduler. scheduleJob (job, trigger); 42 43 // prevent junit from terminating jvm44 while (true) {45 46} 47 48} catch (SchedulerException se) {49 se. printStackTrace (); 50} 51} 52 53}
View Code

3.1.3: scheduled tasks

Compared with simple tasks, the main difference is that the policies are different.

1 public class QuartzTest {2 3 // complex task 4 @ Test 5 public void testCronTrigger () throws SchedulerException {6 // get Scheduler 7 scheduler Scheduler = StdSchedulerFactory. getdefaschscheduler (); 8 9 // enable maxcompute 10 sched. start (); 11 12 // define the job and tie it to our HelloJob class13 JobDetail job = JobBuilder. newJob (HelloQuartz. class) 14. withIdentity ("job1", "group1") 15. build (); 16 17 // Trigger the job t O run now, and then repeat every 40 seconds18 Trigger trigger = TriggerBuilder. newTrigger () 19. withIdentity ("trigger1", "group1") 20. startNow () 21 // execute 22 every 5 seconds. withSchedule (CronScheduleBuilder. cronSchedule ("0/5 **? ** ") 23. build (); 24 25 // Tell quartz to schedule the job using our trigger26 scheduler. scheduleJob (job, trigger); 27 28 // terminate JVM execution 29 while (true) {30 31} 32 33}
View Code

Jar package used in this test:

 1     <dependencies> 2         <dependency> 3             <groupId>org.quartz-scheduler</groupId> 4             <artifactId>quartz</artifactId> 5             <version>2.2.3</version> 6         </dependency> 7         <dependency> 8             <groupId>org.quartz-scheduler</groupId> 9             <artifactId>quartz-jobs</artifactId>10             <version>2.2.3</version>11         </dependency>12         <!-- slf4j log4j -->13         <dependency>14             <groupId>org.slf4j</groupId>15             <artifactId>slf4j-log4j12</artifactId>16             <version>1.7.7</version>17         </dependency>18     <dependency>19             <groupId>junit</groupId>20             <artifactId>junit</artifactId>21             <version>4.11</version>22             <scope>test</scope>23         </dependency>24     </dependencies>
View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.