Quartz use timed tasks in the Java WEB application

Source: Internet
Author: User
Tags numeric value java web
Quartz the ability to send messages on a regular basis, which is attached to Web applications, where the application starts when the Web application is started. At first it was decided to use Java.util.Timer and java.util.TimerTask, but the study found that the Java timer was less functional and that the scope of its threads was not constrained by Web applications. It was interesting to discover later that Quartz was an open source scheduling framework.

First we want to get the latest release of Quartz. The latest version is currently 1.6. We can get its full download package from the following address, which is full of soups, not only for our Quartz.jar, but also for multiple routines and detailed documentation, from APIs to configuration file XSD. Interested friends can also be found in the SRC directory of the project source a look at exactly.

With less nonsense, let's take a look at how this stuff is used in the Java WEB application.

The first thing we have to propose is the three core concepts of quartz: scheduler, triggers, and jobs. Let's see how they work.

A Job Commander--Dispatcher

1. Scheduler interface

The interface may be the most top-level thing in the entire quartz, and it has all the triggers and jobs to coordinate the work. Each scheduler has Jobdetail and trigger registrations, and a scheduler can register multiple jobdetail and multiple trigger, both jobdetail and trigger can be passed through the group Name is distinguished from their own name to keep these jobdetail and trigger instances from conflicting within the same scheduler. Therefore, the group name of the Jobdetail in each scheduler is unique and its name is unique (as if it were a Jobdetail ID). The same is true of trigger.

The scheduler instance is generated by schedulerfactory, and once the scheduler instance is generated, we can find the instance by building its factory to get its associated properties. The following code shows us how to find schedulerfactory from a servlet and get the corresponding scheduler instance, through which we can get the Testmode property in the current job to determine whether the job is in test mode.

