Quartz persisting a timed task to a database

Source: Internet
Author: User
Tags numeric mysql database

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. Excellent reference articles

Lesson 9:job Stores using a database to store scheduled task information

In the simple use of quartz in the previous section, the task is placed in the content. Configured as follows,

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

To prevent the application from restarting the task from being lost, you can use the database to store scheduled task information. Configured as follows,

Org.quartz.jobStore.class = Org.quartz.impl.jdbcjobstore.JobStoreTX
persisting tasks to the MySQL database

The quartz with the previous section is basically consistent with the use of the Web. Only different codes are listed below. join the MySQL driver

<dependency>
    <groupId>mysql</groupId>
    <artifactid>mysql-connector-java</ artifactid>
    <version>5.1.35</version>
</dependency>
MySQL's Build table statement

The

Then needs to build some quartz tables in the database, and the MySQL 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 work best with the driver Mm.mysql-2.0.7-bin.jar # * Please consider using MySQL with InnoDB tables to Avoid locking issues # your Quartz properties file, 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 EXISTS 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 varchar ($) NOT NULL, DESCRIPTION varchar (+) NULL, Job_class_name varchar (+) NOT NULL, is_durable Varc HAR (1) Not NULL,
    Is_nonconcurrent varchar (1) NOT NULL, Is_update_data varchar (1) isn't null, requests_recovery varchar (1) Not NU

LL, 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_g Roup varchar ($) NOT NULL, Job_name varchar ($) NOT NULL, Job_group varchar ($) NOT NULL, DESCRIPTION Varc HAR (+) NULL, Next_fire_time BIGINT (All) null, Prev_fire_time BIGINT (All) NULL, priority INTEGER NULL, TRIG Ger_state varchar (+) NOT NULL, Trigger_type varchar (8) is not NULL, Start_time BIGINT (all) not NULL, End_time BIG  INT () NULL, Calendar_name VARCHAR ($) NULL, MISFIRE_INSTR SMALLINT (2) NULL, Job_data BLOB NULL, PRIMARY Key (Sched_name,trigger_name,trigger_group), FOREIGN key (Sched_name,job_name,job_group) REFERENCES qrtz_job_

DETAILS (Sched_name,job_name,job_group)); CREATE TABLE Qrtz_simplE_triggers (sched_name varchar) NOT NULL, Trigger_name varchar ($) NOT NULL, Trigger_group varchar (200 ) not NULL, Repeat_count BIGINT (7) is not NULL, Repeat_interval BIGINT (a) not NULL, times_triggered BIGINT (Ten) N
        OT 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, TRIG Ger_group varchar (+) 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_t

Riggers (Sched_name,trigger_name,trigger_group)); CREATE TABLE qrtz_simprop_triggers (sched_name varchar) not NULL, Trigger_name varchar ($) Not N ULL, Trigger_grOUP varchar (+) NOT NULL, str_prop_1 varchar (512) 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,trigger_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, TRIG Ger_group VARCHAR ($) NOT NULL, Blob_data BLOB NULL, PRIMARY KEY (Sched_name,trigger_name,trigger_group), for Eign KEY (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 BLOB is 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 (a) NOT NULL, Trigger_group varchar ($) NOT NULL, instance_name varchar ($) NOT NULL, Fired_ti 
    ME BIGINT () not NULL, Sched_time BIGINT (all) not NULL, the priority INTEGER is not NULL, the state VARCHAR (+) is not NULL, Job_name varchar ($) NULL, Job_group varchar ($) NULL, is_nonconcurrent varchar (1) NULL, Requests_recov

ERY VARCHAR (1) NULL, PRIMARY KEY (sched_name,entry_id)); CREATE TABLE qrtz_scheduler_state (sched_name varchar) NOT NULL, instance_name varchar (+) NOT NULL, L
    Ast_checkin_time BIGINT (+) not NULL,Checkin_interval BIGINT (+) not NULL, PRIMARY KEY (sched_name,instance_name)); CREATE TABLE qrtz_locks (sched_name varchar) NOT NULL, Lock_name varchar (+) NOT NULL, PRIMARY KEY (S


Ched_name,lock_name));
 Commit
mysql table prefix

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_
Modification of Quartz.properties
Org.quartz.scheduler.instanceName = Myscheduler
org.quartz.threadPool.threadCount = 3
Org.quartz.jobStore.class = Org.quartz.impl.jdbcjobstore.JobStoreTX
Org.quartz.jobStore.driverDelegateClass = Org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = Qrtz_
Org.quartz.jobStore.dataSource = MyDS

org.quartz.dataSource.myDS.driver = Com.mysql.jdbc.Driver
Org.quartz.dataSource.myDS.URL = Jdbc:mysql://localhost:3306/ll?characterencoding=utf-8
Org.quartz.dataSource.myDS.user = root
Org.quartz.dataSource.myDS.password = 123456
Org.quartz.dataSource.myDS.maxConnections = 5
test tasks stored in MySQL database

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.
The test code used for the test,

import java.util.concurrent.TimeUnit; import org.quartz.Scheduler; import
Org.quartz.SchedulerException;
Import Org.quartz.impl.StdSchedulerFactory;
Import Org.slf4j.Logger;

Import Org.slf4j.LoggerFactory;

    public class Bootstrapstart {private static Logger Logger = Loggerfactory.getlogger (Bootstrapstart.class); public static void Main (string[] args) {try {//Get Scheduler instance Scheduler Scheduler = Std
            Schedulerfactory.getdefaultscheduler ();

            Start scheduler Scheduler.start ();
            /* Run for Watcher, this setting main program sleeps 3 minutes before continuing to run (because the next step is "Close scheduler") */try {TimeUnit.MINUTES.sleep (3);
            } catch (Interruptedexception e) {e.printstacktrace ();

        }//Close scheduler Scheduler.shutdown ();
        } catch (Schedulerexception se) {logger.error (Se.getmessage (), SE); }
    }

}

If no surprises occur, the scheduled task that is registered with the operation continues to run.

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.