Quartz.NET是一个强大、开源、轻量的作业调度框架,是一个用C#编写的纯.NET库,是一个非常流行的开源Java作业调度框架。是 OpenSymphony 的 Quartz API 的.NET移植,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等
通俗说它的功能是:比如说我想每天晚上2点让程序或网站执行某些代码,或者每隔5秒种我想查看是否有新的任务要处理等。
Quartz主要有三部分组成任务(Job)、触发器(Trigger)和调度器(Schedule)。
var trigger = TriggerBuilder.Create()
.WithSimpleSchedule(x =>
x.WithIntervalInSeconds(2).WithRepeatCount(5))//间隔2秒 执行6次
.UsingJobData("key1", 321)
.WithIdentity("trigger", "group")
.Build();
CronTrigger:Cron表达式包含7个字段,秒 分 时 月内日期 月 周内日期 年(可选)
var trigger = TriggerBuilder.Create()
.WithCronSchedule("0 0 0 1 1 ?")// 每年元旦1月1日 0 点触发
.UsingJobData("key1", 321)
.UsingJobData("key2", "trigger-key2")
.WithIdentity("trigger4", "group14")
.Build();
"0 15 10 * * ? *" 每天上午10:15触发
"0 0-5 14 * * ?" 每天下午2点到下午2:05期间的每1分钟触发
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddMvc();
services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();//注册ISchedulerFactory的实例。
}
using Quartz;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Quartz_api
{
public class MyJob : IJob//创建IJob的实现类,并实现Excute方法。
{
public Task Execute(IJobExecutionContext context)
{
return Task.Run(() =>
{
using (StreamWriter sw = new StreamWriter(@"E:\Dome\OneABPproject\Quartz_api\error.log", true, Encoding.UTF8))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));
}
});
}
}
public class MyJob2 : IJob
{
public Task Execute(IJobExecutionContext context)
{
//
// 当Job中的参数和Trigger中的参数名称一样时,用 context.MergedJobDataMap获取参数时,Trigger中的值会覆盖Job中的值。
//
var jobData = context.JobDetail.JobDataMap;//获取Job中的参数
var triggerData = context.Trigger.JobDataMap;//获取Trigger中的参数
var data = context.MergedJobDataMap;//获取Job和Trigger中合并的参数
var value1 = jobData.GetInt("key1");
var value2 = jobData.GetString("key2");
var dateString = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"); return Task.Run(() =>
{
using (StreamWriter sw = new StreamWriter(@"E:\Dome\OneABPproject\Quartz_api\error.log", true, Encoding.UTF8))
{
sw.WriteLine($"{dateString} value1:{value1} value2:{value2}");
}
});
}
}
[PersistJobDataAfterExecution]//更新JobDetail的JobDataMap的存储副本,以便下一次执行这个任务接收更新的值而不是原始存储的值
public class MyJob3 : IJob
{
public Task Execute(IJobExecutionContext context)
{
var jobData = context.JobDetail.JobDataMap;//获取Job中的参数
var triggerData = context.Trigger.JobDataMap;//获取Trigger中的参数
var data = context.MergedJobDataMap;//获取Job和Trigger中合并的参数
var value1 = jobData.GetInt("key1");
var value2 = jobData.GetString("key2");
var value3 = data.GetString("key2"); //data
var dateString = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
Random random = new Random();
jobData["key1"] = random.Next(1, 20);//这里面给key赋值,下次再进来执行的时候,获取的值为更新的值,而不是原始值
jobData["key2"] = dateString;
return Task.Run(() =>
{
using (StreamWriter sw = new StreamWriter(@"E:\Dome\OneABPproject\Quartz_api\error.log", true, Encoding.UTF8))
{
sw.WriteLine($"{dateString} value1:{value1} value2:{value2} value3:{value3}");
}
//context.Scheduler.DeleteJob(context.JobDetail.Key);
//context.Scheduler.Shutdown();
});
}
}
}
注意:当使用 context.MergedJobDataMap; //获取Job和Trigger中合并的参数
当使用的是MergedJobDataMap获取参数时:Job中的参数和Trigger中的参数名称一样时,Trigger中的值会覆盖Job中的值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Quartz;
namespace Quartz_api.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ISchedulerFactory _schedulerFactory;
private IScheduler _scheduler;
public WeatherForecastController(ISchedulerFactory schedulerFactory)
{
this._schedulerFactory = schedulerFactory;
}
[HttpGet]
public async Task<string[]> Get()
{
//1、通过调度工厂获得调度器
_scheduler = await _schedulerFactory.GetScheduler();
//2、开启调度器
await _scheduler.Start();
//3、创建一个触发器
var trigger1 = TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(2).WithRepeatCount(2))//每两秒执行一次 执行3次
.Build();
//4、创建任务
var job1 = JobBuilder.Create<MyJob>()
.WithIdentity("job1", "group1")
.Build();
//在Trigger中添加参数值
var trigger2 = TriggerBuilder.Create()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(2).RepeatForever())//间隔2秒 一直执行
.UsingJobData("key1", 321) //通过在Trigger中添加参数值
.UsingJobData("key2", "trigger123")
.WithIdentity("trigger2", "group1")
.Build();
//在Job中添加参数值
IJobDetail job2= JobBuilder.Create<MyJob2>()
.UsingJobData("key1", 123)//通过Job添加参数值
.UsingJobData("key2", "job123")
.WithIdentity("job2", "group2")
.Build();
//在Job中添加参数值
IJobDetail job3 = JobBuilder.Create<MyJob3>()
.UsingJobData("key1", 123)//通过Job添加参数值
.UsingJobData("key2", "job123")
.WithIdentity("job3", "group3")
.Build();
//5、将触发器和任务器绑定到调度器中
//await _scheduler.ScheduleJob(job1, trigger1);//MyJob
//await _scheduler.ScheduleJob(job2, trigger1);//MyJob2
await _scheduler.ScheduleJob(job3, trigger2);//MyJob3
return await Task.FromResult(new string[] { "value1", "value2" });
}
}
}
项目运行后显示