Java code    /Find from the current servlet context stdschedulerfactory             & nbsp;           servletcontext ctx= Request.getsession () Getservletcontext ();                          StdSchedulerFactory factory =  ( stdschedulerfactory)  ctx.getattribute ("Org.quartz.impl.StdSchedulerFactory.KEY");                           scheduler sch = null;                          try {                             // Get Scheduler                               sch = factory.getscheduler ("Schedulername");                              //through the Scheduler instance to obtain jobdetail, attention to understand jobdetailname and groupname usage                               Jobdetail jd=sch.getjobdetail ("Jobdetailname",  "groupname");                               map jobmap1=jd.getjobdatamap ();                              istest=jobmap1.get (" Testmode ") +" ";                           } catch  (exception se)  {                              // Read testmode                  from the configuration file if the current job is not present             readxml ("Job.xml"). Get ("Job.testmode") ;                          }    
Finds Stdschedulerfactory ServletContext ctx=request.getsession () from the current servlet context   
  
            . Getservletcontext ();   
  
            Stdschedulerfactory factory = (stdschedulerfactory) ctx.getattribute ("Org.quartz.impl.StdSchedulerFactory.KEY");   
  
            Scheduler sch = null;   
  
            try {   
  
                //Get scheduler   
  
                sch = Factory.getscheduler ("Schedulername");   
  
                Get Jobdetail through the scheduler instance, and take note of the usage of jobdetailname and groupname   
  
                jobdetail jd=sch.getjobdetail ("Jobdetailname", " GroupName ");   
  
                Map Jobmap1=jd.getjobdatamap ();   
  
                Istest=jobmap1.get ("Testmode") + "";   
  
            } catch (Exception se) {    
  
                //If the current job is not present, read Testmode   
  
                ReadXML ("Job.xml") from the configuration file. Get ("Job.testmode");   
  
              


After the scheduler instance is generated, it is in the "stand-by" mode and needs to invoke its Start method to put it into operation.
Java code     public class sendmailshedule{                  //set standard schedulerfactory                 static SchedulerFactory schedFact = new  Org.quartz.impl.StdSchedulerFactory ();                  static Scheduler sched;                  public static void run () throws exception{                      //Generation Scheduler Instance                             Sched = schedfact.getscheduler ();                        //Create a Jobdetail instance, the corresponding job implementation class is sendmailjob                           jobdetail jobdetail  = new jobdetail ("Myjob", Sched. Default_group,sendmailjob.class);                      //set Crontrigger, use cron expression to set trigger time                       CronTrigger trigger = new  Crontrigger ("Mytrigger", "Test", "0 0 8 1 * ?");                       Sched.schedulejob (Jobdetail, trigger);                       sched.start ();                 }                 public  Static void  stop () throws exception{                     sched.shutdown ();                 }            }     

public class sendmailshedule{   
  
    //Set standard schedulerfactory   
  
    static schedulerfactory schedfact = new Org.quartz.impl.StdSchedulerFactory ();   
  
    Static Scheduler sched;   
  
    public static void Run () throws exception{   
  
        //Generate Scheduler instance   
  
             Sched = Schedfact.getscheduler ();    
  
        Create a Jobdetail instance, the corresponding job implementation class is Sendmailjob   
  
             jobdetail jobdetail = new Jobdetail ("Myjob", Sched. Default_group,sendmailjob.class);   
  
        Set Crontrigger, use cron expression to set trigger time   
  
        Crontrigger trigger = new Crontrigger ("Mytrigger", "Test", "0 0 8 1 *");   
  
        Sched.schedulejob (Jobdetail, trigger);    
  
        Sched.start ();   
  
    }   
  
    public static void  Stop () throws exception{   
  
        Sched.shutdown ();   
  
    }   
  
  



In addition, we can track the working status of jobs and triggers through listeners.


Two Jobs and their associated

1. Job

A job is actually an interface in which any job can be written as a class that implements the interface, and implements the Execute () method, which completes the specific job task.

2. Jobdetail

Jobdetail can specify the details of our job, such as the dynamic loading of an instance of a job through the reflection mechanism, specifying the job group name and specific job name for a job within a single dispatcher, and specifying a specific trigger.

A job instance can correspond to multiple triggers (that is, the school can put one eye exercise at 10 a day, 3:30 P.M.), but a trigger can only correspond to one job instance (it is not possible for the school to play both eye exercises and radio broadcast recordings at 10 o'clock).

3. Jobdatamap

This is a data structure to provide data support to the job, and it is very convenient to use the same method as Java.util.Map. When a job is assigned to the scheduler, the Jobdatamap instance is generated.

The job has a statefuljob sub-interface that represents a stateful task, which is a label interface with no methods, and its purpose is to let quartz know the type of task in order to adopt different execution scenarios. Stateless tasks have their own jobdatamap copies at execution time, and changes to Jobdatamap do not affect the next execution. While stateful task sharing shares the same Jobdatamap instance, the changes that are made to jobdatamap each time the task executes are saved, and subsequent execution can see the change, which affects subsequent execution after each execution of the task.

For this reason, stateless jobs can be executed concurrently, and stateful statefuljob cannot be executed concurrently, which means that if the previous statefuljob has not been completed, the next task will block the wait until the previous task has finished executing. Stateful tasks have more factors to consider than stateless tasks, and programs tend to have a higher degree of complexity, so you should try to use stateless jobs unless necessary.

If Quartz uses database persistence task scheduling information, stateless Jobdatamap will only be maintained once while scheduler registers the task, and the jobdatamap of the stateful task will be saved each time the task is executed.

Jobdatamap instances can also be associated with a trigger. In this case, for different triggers for the same job, we can add different data to the Jobdatamap so that the job can provide more flexible data support at different times (the first edition of the school morning Exercise and the second edition in the afternoon).

Whether stateful or stateless, changes made to trigger's jobdatamap during the execution of a task do not persist, nor do they affect the next execution.

Three Trigger

Trigger is an abstract class that has three subclasses: Simpletrigger,crontrigger and Nthincludeddaytrigger. The first two are more commonly used.

1. Simpletrigger: This is a very simple class, we can define the trigger time of the job, and optionally set the repeat interval and the number of repetitions.

2. Crontrigger: This trigger is powerful and flexible, but you need to know about cron expressions. If you're a UNIX system enthusiast, you probably already have this knowledge, but if you don't know the cron expression, look at the following cron details:



A cron expression consists of 6 or 7 time fields separated by spaces, as shown in table 1:

Table 1 Cron Expression Time field

Position

Time Domain

Allowed values

Special characters that are allowed

1

Seconds

0-59

, - * /

2

Minutes

0-59

, - * /

3

Hours

0-23

, - * /

4

Date

1-31

, - * ? /L W C

5

Month

1-12

, - * /

6

Week

1-7

, - * ? /L C #

7

Years (optional)

Null value 1970-2099

, - * /



The time field of a cron expression allows you to set a numeric value, and you can use special characters to provide lists, ranges, wildcard characters, and so on, as follows:

Asterisk (*): Used in all fields to represent every moment of the corresponding time field, for example, * "Per minute" in the minute field;

Question mark (?): The character is used only in the date and week fields, which is usually specified as "meaningless value", equivalent to a bit character;

Minus sign (-): expresses a range, such as using "10-12" in the hour field, representing 10 to 12 points, i.e. 10,11,12;

Comma (,): Expresses a list value, such as "Mon,wed,fri" in the Week field, for Monday, Wednesday, and Friday;

Slash (/): X/y expresses a sequence of equal steps, X is the starting value, and Y is the increment step value. If you use 0/15 in the minute field, it is 0,15,30 and 45 seconds, and 5/15 represents 5,20,35,50 in the minute field, you can also use */y, which is equivalent to 0/y;

L: This character is used only in the date and week fields, meaning "last", but it means different in two fields. L In the Date field, represents the last day of the month, such as number 31st in January, not leap year February 28th; if L is used in the week, it means Saturday, equivalent to 7. However, if L appears in the Week field and has a value x in front of it, it means "last x days of the Month", for example, 6L represents the last Friday of the month;



W: This character can only appear in the Date field and is a cosmetic of the leading date that represents the most recent weekday from that date. For example, 15W represents the most recent working day from 15th, if the month 15th is Saturday, match 14th Friday; if 15th is Sunday, match 16th Monday; if 15th is Tuesday, the result is 15th Tuesday. But it must be noted that the associated matching date is not able to span the month, as you specify 1W, if number 1th is Saturday, the result matches the number 3rd Monday, not last month. The W string can only specify a single date and cannot specify a date range;



LW combination: In the Date field can be combined to use LW, which means the last working day of the month;

Pound sign (#): This character can only be used in the week field, indicating a weekday of the month. If 6#3 said that the third Friday of the month (6 for Friday, #3表示当前的第三个), and 4#5 that the month of the fifth Wednesday, assuming that the month does not have the fifth Wednesday, ignoring the trigger;

C: This character is used only in the date and week fields and represents the meaning of calendar. It means the date that the plan is associated with, and if the date is not associated, it is equivalent to all the dates in the calendar. For example 5C in the Date field corresponds to the first day of the calendar after 5th. 1C is equivalent to the first day after Sunday in the Week field. Cron expressions are not sensitive to the case of special characters, nor are they sensitive to the abbreviated English case of the representative week. Table 2 shows some examples of the complete cron representation:

Table 2 Example of a cron representation

Representation of

Description

"0 0 12 * *?"

Run at 12 every day.

"0 15 10?" * *"

Run 10:15 every day

"0 15 10 * *?"

Run 10:15 every day

"0 15 10 * *? *"

Run 10:15 every day

"0 15 10 * *? 2008 "

Running 10:15 every day in 2008.

"0 * 14 * *?"

Run every minute between 14 o'clock and 15 every day, starting at 14:00 and ending at 14:59.

"0 0/5 14 * *?"

Run every 5 minutes from 14 o'clock to 15 every day, starting at 14:00 and ending at 14:55.

"0 0/5 14,18 * *?"

Run every 5 minutes from 14 o'clock to 15 every day, and also every 5 minutes every 18 o'clock to 19 o ' clock.

"0 0-5 14 * *?"

Every day 14:00 to 14:05, run every minute.

"0 10,44 14?" 3 WED "

March every Wednesday of 14:10 minutes to 14:44, run every minute.

"0 15 10?" * Mon-fri "

Run for 10:15 minutes per Monday, two, three, four, five.

"0 15 10 15 *?"

Every month 15th 10:15 minutes run.

"0 L *?"

Run 10:15 on the last day of each month.

"0 15 10?" * 6L "

Run at the last Friday 10:15 per month.

"0 15 10?" * 6L 2007-2009 "

Run in the last Friday of the 2007,2008,2009 year of 10:15 minutes per month.

"0 15 10?" * 6#3 "

10:15 minutes per month for the third Friday run.



OK, so much, finally let's see how to use Quartz in Web applications




Because the configuration of scheduler is quite personalized, in Web applications, we can configure Quartzservlet with a quartz.properties file. But let's start with a look at how the Web.xml is configured

Web.xml Java code     <servlet>               <servlet-name>                    QuartzInitializer                </servlet-name>              < display-name>                    Quartz Initializer Servlet                </display-name>               <servlet-class>                    org.quartz.ee.servlet.QuartzInitializerServlet                </servlet-class>               <load-on-startup>                    -1               </ load-on-startup>              <init-param>                   <param-name >config-file</param-name>                   <param-value>/quartz.properties</param-value>              </init-param>               <init-param>                   <param-name>shutdown-on-unload</param-name>                   <param-value>true</param-value>               </init-param>               <init-param>                   <param-name>start-scheduler-on-load</ param-name>                   <param-value>true</param-value>               </init-param>          </servlet>     

<servlet> <servlet-name> Quartzinitializer </servlet-name> <display-name> Quartz initializer Servlet </display-name> <servlet-cla ss> Org.quartz.ee.servlet.QuartzInitializerServlet </servlet-class> <load- On-startup>-1 </load-on-startup> <init-param> <param- Name>config-file</param-name> <param-value>/quartz.properties</param-value> &  
            Lt;/init-param> <init-param> <param-name>shutdown-on-unload</param-name>  
            <param-value>true</param-value> </init-param> <init-param> <param-name>start-scheduler-on-load</param-name> <param-value>true</param-value&gt  
        ; </init-paRam> </servlet>   




Here, Load-on-startup Specifies whether Quartzservlet is started with the application,-1 indicates no, a positive number indicates that it starts with the application, and the smaller the value, the higher the priority. Initialization parameters, Config-file can specify the Quartzservlet configuration file, where we use the quartz.properties. Shutdown-on-unload, which indicates whether to stop scheduling while uninstalling the application, this parameter recommends true, or your Tomcat process may not stop. Start-scheduler-on-load, which means that the scheduler is started when the application is loaded, and if False, The scheduler specified in Quartz.properties is not loaded until the user accesses the servlet, and before that, if you find schedulerfactory through ServletContext, but to get a specific scheduler, you will surely find The JVM throws a nullpointerexcetion.

Here's a look at Quartz.properties's true colors.

Quartz.properties
Java code     org.quartz.scheduler.instancename = pushdbscheduler      Org.quartz.scheduler.instanceid = one    orgorg.quartz.threadpool.class =  org.quartz.simpl.simplethreadpool       org.quartz.threadpool.threadcount = 4       org.quartz.threadpool.threadpriority = 4     Orgorg.quartz.plugin.jobinitializer.class = org.quartz.plugins.xml.jobinitializationplugin     Org.quartz.plugin.jobIni

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.