Use quartz (1), use quartz (

Source: Internet
Author: User

Use quartz (1), use quartz (

Scheduled tasks are often encountered in projects. quartz is an excellent open-source framework that supports scheduled tasks, task persistence, and database support. The following is a brief introduction to quartz and a small example is provided.

1. The following is the official website of quartz:

Bytes. Quartz has made great changes in analyticdb 2.0. For specific changes, refer to relevant materials.

2. Here I download quartz-2.2.3-distribution.tar.gz. decompress the tool to obtain the file directory structure, as shown in,

Docs: quartz document information, including database-related table creation SQL;

Examples: example;

Javadoc: using java doc, you can use jd2chm tool to convert documents to chm format;

Lib: quartz jar package, which contains all the packages using quartz (dependency package and quartz jar package );

Licenses: license file;

Src: source code of quartz;

3. We can see from the lib package that the quartz log system uses slf4j, but its real log system is log4j. For details about the log framework, see my other articles.

4. quartz can be used in java projects or java Web projects. Here we use the javaweb project as an example.

5. Place all jar packages in the lib folder under the quartz folder to the lib directory of the project.

6. After the above steps, quartz will be introduced into the project.

The following is an example and code:

I. First, to create a task, quartz requires that the task must be org. quarta. job interface, this interface is a method of execute, this method is allowed when the task is executed, the specific business implementation in this method, I only printed here,

 1 package com.cn.myScheduler; 2  3 import org.quartz.Job; 4 import org.quartz.JobExecutionContext; 5 import org.quartz.JobExecutionException; 6 public class MyJob implements Job { 7     @Override 8     public void execute(JobExecutionContext arg0) throws JobExecutionException { 9         // TODO Auto-generated method stub10         System.out.println("this is my first scheduler!");11     }12 13 }

2. After creating a task, we also need to create a jobDetail containing the task information, a trigger for managing the task execution, and finally a scheduling task,

1 package com.cn. myScheduler; 2 3 import org. quartz. jobBuilder; 4 import org. quartz. jobDetail; 5 import org. quartz. schedorg; 6 import org. quartz. schedulerException; 7 import org. quartz. schedulerFactory; 8 import org. quartz. simpleScheduleBuilder; 9 import org. quartz. trigger; 10 import org. quartz. triggerBuilder; 11 import org. quartz. impl. stdSchedulerFactory; 12 public class TestMyJob {13 public static void main (String [] args) {14 // TODO Auto-generated method stub15 try {16 // 1. Obtain a scheduler17 SchedulerFactory sf = new StdSchedulerFactory (); 18 Scheduler schedfactory = sf. getScheduler (); 19 // 2. Obtain a jobDetail20 JobDetail job = JobBuilder. newJob (MyJob. class) 21. withIdentity ("myJob") 22. build (); 23 24 // 3. Get a trigger25 Trigger trigger = TriggerBuilder. newTrigger () 26. withIdentity ("trigger1", "group1") 27. startNow () 28. withSchedule (SimpleScheduleBuilder. simpleSchedule () 29. withIntervalInSeconds (40) 30. withRepeatCount (10) 31. build (); 32 // 4. Place the task and trigger in scheduler 33 scheduler. scheduleJob (job, trigger); 34 // 5. Start task scheduling 35 schedgger. start (); 36} 37 catch (SchedulerException e) {38 e. printStackTrace (); 39} 40} 41 42}

Note 1: Create a scheduler and scheduler is responsible for scheduling all tasks.

NOTE 2: Create a jobDetail that contains the task information, specify the implementation class of the task as MyJob. class, use the withIdentity method to specify the task name, and use the second parameter to specify the group name of the task.

NOTE 3: create a task producer and specify the producer name "trigger1" and the group name "group1 "; set the pushing time to the current pushing time, and execute once every 40 s, a total of 10 times

Note 4: Put jobDetail and trigger in scheduler

Note 5: Start scheduler

Run the program to view the program output.

The above is a simple quartz scheduled task.

The quartz content is continuously updated!

Thank you!

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.