Quartz project application notes

Source: Internet
Author: User

QuartzIs a powerful EnterpriseScheduleTool, which is currently the bestOpen SourceSchedule tool. Recently, due to project requirements, some functions of quartz are simply used to record some problems of using quartz in the project.

In the quartz application, we use the following things, schedulefactory, schedfactory, job, jobdetail, and trigger, to briefly describe their usage.

Schedulerfactory is the scheduler factory from which we can obtain the scheduler object managed by the factory.

Schedulerfactory schedulefactory = new stdschedulerfactory ();
Scheduler schedfactory = schedulefactory. getschedfactory ();

Scheduler is a scheduler set that can contain multiple scheduled tasks composed of jobdetail and trigger.
We can obtain scheduler from schedulerfactory.

An interface job is the interface that needs to be implemented for tasks to be executed on each business. This interface has only one method:

Public interface job {
Public void execute (jobexecutioncontext context)
Throws jobexecutionexception;
}

We can define our job execution logic, such as clearing expired data and updating the cache.

Jobdetail describes the specific information of a task, such as the name and group name.
Jobdetail = new jobdetail ("sayhelloworldjob", schedjob. default_group, sayhelloworldjob. Class );
In the preceding constructor, the first is the task name, the second is the group name, and the third is the actual callback class to be executed by the task.

Trigger, as its name implies, is a trigger. Quartz has a good idea of separating the task and the task execution conditions. A trigger is a class that controls the execution conditions of a task. When a trigger determines that the execution conditions are met, the trigger notifies the relevant job to execute the task. The advantages of separation are:
1. You can associate multiple triggers for a job. If any of the conditions is met, job execution can be triggered. In this way, advanced Triggering Conditions of some combinations can be completed.
2. when a trigger fails (for example, a condition that cannot be met), you do not have to declare a new job, instead, you can associate a new trigger for the job so that the job can continue to run.

In the current quartz implementation, there are two types of triggers: simpletrigger and crontrigger. simpletrigger is used to complete some tasks that are executed at a fixed time, for example, one minute from now on; the crontrigger (yes, the same as the Unix cron process) is used to execute the calendar-like task, for example, every Friday, the last day of each month, and so on.

In our project, there are some fixed-time jobs, so we only use simpletrigger.
Trigger trigger = new simpletrigger ("sayhelloworldjobtrigger", scheduler. default_group, new date (), null, 0, 0l );
In this constructor, the first is the trigger name, the second is the trigger group name, the third is the task start time, and the fourth is the end time, the fifth is the number of repetitions (using simpletrigger. repeat_indefinitely Constant indicates unlimited times), and the last one is the repetition cycle (unit: milliseconds). In this way, a task is created immediately and executed only once.

But after we have defined jobdetail, job, and trigger, we can start a job in schedule.

Scheduler. schedulejob (jobdetail, trigger );

The route cute method is used.

Scheduler. Start ();
Do not forget to add the preceding statement to notify quartz to make the schedule take effect.

About the parameter jobexecutioncontext of the execute Method
Jobexecutioncontext is similar to the class functions at the end of many contexts. It provides the runtime context. jobexecutioncontext contains references to many objects, such as schedcontext, jobdetail, and trigger, so that you need the convenience provided by these objects within the execute method at any time.

In the project, we put the information corresponding to the job to be executed in jobexecutioncontext, which can be called during job execution.

Jobdetail. getjobdatamap (). Put (userid, ID );

In the job, we can get the relevant context information:

Jobexecutioncontext. getjobdetail (). getjobdatamap (). getint (userid );

Name and group of jobdetail and trigger
The schedgger instance corresponds to many job and trigger instances. For convenience, quartz uses the name and group features, just as you would like, the same group cannot have two jobdetail with the same name. The same is true for trigger. The same Scheduler cannot have two jobdetail with the same group. The same is true for trigger. The full names of jobdetail and trigger are: group + name

To ensure that our scheduler information is not lost after the server is restarted, we usually use the database to persist scheduler information.
In the Z download package of dbscript: quartz-1.6.0 \ docs \ dbtables, select your own database script to import it to the database.
In the application, we need to configure a quartz. properties to use the database normally. We can find the sample of this file in quartz-1.6.0 \ examples \ example10, and make some modifications to use it in the root directory of your project source code.

Set org. Quartz. jobstore. Class = org. Quartz. impl. jdbcjobstore. jobstoretx to enable JDBC-based quartz information persistence.

Set the following configurations based on the project:
Org. Quartz. jobstore. Class = org. Quartz. impl. jdbcjobstore. jobstoretx
Org. Quartz. jobstore. driverdelegateclass = org. Quartz. impl. jdbcjobstore. stdjdbcdelegate
Org. Quartz. jobstore. useproperties = false
Org. Quartz. jobstore. datasource = myds
Org. Quartz. jobstore. tableprefix = qrtz _
Org. Quartz. jobstore. isclustered = false

Org. Quartz. datasource. myds. Driver = com. MySQL. JDBC. Driver
Org. Quartz. datasource. myds. url = JDBC: mysql: // localhost: 3306/myapplication
Org. Quartz. datasource. myds. User = root
Org. Quartz. datasource. myds. Password =
Org. Quartz. datasource. myds. maxconnections = 5

However, it is not enough to set the database. We also need to automatically start schedener when the application is started. We only need to write a servlet listener and write it on the web. the listener is declared in XML. schedener is automatically executed when the servlet is easy to start.

Public class schedulestartlistener implements servletcontextlistener {
Public void contextinitialized (servletcontextevent ){
Try {
Schedulefactory. getschedfactory (). Start ();
} Catch (schedulerexception e ){
// Write log
}
}

Public void contextdestroyed (servletcontextevent ){
Try {
Schedulefactory. getscheduler (). Shutdown ();
} Catch (schedulerexception e ){
// Write log
}
}
}

Add the following configuration in Web. xml:
<Listener>
<Listener-class> org. agilejava. schedener. schedulestartlistener </listener-class>
</Listener>

The above briefly records some quartz applications in the project. If you have any new usage experiences, you will continue to add them later.

 

Quartz project application notes-Supplement

Reschedule
Reschedulejob (StringTriggername, string groupname, trigger newtrigger)

During the reschedule operation, we usually only need to modify the trigger time. At this time, we only need to re-create a newScheduleTime trigger object, reschedule.

Unschedule
Unschedulejob (string triggername, string groupname)

During unschedule, we only need to know the name and group.

Before and after the schedule operation, the relevant data in the database will be changed. When unschedule is executed or the schedule has been executed, the trigger information in the database will be deleted.

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.