Using configuration configuration to end using hard-coded Job in quartz.net improves the flexibility of Trigger tasks

Keywords: C# xml encoding

Timer timer is often the first thing that comes to mind when a project encounters a timing task, but it's too weak. In fact, it usually uses a specialized third-party scheduling framework, such as Timer timer.

Quartz, which has powerful functions and flexibility in application, is well known to people I want to use. So this article will talk about how to configure job and trigger through code and configuration files.

 

First: Conventional job, trigger configuration

This routine is also the first thing we learned about Quartz in our initial study, that is, to chain an IJobDetail and ISimpleTrigger through JobBuilder and TriggerBuilder, 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     }

   

As you can see from the above code, when building Job, you use the Hello Job provided as the current scheduled task. The following ISimpleTrigger means to execute Hello Job once a second.

That's all, and then the specific definition of HelloJob, which is very simple, implements an IJob interface.

1     public class HelloJob : IJob
2     {
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 look at the final result. You can see that there is a data output every second.

 

This approach seems perfect, but there is also a disadvantage. Now that the requirements have changed, I need to change WithIntervalInSeconds(1) to WithIntervalInSeconds(2), that is, to execute every second.

It's a headache to change one time to execute every two seconds. You have to face the need to change one time, and you need to compile and release again. Such a wound, I don't think you are willing to? Faced with this demand, it must be

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

 

2: Define job and trigger in quartz_jobs.xml

I've also talked about the disadvantage of hard coding above. Next, let's look at how to configure job and trigger using xml. When we get the quartz framework from nuget, we'll find out about you.

There's an additional xsd file in the solution, but many people probably don't know what the xsd file is for... In fact, it is used to write xml for code prompts. Okay, here I am.

Step by step to show you.

 

1. Download the file from nuget. There's nothing to say. Right-click the mouse on "Reference" and select "Manage NuGet Package" to see the following picture:

 

Then you will see an additional job_scheduling_data_2_0.xsd file.

 

2. Next, create a new quartz_jobs.xml file, then select the XML menu bar in visual studio, and select the "Architecture" menu item.

 

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

 

4. Then you can code freely in xml, and you will find hints about job and trigger. This greatly improves our development.

Efficiency, right.

 

Okay, here's a complete xml case. You can probably see from xml that a job and trigger are defined in the scheme, and a namespace and class name are defined in the job-type node to execute the job.

It's interesting, but it's also the task that schedules eventually need to schedule.

<?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. job's configuration file is basically done, and then the Quartz. Plugin. Xml. XML Scheduling Data Processor Plugin class is configured to handle it.

This XML file specifies the path to quartz_jobs.xml as follows:

 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     }

 

Okay, basically that's the end of the configuration. The last thing to note is to configure quartz_jobs.xml and always copy it to the bin file, as shown in Figure 1:

 

Finally, run to the source code to see what the effect is like, see no, is it very compelling?

 

Well, now is it found that your flexibility has been greatly improved. If you change 1s to 2s, I only need to change <repeat-interval>1000</repeat-interval> in quartz_jobs.xml to <repeat-interval>1000</repeat-interval>?

Is it cool that < repeat-interval > 2000 </repeat-interval > can dynamically configure trigger?

 

Finally, a complete case is attached. ConsoleApplication5.zip

Posted by champoi on Thu, 20 Dec 2018 20:00:05 -0800