Java Job scheduling Class Library Quartz basic use guide _java

Source: Internet
Author: User
Tags time interval

One, commonly used interface:
1. Job interface: This interface has only one method

void execute (Jobexecutioncontext context)

The developer implements this interface to define the tasks that need to be performed. The Jobexecutioncontext class provides various information about the scheduling context

2, Jobdetail: Used to describe the job implementation class and other static information

3. Trigger: Describe the time trigger rule that triggers job execution

4. Calendar: Defines a time space that the association trigger may (or cannot) trigger. It does not define the actual time of the trigger, but is used when the normal schedule need to limit trigger triggering. Most calendar contains default all the time, and the user goes to exclude part of the time.

5, Scheduler: Run the container, use Schedulerfactory to create Scheduler instance

Second, the code example:

1, the use of quartz, the need to implement job interface;

public class Testjob implements Job {public
 void execute (Jobexecutioncontext context) throws Jobexecutionexception {
 System.out.println ("Hello world! -"+ new Date ());
 Do more ...}}


2, scheduling "relatively simple, direct look at the code can be"

public class Quartztest {public
 static void Main (String args[]) throws Schedulerexception, ParseException {
 jobd Etail jobdetail= jobbuilder.newjob (testjob.class)
  . Withidentity ("Testjob_1", "Group_1")
  . Build ();
 
 Trigger trigger= triggerbuilder
  newtrigger (). Withidentity (
  "trigger_1", "Group_1").
  Startnow ()
  . Withschedule (Simpleschedulebuilder.simpleschedule ()
   . Withintervalinseconds (10)//time interval
   . Withrepeatcount (5)//repetition (will be performed 6 times)
   )
  . Build ();
 Schedulerfactory SF = new Stdschedulerfactory ();
 Scheduler sched = Sf.getscheduler ();
 
 Sched.schedulejob (Jobdetail,trigger);
 
 Sched.start ();
 
 }

The 2.2.1 version is used here, and refer to the official documentation example. See some reference books used in the version of older, so there will be some access, many methods have been discarded, so or directly to see the document more really, Quartz's official website address is: http://www.quartz-scheduler.org/

Iii. Quartz2.2.1 Configuration File Example

# Default Properties file for use by Stdschedulerfactory # to create a Quartz Scheduler Instance, if a different # proper
Ties file is not explicitly specified.
# #集群配置 Org.quartz.scheduler.instanceName:DefaultQuartzScheduler Org.quartz.scheduler.rmi.export:false
 
Org.quartz.scheduler.rmi.proxy:false Org.quartz.scheduler.wrapJobExecutionInUserTransaction:false
Org.quartz.threadPool.class:org.quartz.simpl.SimpleThreadPool org.quartz.threadpool.threadcount:10
Org.quartz.threadpool.threadpriority:5
 
Org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread:true org.quartz.jobstore.misfirethreshold:60000 #=================================================================== ========= # Configure Jobstore #============================================================================ #默认配置, Data is saved to memory #org. Quartz.jobStore.class:org.quartz.simpl.RAMJobStore #持久化配置 Org.quartz.jobstore.class:o Rg.quartz.impl.jdbcjobstore.JobStoreTX Org.quartz.jobStore.driverDelegateClaSs:org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.useProperties:true #数据库表前缀 Org.quartz.jobStore.tablePrefix:qrtz_ Org.quartz.jobStore.dataSource:qzDS #===================================== ======================================= # Configure DataSources #=================================================
=========================== #JDBC驱动 Org.quartz.dataSource.qzDS.driver:com.mysql.jdbc.Driver
Org.quartz.datasource.qzds.url:jdbc:mysql://localhost:3306/quartzdb Org.quartz.dataSource.qzDS.user:root
 org.quartz.datasource.qzds.password:123456 org.quartz.datasource.qzds.maxconnection:10

Iv. database-related
persistence is required to create a quartz datasheet in the corresponding database, and the docs/dbtables in the Quartz release package has SQL scripts corresponding to the different databases

For example, this is used for MySQL:

Data table field Explanation:

  • Table Qrtz_job_details: Save the job details, which requires the user to initialize the data
  • Job_name: The name of the job in the cluster, the name of the user can be customized, no forced requirements
  • Job_group: The name of the group that the job belongs to in the cluster, which the user is free to customize, no forced requirements
  • Job_class_name: The full package name of the implementation class, Quartz is the path to Classpath to find the job class
  • Is_durable: Persistent, setting this property to 1,quartz will persist the job to the database
  • Job_data: A BLOB field that holds the persistent Job Object
  • Table Qrtz_triggers: Saving trigger information
  • Trigger_name:trigger's name, the name of the user can be customized at will, no forced requirements
  • Trigger_group:trigger the name of the group, the name of the user's own arbitrary customization, no forced requirements
  • Foreign keys for job_name:qrtz_job_details table job_name
  • Foreign keys for job_group:qrtz_job_details table Job_group
  • Trigger_state: The current trigger state, set to acquired, if set to waiting, the job does not trigger
  • Trigger_cron: Trigger type, using cron expression
  • Table Qrtz_cron_triggers: Storage of cron expression tables
  • Foreign keys for trigger_name:qrtz_triggers table trigger_name
  • Foreign keys for trigger_group:qrtz_triggers table Trigger_group
  • Cron_expression:cron expression
  • Table Qrtz_scheduler_state: Store instance information in a cluster, quartz read the table's information periodically to determine the current status of each instance in the cluster
  • Instance_name: The name of the Org.quartz.scheduler.instanceId configuration in the previous configuration file will be written to the field, if set to Auto,quartz will produce a name based on the physical machine name and the current time
  • Last_checkin_time: Last Check time
  • Checkin_interval: Check time interval
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.