工作中我们经常碰到定时或者固定时间点去做一些事情,然后每天到时间点就会去做这样的事情,如果理解这样的场景,我们就要引入今天我们的主角Quartz,其实这个跟数据库的作业类似,但是不仅仅局限于数据库。

一: quartZ引入&三大核心对象简介

1:在项目中打开Nuget管理,然后搜索QuartZ,现在最新的版本是3.0.7,需要在Framework4.5.2上面使用。

2:quartZ的三大核心对象

A:IScheduler:单元/实例,在这里去完成定时任务的配置,只有单元启动,里面的作业才能正常运行

B:IJob:任务,定时执行动作就是Job

C:ITrigger:定时策略(设置执行的频率或者执行方式)

二:三大核心对象的初始化以及使用如下:

 #region scheduler
Console.WriteLine("初始化scheduler......");
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
//scheduler.ListenerManager.AddSchedulerListener(new CustomSchedulerListener());
//scheduler.ListenerManager.AddTriggerListener(new CustomTriggerListener());
//scheduler.ListenerManager.AddJobListener(new CustomJobListener());
await scheduler.Start();
#endregion //IJob ITrigger
{
//创建作业
IJobDetail jobDetail = JobBuilder.Create<TestJob>()
.WithIdentity("testjob", "group1")
.WithDescription("This is TestJob")
.Build(); //IJobDetail jobDetail = JobBuilder.Create<TestStatefulJob>()
// .WithIdentity("testjob", "group1")
// .WithDescription("This is TestJob")
// .Build(); jobDetail.JobDataMap.Add("student1", "Milor");
jobDetail.JobDataMap.Add("student2", "心如迷醉");
jobDetail.JobDataMap.Add("student3", "宇洋");
jobDetail.JobDataMap.Add("Year", DateTime.Now.Year); //ITrigger trigger = TriggerBuilder.Create()
// .WithIdentity("trigger1", "group1")
// .StartNow()
// .WithSimpleSchedule(x => x
// .WithIntervalInSeconds(10)
// .WithRepeatCount(10)
// .RepeatForever())
// .WithDescription("This is testjob's Trigger")
// .Build(); //创建时间策略
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("testtrigger1", "group1")
.StartAt(new DateTimeOffset(DateTime.Now.AddSeconds()))
//.StartNow()//StartAt
.WithCronSchedule("5/10 * * * * ?")//每隔一分钟
//"10,20,30,40,50,0 * * * * ?"
.WithDescription("This is testjob's Trigger")
.Build(); trigger.JobDataMap.Add("student4", "Ray");
trigger.JobDataMap.Add("student5", "心欲无痕");
trigger.JobDataMap.Add("student6", "风在飘动");
trigger.JobDataMap.Add("Year", DateTime.Now.Year + ); await scheduler.ScheduleJob(jobDetail, trigger);
Console.WriteLine("scheduler作业添加完成......");
}
  public class TestJob : IJob
{
public TestJob()
{
Console.WriteLine("This is TestJob的构造。。。");
} public async Task Execute(IJobExecutionContext context)
{
await Task.Run(() =>
{
Console.WriteLine();
Console.WriteLine("*****************************");
{
JobDataMap dataMap = context.JobDetail.JobDataMap;
Console.WriteLine(dataMap.Get("student1"));
Console.WriteLine(dataMap.Get("student2"));
Console.WriteLine(dataMap.Get("student3"));
Console.WriteLine(dataMap.GetInt("Year"));
}
Console.WriteLine($"This is {Thread.CurrentThread.ManagedThreadId} {DateTime.Now}");
//可以换成去数据库查询,可以做啥啥啥
//但是很多情况下,我们也是需要参数的
{
JobDataMap dataMap = context.Trigger.JobDataMap;
Console.WriteLine(dataMap.Get("student4"));
Console.WriteLine(dataMap.Get("student5"));
Console.WriteLine(dataMap.Get("student6"));
Console.WriteLine(dataMap.GetInt("Year"));
}
{
Console.WriteLine("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
JobDataMap dataMap = context.MergedJobDataMap;
Console.WriteLine(dataMap.Get("student1"));
Console.WriteLine(dataMap.Get("student2"));
Console.WriteLine(dataMap.Get("student3"));
Console.WriteLine(dataMap.Get("student4"));
Console.WriteLine(dataMap.Get("student5"));
Console.WriteLine(dataMap.Get("student6"));
Console.WriteLine(dataMap.GetInt("Year"));
}
Console.WriteLine("*****************************");
Console.WriteLine();
});
}
}

三:任务或者定时策略传参

1:通过定时任务进行传参如下:

然后接收通过:

2:定时策略进行传参:

接收如下:

注意以上两种传参也可以统一通过下面的方式来接收参数,但是如果key相同,则会进行覆盖掉

四:常用的Trigggr

1:SimpleTrigger:从什么时间开始,间隔多久执行重复操作,可以限制最大次数,如下:

  ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds()
.WithRepeatCount()
.RepeatForever()) //从什么时间开始,间隔多久执行重复操作,可以限制最大次数
.WithDescription("This is testjob's Trigger")
.Build();

