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

这篇博客介绍了如何使用开源调度框架Quartz.NET来创建和管理定时任务。Quartz.NET提供强大的调度功能,支持分布式和集群,并能与Spring无缝集成。文中详细展示了创建JobDetail、Trigger、Scheduler的过程,并给出了一个简单的控制台应用示例,演示了如何启动调度器,添加任务和触发器,并执行任务。此外,还展示了如何清除任务和触发器。

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

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

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

    quartz调度核心元素:

  1. Scheduler:任务调度器,是实际执行任务调度的控制器。在spring中通过SchedulerFactoryBean封装起来。
  2. Trigger:触发器,用于定义任务调度的时间规则,有SimpleTrigger,CronTrigger,DateIntervalTrigger和NthIncludedDayTrigger,其中CronTrigger用的比较多,本文主要介绍这种方式。CronTrigger在spring中封装在CronTriggerFactoryBean中。
  3. Calendar:它是一些日历特定时间点的集合。一个trigger可以包含多个Calendar,以便排除或包含某些时间点。
  4. JobDetail:用来描述Job实现类及其它相关的静态信息,如Job名字、关联监听器等信息。在spring中有JobDetailFactoryBean和 MethodInvokingJobDetailFactoryBean两种实现,如果任务调度只需要执行某个类的某个方法,就可以通过MethodInvokingJobDetailFactoryBean来调用。
  5. 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");

            // 在这里处理你的任务

             

        }

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值