工作中我们经常碰到定时或者固定时间点去做一些事情,然后每天到时间点就会去做这样的事情,如果理解这样的场景,我们就要引入今天我们的主角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. Codechef Prime Distance On Tree

    [传送门] FFT第四题! 暑假的时候只会点分,然后合并是暴力合并的...水过去了... 其实两条路径长度的合并就是卷积的过程嘛,每次统计完路径就自卷积一下. 刚开始卷积固定了值域.T了.然后就不偷懒 ...

  2. 虚拟机-Ubuntu

    1.安装 下载iso镜像文件,在VMware中创建时添加即可 2.安装tools,可以复制文件 参考:https://www.cnblogs.com/justaman/p/10545239.html ...

  3. [LeetCode] 897. Increasing Order Search Tree 递增顺序查找树

    Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...

  4. 谈谈IntersectionObserver懒加载

    概念 IntersectionObserver接口(从属于Intersection Observer API)为开发者提供了一种可以异步监听目标元素与其祖先或视窗(viewport)交叉状态的手段.祖 ...

  5. 国家集训队 Crash 的文明世界(第二类斯特林数+换根dp)

    题意 ​ 题目链接:https://www.luogu.org/problem/P4827 ​ 给定一棵 \(n\) 个节点的树和一个常数 \(k\) ,对于树上的每一个节点 \(i\) ,求出 \( ...

  6. Spring 常犯的十大错误,这坑你踩过吗?

    阅读本文大概需要 9 分钟. 1.错误一:太过关注底层 我们正在解决这个常见错误,是因为 “非我所创” 综合症在软件开发领域很是常见.症状包括经常重写一些常见的代码,很多开发人员都有这种症状. 虽然理 ...

  7. IDEA 最新破解方法,2019 版本已支持(持续更新,更新时间2019-05-14)

    一.安装进入官网下载适合自己系统的版本,下载地址:https://www.jetbrains.com/idea/download/#section=windows ​ 2. 安装中选择适合本机的64位 ...

  8. 微信企业号获取OpenID过程

    define('CorpID', "wx82e2c31215d9a5a7"); define('CorpSecret', ""); //当前管理组 设置-> ...

  9. 轻量级Mysql Sharding中间件——Shark

    Shark简介 前身为Kratos,轻量级Mysql分库分表(Sharding)中间件,丰富的Sharding算法支持(2类4种分片算法),能够方便DBA实现库的水平扩容和降低数据迁移成本.Shark ...

  10. (一)golang--初识go语言

    学习来源:https://www.bilibili.com/video/av35928275/?p=1  尚硅谷的(我学spring.springmvc和mybatis就是他们的课) 使用1.9.2的 ...