C#中Quartz的简单易懂定时任务实现

    xiaoxiao2022-07-12  179

    作为一个优秀的开源调度框架,Quartz 具有以下特点:

    强大的调度功能,例如支持丰富多样的调度方法,可以满足各种常规及特殊需求;灵活的应用方式,例如支持任务和调度的多种组合方式,支持调度数据的多种存储方式;分布式和集群能力,Terracotta 收购后在原来功能基础上作了进一步提升。

          另外,作为 Spring 默认的调度框架,Quartz 很容易与 Spring 集成实现灵活可配置的调度功能。

        quartz调度核心元素:

    Scheduler:任务调度器,是实际执行任务调度的控制器。在spring中通过SchedulerFactoryBean封装起来。Trigger:触发器,用于定义任务调度的时间规则,有SimpleTrigger,CronTrigger,DateIntervalTrigger和NthIncludedDayTrigger,其中CronTrigger用的比较多,本文主要介绍这种方式。CronTrigger在spring中封装在CronTriggerFactoryBean中。Calendar:它是一些日历特定时间点的集合。一个trigger可以包含多个Calendar,以便排除或包含某些时间点。JobDetail:用来描述Job实现类及其它相关的静态信息,如Job名字、关联监听器等信息。在spring中有JobDetailFactoryBean和 MethodInvokingJobDetailFactoryBean两种实现,如果任务调度只需要执行某个类的某个方法,就可以通过MethodInvokingJobDetailFactoryBean来调用。Job:是一个接口,只有一个方法void execute(JobExecutionContext context),开发者实现该接口定义运行任务,JobExecutionContext类提供了调度上下文的各种信息。Job运行时的信息保存在JobDataMap实例中。实现Job接口的任务,默认是无状态的,若要将Job设置成有状态的,在quartz中是给实现的Job添加@DisallowConcurrentExecution注解(以前是实现StatefulJob接口,现在已被Deprecated),在与spring结合中可以在spring配置文件的job detail中配置concurrent参数。

    我这里简单记录使用过程及代码:

    1:首先引用Quartz组件

    2:using Quartz;using Quartz.Impl;

    注:在本地新建一个控制台项目,将以下代码copy过去即可用,只需要重写Execute方法即可。Quartz3.0及以上的版本是采用的异步,3.0以下的版本没有采用异步,使用方法是一样的

    主函数入口文件:

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    using BackgroundTask.job;

    using log4net;

    using Quartz;

    using Quartz.Impl;

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

     

    namespace BackgroundTask

    {

        class Program

        {

            private static readonly ILog _log = LogManager.GetLogger(typeof(Program));

     

            private static readonly string tiggerName = "TestJobTrigger";

            private static readonly string gropName = "TestJobTriggerGrop";

            private static readonly string jobName = "TestJob";

            //从工厂中获取一个调度器实例化

            private static IScheduler scheduler = null;

     

     

            static void Main(string[] args)

            {

                Console.WriteLine("开始任务....");

                _log.Debug("开始任务....");

                Start();

     

            }

     

            private static async void Start()

            {

                //从工厂中获取一个调度器实例化

                scheduler = await StdSchedulerFactory.GetDefaultScheduler();

                await scheduler.Start();

     

     

                //创建一个作业

                IJobDetail job1 = JobBuilder.Create<TestJob>()

                 .WithIdentity(jobName, gropName)

                 .UsingJobData("key","value")// 传递参数 在Execute方法中获取(以什么类型值传入,取值就用相应的类型方法取值)

                 .Build();

     

                // 创建触发器

                ITrigger trigger1 = TriggerBuilder.Create()

                                            .WithIdentity(tiggerName, gropName)

                                            .StartNow()                        //现在开始

                                            .WithSimpleSchedule(x => x         //触发时间,10秒一次。

                                                .WithIntervalInSeconds(10)

                                                .RepeatForever())              //不间断重复执行

                                            .Build();

     

     

                await scheduler.ScheduleJob(job1, trigger1);      //把作业,触发器加入调度器。

     

                Console.ReadKey();

     

                // 清除任务和触发器

                ClearJobTrigger();

            }

     

            /// <summary>

            /// 清除任务和触发器

            /// </summary>

            private static void ClearJobTrigger()

            {

                TriggerKey triggerKey = new TriggerKey(tiggerName, gropName);

                JobKey jobKey = new JobKey(jobName, gropName);

                if (scheduler != null)

                {

                    scheduler.PauseTrigger(triggerKey);

                    scheduler.UnscheduleJob(triggerKey);

                    scheduler.DeleteJob(jobKey);

                    scheduler.Shutdown();// 关闭

                }

     

            }

     

        }

     

    }

      

    实现IJob 接口的任务文件

    ?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    using log4net;

    using Quartz;

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

     

    namespace BackgroundTask.job

    {

        public class TestJob : IJob

        {

            private readonly ILog _log = LogManager.GetLogger(typeof(TestJob));

            /// <summary>

            /// 测试作业

            /// </summary>

            /// <param name="context"></param>

            /// <returns></returns>

            public async Task Execute(IJobExecutionContext context)

            {

            JobDataMap dataMap = context.JobDetail.JobDataMap;         string k = dataMap.GetString("key");//获取参数(可根据传递的类型使用GetInt、GetFloat、GetString.....)

    ?

    1

    2

    3

    4

    5

    6

    7

    8

                _log.Debug("run TestJob debug");

                _log.Error("run TestJob error");

                _log.Info("run TestJob info");

                // 在这里处理你的任务

                 

            }

        }

    }<br><br>

    最新回复(0)