Quartz.Net Use Tutorial

During the development of a project, you will inevitably encounter tasks that require background processing, such as periodic email notifications, time-consuming data processing for background processing, and so on, when you need Quartz.Net.

Quartz.Net is pure, is a.Net assembly, and is the C# implementation of Quartz, a very popular Java job scheduling system.

Quartz.Net is a fully functional task scheduling system that can be applied from small to large enterprise systems.Full functionality now supports both simple timers and Cron expressions on the diversity of triggers; repetitive job tasks and calendars with specified exceptions; and tasks can be diverse as long as the IJob interface is inherited.

For small applications, Quartz.Net can be integrated into your system. For enterprise systems, it provides Routing support, Group ing to organize and manage tasks, and persistence, plug-in capabilities. load balancing And failover to meet the needs of different application scenarios.

We are using the latest version 2.4.0 for demonstration.(Note: Higher versions are not suitable for the current tutorial) Create a Job class after adding references

 

 

 

 

 1        static void Main(string[] args)
 2         {
 3             //1.First create a job dispatch pool
 4             ISchedulerFactory schedf = new StdSchedulerFactory();
 5             //2.Instantiate Scheduler Factory
 6             ISchedulerFactory schedulefactory = new StdSchedulerFactory();
 7             //3.Instantiate Scheduler
 8             IScheduler scheduler = schedulefactory.GetScheduler();
 9 
10             //4.Create a job
11             IJobDetail job1 = JobBuilder.Create<Class1>()
12                 .WithIdentity("demojob1", "groupa")
13                 .Build();
14 
15             //5.1:How many seconds does the first method execute directly
16             //ITrigger trigger1 = TriggerBuilder.Create()//Create a trigger
17             //    .WithIdentity("demotrigger1", "groupa")
18             //    .StartNow()
19             //    .WithSimpleSchedule(b => b.WithIntervalInSeconds(5)//5 Execute once per second
20             //    .RepeatForever())//Infinite Loop Execution
21             //    .Build();
22 
23             //5.2 Recommendation: Second use cron Expression
24             //Online Generation cron Expression: http://cron.qqe2.com/ 
25             string corn = "*/10 * * * * ?";
26             ITrigger trigger1 = TriggerBuilder.Create()
27                .WithIdentity("demotrigger1", "groupa")
28               .WithCronSchedule(corn)//Execute once an hour
29               .Build();
30 
31             //6.Add a parameter (key-value pair) and ignore this step if no parameter is required
32             //Get the parameters you passed in the method: string Name = context.Trigger.JobDataMap.GetString("Name");
33             trigger1.JobDataMap.Add("Name", "xuande");
34 
35             //7.Add Jobs, Triggers to Scheduler
36             scheduler.ScheduleJob(job1, trigger1);
37             //8.Start running
38             scheduler.Start();
39         }

That's the simplest use case of Quartz

2. Here's a little expansion of my own

1             Type type = Type.GetType("test1.TestCase");//Get Classes ""Is the namespace in.Class name
2             Object obj = Activator.CreateInstance(type);//Create here
3             //4.Create a job
4             IJobDetail job2 = JobBuilder.Create(obj.GetType())
5                 .WithIdentity("demojob1", "groupa")
6                 .Build();
7   //Dynamically add timed tasks for existing methods

 

1             //Suitable for dynamically adding methods to be executed on time without changing the code(File physical address is preceded by file physical address (virtual address can be converted to physical address using mapping), followed by namespace plus class name)
2             object obj = Assembly.LoadFile(@"D:\Normal File Bag\test\ClassLibrary2\bin\Debug\ClassLibrary2.dll").CreateInstance("ClassLibrary2.Class2");
3             //4.Create a job
4             IJobDetail job2 = JobBuilder.Create(obj.GetType())
5                 .WithIdentity("demojob1", "groupa")
6                 .Build();

 

3.Quartz.NET is deployed on IIS to prevent passive dynamic recycling (I personally recommend not using an IIs server)

 

 

Tags: C# IIS Java

Posted on Wed, 25 Dec 2019 22:46:43 -0500 by Obsession