2:Cron:表达式的方式,可以灵活订制时间规则(具体详情咨询

 ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("testtrigger1", "group1")
.StartAt(new DateTimeOffset(DateTime.Now.AddSeconds()))
//.StartNow()//StartAt
.WithCronSchedule("5/10 * * * * ?")//每隔一分钟 //"10,20,30,40,50,0 * * * * ?"
.WithDescription("This is testjob's Trigger")
.Build();

五:Listener框架的各个环节--事件能做的监听

如果我们自己写定时任务的时候,因为是定时或者周期去做一些事情,所以有时候有问题或者出现了什么故障,我们只能通过我们自己写一下具体的日志,但是Quartz里面的Listener里面内置封装了一些监听接口,分别为:ISchedulerListener,ITriggerListener,IJobListener,里面有一些方法可以让我们去做一下其它的事情,我们下面分别实现了三个接口,代码如下:

1:jobListerer

 public class CustomJobListener : IJobListener
{
public string Name => "CustomJobListener"; public async Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(()=> {
Console.WriteLine($"CustomJobListener JobExecutionVetoed {context.JobDetail.Description}");
});
} public async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(() => {
Console.WriteLine($"CustomJobListener JobToBeExecuted {context.JobDetail.Description}");
});
} public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(() => {
Console.WriteLine($"CustomJobListener JobWasExecuted {context.JobDetail.Description}");
});
}
}

2:ITriggerListener

 public class CustomTriggerListener : ITriggerListener
{
public string Name => "CustomTriggerListener"; public async Task TriggerComplete(ITrigger trigger, IJobExecutionContext context, SchedulerInstruction triggerInstructionCode, CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(() =>
{
Console.WriteLine($"CustomTriggerListener TriggerComplete {trigger.Description}");
});
} public async Task TriggerFired(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(() =>
{
Console.WriteLine($"CustomTriggerListener TriggerFired {trigger.Description}");
});
} public async Task TriggerMisfired(ITrigger trigger, CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(() =>
{
Console.WriteLine($"CustomTriggerListener TriggerMisfired {trigger.Description}");
});
} /// <summary>
/// 要不要放弃job
/// </summary>
/// <param name="trigger"></param>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<bool> VetoJobExecution(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(() =>
{
Console.WriteLine($"CustomTriggerListener TriggerMisfired {trigger.Description}");
});
return false;//false才能继续执行
}
}

