Quartz framework (first version), first version of quartz framework

Source: Internet
Author: User

Quartz framework (first version), first version of quartz framework

Task Scheduling

In enterprise-level applications, some "scheduled tasks" are often formulated, that is, at a certain point in time, the core of a task is to focus on time, that is, at a specific point in time, the Quartz framework is an open-source enterprise-level Task Scheduling Service, which involves multi-thread concurrency, thread pool maintenance, runtime rule parsing, and on-site protection for recovery, it has been used as a good solution for task scheduling.

Core of Quartz framework

Quartz highly abstracts task scheduling, puts forward three core concepts, and describes the task in the org. quartz package through interfaces and classes: the task is the execution content. Quartz provides the Job interface to support task definition triggers: defines the time trigger rule for triggering Job execution. Quartz provides the Trigger class and its sub-classes to support the Trigger function. The Scheduler: Quartz provides the Scheduler interface to bind the job and Trigger to ensure that the job can be executed at the right time.

 

Running Environment

Task Scheduling

Task execution

Task persistence

Transactions

Cluster

Listener and insert

Let's take a look at the example below.

Create a Plan

Start creating entity layer definition task Template

Public class Plan {private String date; // time private String task; // task/* (non-Javadoc) * @ see java. lang. object # toString () */@ Override public String toString () {return "Plan [date =" + date + ", task =" + task + "]";} public Plan () {super (); // TODO Auto-generated constructor stub} public Plan (String date, String task) {super (); this. date = date; this. task = task ;}

We create a reminder service RemindService

Customizes a generic set. Users can store multiple Plan objects and provide a method to read data from the generic set.

*/Public class RemindService {public void printPlan (String userName) {List <Plan> plansFormToday = getPlansFormToday (userName); System. out. println (userName + "//"); for (Plan plan: plansFormToday) {System. out. println (plan + "\\\\") ;}} private List <Plan> getPlansFormToday (String userName) {// simulate database query, only List <Plan> list = new ArrayList <Plan> (); list. add (new Plan ("14:31:11", "| location: Conference Room C01"); list. add (new Plan ("14:31:20", "President's Office | location: President's Office"); return list ;}

We create a job scheduling class RemindJob

/*** Alert service class ** @ author Admin **/public class RemindJob implements Job {private RemindService service = new RemindService (); @ Override public void execute (JobExecutionContext context) throws JobExecutionException {service. printPlan ("Hello! ");}/*** @ Return the service */public RemindService getService () {return service ;} /*** @ param service the service to set */public void setService (RemindService service) {this. service = service ;}

Core code analysis:

 

The Job interface has only one execute () method, which is implemented in the implementation class to execute a specific task.

 

 

The JobExecutionContext parameter can be used to obtain information about the scheduling context, such as the Task Name.

Add the following code to the execute () method of the original RemindJob to obtain the name of the task, the name of the bound trigger, and the trigger time of each task.

// Obtain the user information JobDataMap dataMap = context. getJobDetail (). getJobDataMap (); String value = (String) dataMap. get ("happykey"); System. out. println (value); System. out. println ("TriggerName \ t" + context. getTrigger (). getKey (). getName (); System. out. println ("JobName \ t" + context. getJobDetail (). getKey (). getName ());

Real task object and trigger object Test

Public class Test {public static void main (String [] args) throws SchedulerException {doRemind ();} public static void doRemind () throws SchedulerException {JobDetail job = JobBuilder. newJob (RemindJob. class ). withIdentity ("job1", "group1 "). build (); // create a task/*** create trigger ** the first method is not good * // * SimpleTrigger trigger = TriggerBuilder. newTrigger (). withIdentity ("myTrigger", "myTriggerGroup "). withSchedule (Simpl EScheduleBuilder. simpleSchedule (). withIntervalInSeconds (3 ). repeatForever ()). startAt (new Date (System. currentTimeMillis () + 1000 )). build (); * // ** create a trigger * The second method is very good * Can I use the third Friday of every month on January 1, 2013 at AM to trigger 0 30 10? * 6 #3 2013 * trigger 0 17 16 on the first Thursday of every month at pm? * 5 #1 2016 * run every 5 minutes from to every day. In addition, run every 5 minutes from to */CronTrigger trigger = TriggerBuilder. newTrigger (). withIdentity ("myTrigger", "myTriggerGroup "). withSchedule (CronScheduleBuilder. cronSchedule ("0 18 16? * 5 #1 2016 ")). build (); SchedulerFactory sf = new StdSchedulerFactory (); // create a Scheduler factory scheduler Scheduler = sf. getsched (); // create a scheduler. scheduleJob (job, trigger); // register and schedule schedgger. start (); // start scheduling // Thread. sleep (millis) // scheduler. shutdown (); // close scheduling}

Core code analysis:

This is to create a Task instance through JobDetail.

 

You can also use the JobDataMap object of JobDetail to set the initial data, such as the user name to be reminded.

Public class TestJob {public void doRemind () throws SchedulerException, InterruptedException {JobDetail job = new JobDetail ("remindJob", "group1", RemindJob. class); // obtain the JobDataMap object associated with the task. JobDataMap dataMap = job. getJobDataMap (); dataMap. put ("user", "Zhang San"); // set user data in the JobDataMap object // omit the original program code }}

 

JobBuilder class

 

Use SimpleTrigger to create a trigger

    SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "myTriggerGroup").    withSchedule(SimpleScheduleBuilder.simpleSchedule().            withIntervalInSeconds(3).            repeatForever()).            startAt(new Date(System.currentTimeMillis()+1000)).build();

Create a scheduler

SchedulerFactory sf = new StdSchedulerFactory (); // create a Scheduler factory scheduler Scheduler = sf. getScheduler (); // create a scheduler

Register tasks and triggers

Scheduler. scheduleJob (job, trigger); // register and schedule scheduler. start (); // start scheduling

Start and close Scheduling

sched.start(); sched.shutdown(); 

 

In the above example, the task scheduling is basically completed.

Expected Version 2

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.