"Quartz" persists the timed task to the database

Source: Internet
Author: User

Excellent articles for > reference

Lesson 9:job Stores

> Storing scheduled Task information with database

The demo of the previous article is to save the information of the scheduled task in memory, see the following configuration

Org.quartz.jobStore.class = Org.quartz.simpl.RAMJobStore

If the scheduled task information is logged with memory, the scheduled task information is lost after the app restarts. For example, user A through the system Setup 1 hours after the z operation, set up, because the system restarts, the newly-launched system will be lost "1 hours after the z operation" of the scheduled task.

If we need to keep the scheduled task information after the system restarts unexpectedly (or unexpectedly), you can use the database to store the scheduled task information.

Org.quartz.jobStore.class = Org.quartz.impl.jdbcjobstore.JobStoreTX

> with MySQL as an example, do a simple demo

The code is basically consistent with the "simple build" of the previous section "Quartz" Quartz, which uses Quartz alone, and lists only the different codes.

Due to the need to connect to the MySQL database, you need to add the database JDBC driver, here in the form of pom download, you can directly introduce the package

<Dependency>    <groupId>Mysql</groupId>    <Artifactid>Mysql-connector-java</Artifactid>    <version>5.1.35</version></Dependency>

Then you need to build some quartz tables in the database, and MySQL's Build and delete table scripts are as follows. If you are using a different database, you can download the quartz distribution, under \docs\dbtables.

# # Quartz seems to  WorkBest withThe driver Mm.mysql-2.0.7-bin.jar## consider using MySQL withInnoDB tables toAvoid locking issues##inchYour Quartz propertiesfile, you'll need to set # Org.quartz.jobStore.driverDelegateClass = Org.quartz.impl.jdbcjobstore.stdjdbcdelegate#drop TABLE IF EXISTS qrtz_fired_triggers;drop table if EXISTS qrtz_paused_trigger_grps;drop table if EXISTS qrtz_scheduler_state;drop  Table if EXISTS qrtz_locks;drop table if EXISTS qrtz_simple_triggers;drop table if EXISTS qrtz_simprop_triggers;drop table If EXISTS qrtz_cron_triggers;drop table if EXISTS qrtz_blob_triggers;drop table if EXISTS qrtz_triggers;drop table if EXI STS qrtz_job_details;drop TABLE IF EXISTS qrtz_calendars; CREATE TABLE qrtz_job_details (sched_name varchar) NOT NULL, Job_name varchar ($) NOT NULL, Job_group VAR CHAR (+) NOT NULL, DESCRIPTION varchar (+) NULL, Job_class_name varchar () NOT NULL, is_durable varchar (1) Not NULL, is_nonconcurrent varchar (1) is not NULL, Is_update_data varchar (1) is not NULL, requests_recovery varchar (1) Not NULL, job_data BLOB null, PRIMARY KEY (Sched_name,job_name,job_group)); CREATE TABLE QRtz_triggers (sched_name varchar) NOT NULL, Trigger_name varchar ($) NOT NULL, Trigger_group varchar (200) Not NULL, Job_name varchar ($) NOT NULL, Job_group varchar ($) NOT NULL, DESCRIPTION varchar (+) NULL, NEX  T_fire_time BIGINT (+) NULL, Prev_fire_time BIGINT (All) NULL, priority INTEGER NULL, Trigger_state VARCHAR (+) not NULL, Trigger_type VARCHAR (8) NOT NULL, Start_time BIGINT (all) not NULL, End_time BIGINT (All) null, Calendar_na ME VARCHAR ($) NULL, MISFIRE_INSTR SMALLINT (2) NULL, Job_data BLOB null, PRIMARY KEY (sched_name,trigger_name,tr Igger_group), FOREIGN KEY (sched_name,job_name,job_group) REFERENCES qrtz_job_details (sched_name,job_name,job_gr OUP)); CREATE TABLE qrtz_simple_triggers (sched_name varchar) NOT NULL, Trigger_name varchar ($) NOT NULL, Trigge R_group VARCHAR ($) NOT NULL, Repeat_count BIGINT (7) is not NULL, Repeat_interval BIGINT (+) not NULL, Times_trigge RED BIGINT (Ten) not NULL, PRIMARY Key (Sched_name,trigger_name,trigger_group), FOREIGN key (Sched_name,trigger_name,trigger_group) REFERENCES qrtz_triggers (Sched_name,trigger_name,trigger_group)); CREATE TABLE qrtz_cron_triggers (sched_name varchar) NOT NULL, Trigger_name varchar ($) NOT NULL, Trigger_ GROUP varchar is not NULL, cron_expression varchar ($) NOT NULL, time_zone_id varchar, PRIMARY KEY (sched_ Name,trigger_name,trigger_group), FOREIGN KEY (sched_name,trigger_name,trigger_group) REFERENCES qrtz_triggers (S Ched_name,trigger_name,trigger_group)); CREATE TABLE qrtz_simprop_triggers (sched_name varchar) NOT NULL, Trigger_name varchar ($) NOT NULL , Trigger_group varchar () NOT NULL, str_prop_1 varchar (+) NULL, str_prop_2 varchar (+) NULL, str_prop_3     VARCHAR (+) NULL, int_prop_1 int NULL, int_prop_2 int NULL, long_prop_1 BIGINT null, long_prop_2 BIGINT NULL, Dec_prop_1 NUMERIC (13,4) NULL, DEc_prop_2 NUMERIC (13,4) NULL, bool_prop_1 varchar (1) NULL, bool_prop_2 VARCHAR (1) NULL, PRIMARY KEY (sched_name,t Rigger_name,trigger_group), FOREIGN KEY (sched_name,trigger_name,trigger_group) REFERENCES qrtz_triggers (SCHED_NAME , Trigger_name,trigger_group)); CREATE TABLE qrtz_blob_triggers (sched_name varchar) NOT NULL, Trigger_name varchar ($) NOT NULL, Trigger_ GROUP VARCHAR (+) NOT NULL, Blob_data BLOB null, PRIMARY KEY (sched_name,trigger_name,trigger_group), FOREIGN KE Y (Sched_name,trigger_name,trigger_group) REFERENCES qrtz_triggers (Sched_name,trigger_name,trigger_group)); CREATE TABLE qrtz_calendars (sched_name varchar) NOT NULL, Calendar_name varchar ($) NOT NULL, CALENDAR B LOB not NULL, PRIMARY KEY (sched_name,calendar_name));     CREATE TABLE Qrtz_paused_trigger_grps (sched_name varchar) NOT NULL, Trigger_group varchar (+) NOT NULL, PRIMARY KEY (Sched_name,trigger_group)); CREATE TABLE Qrtz_fired_trIggers (sched_name varchar) NOT NULL, entry_id varchar (-) NOT NULL, trigger_name varchar (+) NOT NULL, Trigger_group varchar () NOT NULL, instance_name varchar ($) NOT NULL, Fired_time BIGINT (all) not NULL, sched_ Time BIGINT is not NULL, the priority INTEGER is not NULL, the state varchar (+) is not NULL, job_name varchar ($) NULL, J Ob_group varchar ($) NULL, is_nonconcurrent varchar (1) NULL, Requests_recovery varchar (1) NULL, PRIMARY KEY (SCH ed_name,entry_id)); CREATE TABLE qrtz_scheduler_state (sched_name varchar) NOT NULL, instance_name varchar ($) NOT NULL, Last_ Checkin_time BIGINT () not NULL, Checkin_interval BIGINT (all) not NULL, PRIMARY KEY (sched_name,instance_name)); CREATE TABLE qrtz_locks (sched_name varchar) NOT NULL, Lock_name varchar (+) NOT NULL, PRIMARY KEY (SCHED _name,lock_name)); commit;
View Code

