Task Scheduling through the configuration file

Source: Internet
Author: User

Quartz. NET 2.0 can easily implement flexible task scheduling configuration through the configured XML file (version 1.0 already supports, but the configuration file format has changed somewhat)

The default quartz job configuration file is quartz_jobs.xml. Under the root directory of the quartz service, you can use quartz. plugin. xml. filenames = ~ in quartz. config ~ /Quartz_jobs.xml

The default quartz. config file structure is as follows:

Quartz. config
 1 # You can configure your scheduler in either <quartz> configuration section 2 # or in quartz properties file 3 # Configuration section has precedence 4  5 quartz.scheduler.instanceName = ServerScheduler 6  7 # configure thread pool info 8 quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz 9 quartz.threadPool.threadCount = 1010 quartz.threadPool.threadPriority = Normal11 12 # job initialization plugin handles our xml reading, without it defaults are used13 quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz14 quartz.plugin.xml.fileNames = ~/quartz_jobs.xml15 16 # export this server to remoting context17 quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz18 quartz.scheduler.exporter.port = 55519 quartz.scheduler.exporter.bindName = QuartzScheduler20 quartz.scheduler.exporter.channelType = tcp21 quartz.scheduler.exporter.channelName = httpQuartz

First, let's take a look at the simple quartz_jobs.xml example.

Quartz_jobs.xml
 1 <?xml version="1.0" encoding="UTF-8"?> 2  3 <!-- This file contains job definitions in schema version 2.0 format --> 4  5 <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> 6  7   <processing-directives> 8     <overwrite-existing-data>true</overwrite-existing-data> 9   </processing-directives>10 11   <schedule>12 13     <job>14         <name>sampleJob</name>15         <group>sampleGroup</group>16         <description>Sample job for Quartz Server</description>17         <job-type>Quartz.Server.SampleJob, Quartz.Server</job-type>18         <durable>true</durable>19         <recover>false</recover>20     </job>21     <trigger>22       <simple>23         <name>sampleSimpleTrigger</name>24         <group>sampleSimpleGroup</group>25         <description>Simple trigger to simply fire sample job</description>26         <job-name>sampleJob</job-name>27         <job-group>sampleGroup</job-group>28         <misfire-instruction>SmartPolicy</misfire-instruction>29         <repeat-count>-1</repeat-count>30         <repeat-interval>10000</repeat-interval>31       </simple>32     </trigger>33 34     <job>35       <name>CommissionJob</name>36       <group>CommissionJob</group>37       <description>Sample job for Quartz Server</description>38       <job-type>Settlement.Jobs.CommissionJob, Settlement.Jobs</job-type>39       <durable>true</durable>40       <recover>false</recover>41     </job>42      <trigger>43       <cron>44         <name>sampleSimpleTrigger2</name>45         <group>sampleSimpleTrigger2</group>46         <job-name>sampleJob2</job-name>47         <job-group>sampleGroup2</job-group>48         <cron-expression>0/10 * * * * ?</cron-expression>49       </cron>50     </trigger>51   </schedule>52 </job-scheduling-data>

Job-scheduling-data is not explained for the following nodes

Processing-directives exists in the example officially provided by this node. If you do not have a deep understanding of what to use, keep the default configuration.

You can configure multiple schedule task scheduling sets, but it seems that only the first configuration takes effect. I would like to tell you how to make multiple schedule take effect at the same time. All jobs and triggers are placed under this node.

A job is actually a <job-Detail> In 1.x. This node is used to define each specific task. Create multiple job nodes for multiple tasks.

  • Name (required) task name. The names of multiple jobs in the same group cannot be the same. If the group is not set, all jobs in the group are in the same group. For example: <Name> samplejob </Name>
  • Group (optional) indicates the group to which the task belongs. For example, <group> samplegroup </group>
  • Description (optional) task description, used to describe the specific content of the task, such as: <description> sample job for quartz server </description>
  • Job-type (required) indicates the task type, the specific type of the task, and the Assembly to which the task belongs. Format: Class Name that implements the ijob interface that contains the complete namespace, assembly name, such: <job-type> quartz. server. samplejob, quartz. server </job-type>
  • I do not know the specific role of durable (Optional). In the official example, the default value is true, for example, <durable> true </durable>
  • The function of recover (optional) is unknown. In the official example, the default value is false, for example, <recover> false </recover>.

A trigger is used to define the method used to start a job. A job can define multiple triggers, and multiple triggers can be executed and scheduled independently, each trigger must and can only define one trigger type (calendar-interval, simple, cron)

Calendar-interval is a trigger type, which is rarely used and skipped here.

A trigger for a simple task, which can be scheduled for repeated tasks.

  • Name (required) Name of the trigger, which must be different in the same group
  • Group (optional) trigger Group
  • Description (optional) trigger description
  • The name of the job to be scheduled. The job-name must be exactly the same as the name in the corresponding job node.
  • The group to which the job belongs. The value must be identical to the group in the job.
  • Start-time (optional) UTC time when the task starts to run. Beijing time must be +, for example, <start-time> 2012-04-01t08: 00: 00 + 08: 00 </start-time> indicates that execution starts at, January 1, April 1, 2012, Beijing time. Note that this attribute is detected when the service is started or restarted, if this attribute is not set or the start-time is earlier than the current timeRun nowOne scheduling. If the set time is later than the current time, the service will wait until the set time is the same.First executionTask. Do not set this attribute unless otherwise required.
  • Repeat-count (required) number of task executions. For example, <repeat-count>-1 </repeat-count> indicates unlimited execution, <repeat-count> 10 </repeat-count> indicates 10 executions.
  • Repeat-interval (required) task trigger interval (MS), for example, <repeat-interval> 10000 </repeat-interval> is executed every 10 seconds.

Cron complex task trigger-use cron expressions to customize Task Scheduling (highly recommended)

  • Name (required) Name of the trigger, which must be different in the same group
  • Group (optional) trigger Group
  • Description (optional) trigger description
  • The name of the job to be scheduled. The job-name must be exactly the same as the name in the corresponding job node.
  • The group to which the job belongs. The value must be identical to the group in the job.
  • Start-time (optional) UTC time when the task starts to run. Beijing time must be +, for example, <start-time> 2012-04-01t08: 00: 00 + 08: 00 </start-time> indicates that execution starts at, January 1, April 1, 2012, Beijing time. Note that this attribute is detected when the service is started or restarted. If this attribute is not set, the service executes task scheduling according to Cron-expression settings. If the start-time is earlier than the current time, the cron-expression settings are ignored after the service is started,Run nowAfter one scheduling, execute the task scheduling according to Cron-expression. If the set time is later than the current time, the service will apply Cron-expression only after the set time is the same, execute task scheduling according to rules. Do not set this attribute unless otherwise required.
  • Cron-expression (required) cron expression, for example, <Cron-expression> 0/10 ****? </Cron-expression> Run Once every 10 seconds

I learned about quartz. after the quartz_jobs.xml configuration of NET 2.0, You can reference quartz in the project. the DLL file then implements the ijob interface and flexibly configures quartz_jobs according to the needs of the actual project. No additional development is required to implement flexible multi-task scheduling. Note that after the quartz_jobs.xml file is modified, the quartz service does not reload the file by default. To make the modified file take effect, restart the service. In addition, use the start-time attribute with caution, if necessary, you can delete this attribute immediately after use. Otherwise, it may cause serious consequences)

The introduction is superficial, and I have not studied it in depth. I just figured it out based on my daily use experience. I sorted out that it is enough to schedule tasks through configuration files for simple daily applications.

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.