Use config to flexibly configure Job, Trigger, and quarz. nettrigger in Quarz.net

Source: Internet
Author: User

Use config to flexibly configure Job, Trigger, and quarz. nettrigger in Quarz.net

When a scheduled task is often encountered in a project, Timer is usually the first thing to think of. However, this stuff is too weak. In fact, it usually uses a professional third-party scheduling framework, for example

Quartz, which has powerful functions and application flexibility. I want to know more about it. This article describes how to configure jobs and triggers through code and configuration files.

 

I. Conventional job and trigger Configuration

This conventional method is also the first thing we have learned from Quartz, that is, using JobBuilder and TriggerBuilder to chain an IJobDetail and ISimpleTrigger, such as the following code.

 1  class Program 2     { 3         static void Main(string[] args) 4         { 5             IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); 6  7             scheduler.Start(); 8  9             var job = JobBuilder.Create<HelloJob>().Build();10 11             var trigger = TriggerBuilder.Create().WithSimpleSchedule(m => m.WithIntervalInSeconds(1)12                                                                             .RepeatForever())13                                                  .StartNow()14                                                  .Build();15 16             scheduler.ScheduleJob(job, trigger);17 18             Console.Read();19         }20     }

The above Code shows that during BuildJob, the provided HelloJob is used as the current scheduled task. The following ISimpleTrigger indicates that the HelloJob is executed every second,

That's all. The specific definition of HelloJob is very simple. It implements an IJob interface.

1 public class HelloJob: IJob2 {3 private string name = string. empty; 4 public void Execute (IJobExecutionContext context) 5 {6 Console. writeLine ("Current Time: {0}", DateTime. now); 7} 8}

 

Then you can run the program and check the final result. You can see that each second has a data output:

 

This method seems perfect, but there is also a disadvantage. Now there is a change in demand. I need to change WithIntervalInSeconds (1) To WithIntervalInSeconds (2), that is, the execution per second.

Once changed to every two seconds for execution, this is a headache, you have to face the need to change once, you need to compile and re-release, this is so frustrating, I think you are not willing to do it? In the face of such requirements, it must be

If you don't believe it, you can look down.

 

Ii. Define job and trigger in quartz_jobs.xml

I also told you about the disadvantages of the above hard encoding. Next, let's take a look at how to use xml to configure the job and trigger. When we get the quartz framework from nuget, we will find your

In this solution, an xsd file is added, but many people may not know what the xsd file is .... It is actually used to prompt the code when writing xml. Well, I will try it here.

Step by step.

 

1. There is nothing to say about downloading files from nuget. Right-click "Reference" and select "manage NuGet package:

 

Then you will see an additional job_scheduling_data_2_0.xsd file.

 

2. Create a quartz_jobs.xml file, select the xml menu bar in visual studio, and select the schema menu item.

 

3. In the pop-up dialog box, select "add", find job_scheduling_data_2_0.xsd generated from nuget, and click "finish.

 

4. Then you can encode the code in xml. You will find a prompt about the job and trigger code... This greatly improves our development

Efficiency, right.

 

Well, the following is a complete xml case. We can see from xml that a job and trigger are defined in schedule, the namespace and Class Name of the job to be executed are defined in the job-type node,

This is also the final schedule task to be scheduled.

<?xml version="1.0" encoding="utf-8" ?><job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData">  <processing-directives>    <overwrite-existing-data>true</overwrite-existing-data>  </processing-directives>  <schedule>    <job>      <name>sampleJob</name>      <group>sampleGroup</group>      <description>Sample job for Quartz Server</description>      <job-type>ConsoleApplication5.HelloJob,ConsoleApplication5</job-type>      <durable>true</durable>      <recover>false</recover>    </job>    <trigger>      <simple>        <name>sampleSimpleTrigger</name>        <group>sampleSimpleGroup</group>        <description>Simple trigger to simply fire sample job</description>        <job-name>sampleJob</job-name>        <job-group>sampleGroup</job-group>        <misfire-instruction>SmartPolicy</misfire-instruction>        <repeat-count>-1</repeat-count>        <repeat-interval>1000</repeat-interval>      </simple>    </trigger>  </schedule></job-scheduling-data>

 

5. The job configuration file is basically done, and then the Quartz. Plugin. Xml. XMLSchedulingDataProcessorPlugin class is configured for processing.

This xml file also specifies the quartz_jobs.xml path, as shown below:

 1     class Program 2     { 3         static void Main(string[] args) 4         { 5             var factory = new StdSchedulerFactory(new System.Collections.Specialized.NameValueCollection() 6                 { 7                     {"quartz.plugin.xml.fileNames","~/quartz_jobs.xml" }, 8                     {"quartz.plugin.xml.type","Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin,Quartz"} 9                 });10 11             IScheduler scheduler = factory.GetScheduler();12 13             scheduler.Start();14         }15     }

 

Now, the configuration is complete. The last point is to configure quartz_jobs.xml to always copy to the binfile,

 

Finally, run the source code to see what the effect is. If no, is it awesome.

 

Now, do you find that your flexibility is greatly improved? If you change 1 s to 2 s, you only need to set <repeat-interval> 1000 </repeat-interval> In quartz_jobs.xml, change

<Repeat-interval> 2000 </repeat-interval> is it cool to configure trigger dynamically.

 

Complete case: leleapplication5.zip

 

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.