3:ISchedulerListener

 public class CustomSchedulerListener : ISchedulerListener
{
public async Task JobAdded(IJobDetail jobDetail, CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(() =>
{
Console.WriteLine($"This is {nameof(CustomSchedulerListener)} JobAdded {jobDetail.Description}");
});
} public Task JobDeleted(JobKey jobKey, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task JobInterrupted(JobKey jobKey, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task JobPaused(JobKey jobKey, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task JobResumed(JobKey jobKey, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task JobScheduled(ITrigger trigger, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task JobsPaused(string jobGroup, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task JobsResumed(string jobGroup, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task JobUnscheduled(TriggerKey triggerKey, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task SchedulerError(string msg, SchedulerException cause, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task SchedulerInStandbyMode(CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task SchedulerShutdown(CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task SchedulerShuttingdown(CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public async Task SchedulerStarted(CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(() =>
{
Console.WriteLine($"This is {nameof(CustomSchedulerListener)} SchedulerStarted ");
});
} public async Task SchedulerStarting(CancellationToken cancellationToken = default(CancellationToken))
{
await Task.Run(() =>
{
Console.WriteLine($"This is {nameof(CustomSchedulerListener)} SchedulerStarting ");
});
} public Task SchedulingDataCleared(CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task TriggerFinalized(ITrigger trigger, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task TriggerPaused(TriggerKey triggerKey, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task TriggerResumed(TriggerKey triggerKey, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task TriggersPaused(string triggerGroup, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
} public Task TriggersResumed(string triggerGroup, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
}
}

然后通过下面代码来融合到实例中:

  StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
scheduler.ListenerManager.AddSchedulerListener(new CustomSchedulerListener());
scheduler.ListenerManager.AddTriggerListener(new CustomTriggerListener());
scheduler.ListenerManager.AddJobListener(new CustomJobListener());
await scheduler.Start();

这样就完美的结合在一起了,然后实例,任务,策略每次发生的动作,都会以日志的形式输出来,当然可以记录任何形式的日志。

六:LogProvider可以展示框架运行的一些信息

Quartz.Logging内置了一些记录日志的类,然后需要记录日志的话,可以直接拿来使用,不用自己再去引用配置等记录日志了,下面是自己试着写了一个:

 public class CustomConsoleLogProvider : ILogProvider
{
public Logger GetLogger(string name)
{
return new Logger((level, func, exception, parameters) =>
{
if (level >= LogLevel.Info && func != null)
{
Console.WriteLine($"[{ DateTime.Now.ToLongTimeString()}] [{ level}] { func()} {string.Join(";", parameters.Select(p => p == null ? " " : p.ToString()))} 自定义日志{name}");
}
return true;
});
}
public IDisposable OpenNestedContext(string message)
{
throw new NotImplementedException();
} public IDisposable OpenMappedContext(string key, string value)
{
throw new NotImplementedException();
}
}

然后在DispatcherManager中的init方法中

这样即实现了log的日志

如果想要测试,可以通过:

  static void Main(string[] args)
{
try
{
Console.WriteLine("QuartZ.Net定时调度");
DispatcherManager.Init().GetAwaiter().GetResult();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}

定时调度之Quartz的更多相关文章

  1. 定时调度框架Quartz随笔

    最近项目中的定时批处理用到了quartz定时任务,在此记录下quartz的配置吧,一个小demo仅供参考,也方便自己今后复习! 下面直接来步骤吧! 一.首先,要搭起能让quartz正常运行的环境,至少 ...

  2. 定时调度任务quartz

    依赖 <!-- quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> < ...

  3. Spring中实现定时调度

    1,   内容简介 所谓的定时调度,是指在无人值守的时候系统可以在某一时刻执行某些特定的功能采用的一种机制,对于传统的开发而言,定时调度的操作分为两种形式: 定时触发:到某一时间点上执行某些处理操作: ...

  4. Quartz定时调度框架

    Quartz定时调度框架CronTrigger时间配置格式说明 CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年 ...

  5. Spring Quartz定时调度任务配置

    applicationContext-quartz.xml定时调度任务启动代码: <?xml version="1.0" encoding="UTF-8" ...

  6. java 多线程——quartz 定时调度的例子

    java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...

  7. springmvc+quartz简单实现定时调度

    一.简介:Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Quartz可以用来创建简单或为运行十 ...

  8. Spring整合quartz框架实现任务定时调度

    1.  首先需要引入需要的jar包,如上图所示. 2. 编写需要定时调度的测试类: package com.jp.task; import java.util.Date; public class T ...

  9. 定时调度系列之Quartz.Net详解

    一. 背景 我们在日常开发中,可能你会遇到这样的需求:"每个月的3号给用户发信息,提醒用户XXX "."每天的0点需要统计前一天的考勤记录"."每个月 ...

随机推荐

  1. 图学Kubernetes

    所有图片来自:Kubernetes Patterns: Reusable Elements for Designing Cloud-Native Applications 本文图片摘要曾经在某大厂内网 ...

  2. leetcode2. 两数相加

    使用迭代的方式 class Solution{ public: ListNode *addTwoNumbers(ListNode* l1,ListNode *l2) { ListNode *res=) ...

  3. 在centos8搭建dhcp服务

    1.用这两个命令就可以安装成功 yum - y install dhcp yum -y install dhcp-server.x86_64 2.配置文件在 /etc/dhcp/dhcpd.conf ...

  4. Codeforces Round #609 (Div. 2) 题解

    Equation Modulo Equality Long Beautiful Integer Domino for Young K Integers Equation \[ Time Limit: ...

  5. Zabbix设置邮件报警

    邮件报警 概述当监控项收集了数据后,触发器会根据异常状态触发报警.根据一些报警机制,它也会通知我们一些重要的事件,而不需要我们直接在Zabbix前端进行查看.这就是通知(Notifications)的 ...

  6. Codeforces Round #576 (Div. 1)

    Preface 闲来无事打打CF,就近找了场Div1打打 这场感觉偏简单,比赛时艹穿的人都不少,也没有3000+的题 两三个小时就搞完了吧(F用随机水过去了) A. MP3 题意不好理解,没用翻译看了 ...

  7. MySQL实战45讲学习笔记:第九讲

    一.今日内容概要 今天的正文开始前,我要特意感谢一下评论区几位留下高质量留言的同学.用户名是 @某.人 的同学,对文章的知识点做了梳理,然后提了关于事务可见性的问题,就是先启动但是后提交的事务,对数据 ...

  8. [LeetCode] 70. Climbing Stairs 爬楼梯问题

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  9. Redis快速入门及使用

    概述 redis是一种支持分布式的nosql数据库,他的数据是保存在内存中,同时redis可以定时把内存数据同步到磁盘,即可以将数据持久化,并且他比memcached支持更多的数据结构(string, ...

  10. c#中怎样取得某坐标点的颜色

    // x,y 分别为x轴,y轴坐标 返回System.Drawing.Color 可以直接显示 public System.Drawing.Color GetPixelColor(int x, int ...