Development of task management platform based on quartz.net (1)--Fundamentals

Source: Internet
Author: User

Because of the work reason, need to use the task monitoring platform, after some groping, found quratz.net this open-source job scheduling framework. Prevent forgetting, make a memo.

1 quartz.net Overview

Open Source Components Quartz.net, which is an open-source job scheduling framework, is a. Net migration of the Opensymphony Quartz API, written in C # that can be used in WinForm and ASP. It provides a great deal of flexibility without sacrificing simplicity. It can be used to create simple or complex schedules for the execution of a job.

Quartz in many features, such as: Database support, clustering, plug-ins, support cron-like expressions, and so on.

Quartz.net is an open-source task scheduling tool, equivalent to the job in the database, Windows scheduled Tasks, Unix/linux under the Cron, but Quartz can be more detailed scheduling control, task scheduling of the domain issues are highly abstracted, Implement flexible scheduling of jobs.

2 QuartzBasic Concepts

The quartz consists of several basic interfaces, such as:

Scheduler:

Scheduler , which can be filled with numerous Jobdetail and Trigger, when the container starts, each jobdetail inside will be executed automatically according to the trigger.

Trigger:

Trigger, configure the configuration of scheduling parameters, when to tune. Description Trigger The time-triggered rule for job execution. There are mainly two subclasses of Simpletrigger and Crontrigger. Simpletrigger is the most suitable choice when only one or a fixed time interval is required, while Crontrigger can define scheduling schemes for various complex time rules through cron expressions: 5:0 per 9:00, Monday, Wednesday, Friday pm 0 implementation, etc.;

Job:

The task is an interface, there is only one method void execute (jobexecutioncontext context), the developer implements the interface definition to run the task, The Jobexecutioncontext class provides various information about the scheduling context. The job runtime information is stored in the Jobdatamap instance;

Jobdetail:

Executable work, it may itself be stateful. Quartz re-creates a job instance each time the job is executed, so it does not directly accept an instance of the job, instead it receives a job implementation class so that the job is instantiated by the reflection mechanism of newinstance () at runtime. Therefore, a class is needed to describe the job implementation class and other related static information, such as job name, description, Association Listener and other information, jobdetail assume this role.

Jobdatamap:

Stores information about the job runtime.

Calendar:

calendar feature, a Trigger can be associated with more than one calendar to exclude or include certain points in time. Let's say we schedule a task for Monday 10:00 every week, but if you encounter a legal holiday and the task does not execute, then you need to use the calendar for point exclusion on the basis of the trigger trigger mechanism.

ThreadPool:

Scheduler uses a thread pool as the infrastructure for the task to run, and the task improves efficiency by sharing threads in the thread pool.

Schedulercontext:

The context of the scheduler

3 Quartz Operating principle3.1 Task Scheduling principle

Startup process:

(1) Instantiate a scheduler factory schedulerfactory, you can use a configuration file, or code to implement a custom instance.

(2) using the Getscheduler () method to get the scheduler instance from the Scheduler factory

(3) Scheduler has a quartzschedulerthread (subclass of Thread) property that instantiates an object when the scheduler is instantiated and starts the thread object with Threadexecutor. The thread is the dispatch thread, and the main task is to get the trigger to be triggered (the default 30s schedule once) from Jobstore. At this point the dispatch thread starts, but is in the pause state.

Scheduling process:

(4) using the Schedulejob () method to store tasks and triggers in Jobstore, the start () method sets the Quartzschedulerthread pause state to False to notify the dispatch thread to perform the task, The dispatch thread will then continue to take the task from the Jobstore to be triggered.

3.2Task Execution principle

(1) The dispatch thread first gets the available threads in the thread pool and, if not, blocks them.

(2) from the Jobstore (get the trigger from the storage media, the storage media can be memory or the database) get (in the next 30s) trigger, and then wait for the trigger to fire.

(3) The dispatch thread creates a Jobrunshell (that is, a runnable) and then invokes the thread from the thread pool to perform the task.

get Trigger, Jobdetail, and build job instances, and execute the job's execute interface function.

3.3 Task Persistence

Quartz provides two types of basic job storage. The first type is called Ramjobstore, and the second type is called the JDBC Job store. By default, Quartz saves the running information for task scheduling in memory, which provides the best performance because in-memory data access is the fastest. The disadvantage is the lack of data persistence, when the program stops or the system crashes, all running information is lost. is also not convenient for cluster processing, so this module uses the JDBC Job storage scheme,Quartz.net supports the storage of multiple databases for task persistence.

A variety of official database scripts: Https://github.com/quartznet/quartznet/tree/master/database/tables

3.4 QuartzTable Structure

1.qrtz_job_details: The details of the JOBare stored , including: [DESCRIPTION] Description, [is_durable] is persistent, [job_data] persistent objects and other basic information.

2,qrtz_triggers: Trigger information , including: Job name, group foreign key, [DESCRIPTION] trigger description and other basic information, and [start_time] Start execution time, [End_time] End execution time, [prev_fire_time] Last execution time, [next_fire_time] Next execution time, [Trigger_type] Trigger type: Simple and cron,[trigger_state] Execution Status: waiting,paused,acquired: Wait, pause, run.

3.qrtz_cron_triggers: Save CRON expression.

4,qrtz_scheduler_state: Storage cluster Note instance information, Quartz will periodically read the table information to determine the current status of each instance of the cluster, instance_name:  The name of the Org.quartz.scheduler.instanceId configuration in the previous configuration file is written to this field, and if set to Auto,quartz a name is generated based on the physical machine name and the current time. [Last_checkin_time] Last check time, [checkin_interval] check interval time.

5.Qrtz_paused_trigger_grps: Paused task group information.

6,qrtz_locks, pessimistic lock occurred record information.

7,qrtz_fired_triggers, the trigger information that is running.

8,Qrtz_simple_triggers, simple departure device details.

9.Qrtz_blob_triggers, the trigger is saved as a binary large object type (for quartz users to trigger their own triggers, but Jobstore does not understand how to store instances).

The above is the basic principle of quartz.net.

Development of task management platform based on quartz.net (1)--Fundamentals

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.