Look, this script, in addition to the construction and deletion of the table statement, also gives the Org.quartz.jobStore.driverDelegateClass configuration, this configuration to Quartz.properties

Note : You may also notice that these tables are prefixed with qrtz_, which is the default prefix. If you need to use a different prefix (personalization requirements, or need to configure multiple quartz instances), you can configure it in the following items (in quartz.properties)

Org.quartz.jobStore.tablePrefix = Qrtz_

The main change is quartz.properties.

Org.quartz.scheduler.instanceName = MySchedulerorg.quartz.threadPool.threadCount = 3org.quartz.jobstore.class = Org.quartz.impl.jdbcjobstore.JobStoreTXorg.quartz.jobStore.driverDelegateClass = Org.quartz.impl.jdbcjobstore.StdJDBCDelegateorg.quartz.jobStore.tablePrefix = Qrtz_ Org.quartz.jobStore.dataSource = MyDSorg.quartz.dataSource.myDS.driver = Com.mysql.jdbc.Driverorg.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/ll?characterencoding= Utf-8org.quartz.datasource.myds.user = Rootorg.quartz.dataSource.myDS.password = 123456org.quartz.datasource.myds.maxconnections = 5

OK, then we can start the bootstrap class, register, run the scheduled task. You can see that there is already a record of this scheduled task in the MySQL table.

You can then stop the bootstrap class, delete or annotate the code in the bootstrap class about registering, start a scheduled task, and then start the Bootstrap class, and the scheduled task of registering a single operation will continue to run.

If there is no accident, the scheduled task that is registered with the operation will continue to run.

"Quartz" persists the timed task to the database

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.