您好,登录后才能下订单哦!
这期内容当中小编将会给大家带来有关Quartz怎么在.net core中使用,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
Quartz版本:3.0.7
1.NuGet中下载安装Quartz
2.新建Quartz帮助类
(1)新建QuartzJob类,代码如下:
public class QuartzJob : IJob { public async Task Execute(IJobExecutionContext context) { var jobKey = context.JobDetail.Key;//获取job信息 var triggerKey = context.Trigger.Key;//获取trigger信息 LogHelper.Info($"{DateTime.Now} QuartzJob:==>>自动执行.{jobKey.Name}|{triggerKey.Name}"); await Task.CompletedTask; } }
ps: 上面代码中的jobKey和triggerKey可获取到自己定义的job和trigger的信息,这样做多任务时可区分要执行的是哪个任务. 另LogHelper.Info是自己封装好的日子记录类,可替换为自己的日志类
(2)新建QuartzFactory类,代码如下:
public class QuartzFactory : IJobFactory { private readonly IServiceProvider _serviceProvider; public QuartzFactory(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) { var jobDetail = bundle.JobDetail; var job = (IJob)_serviceProvider.GetService(jobDetail.JobType); return job; } public void ReturnJob(IJob job) { } }
(3)新建QuartzService类,代码如下:
#region 单任务 public static void StartJob<TJob>() where TJob : IJob { var scheduler = new StdSchedulerFactory().GetScheduler().Result; var job = JobBuilder.Create<TJob>() .WithIdentity("job") .Build(); var trigger1 = TriggerBuilder.Create() .WithIdentity("job.trigger") .StartNow() .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(5)).RepeatForever()) .ForJob(job) .Build(); scheduler.AddJob(job, true); scheduler.ScheduleJob(job, trigger1); scheduler.Start(); } #endregion #region 多任务 public static void StartJobs<TJob>() where TJob : IJob { var scheduler = new StdSchedulerFactory().GetScheduler().Result; var job = JobBuilder.Create<TJob>() .WithIdentity("jobs") .Build(); var trigger1 = TriggerBuilder.Create() .WithIdentity("job.trigger1") .StartNow() .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(5)).RepeatForever()) .ForJob(job) .Build(); var trigger2 = TriggerBuilder.Create() .WithIdentity("job.trigger2") .StartNow() .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(11)).RepeatForever()) .ForJob(job) .Build(); var dictionary = new Dictionary<IJobDetail, IReadOnlyCollection<ITrigger>> { {job, new HashSet<ITrigger> {trigger1, trigger2}} }; scheduler.ScheduleJobs(dictionary, true); scheduler.Start(); } #endregion #region 配置 public static void AddQuartz(this IServiceCollection services, params Type[] jobs) { services.AddSingleton<IJobFactory, QuartzFactory>(); services.Add(jobs.Select(jobType => new ServiceDescriptor(jobType, jobType, ServiceLifetime.Singleton))); services.AddSingleton(provider => { var schedulerFactory = new StdSchedulerFactory(); var scheduler = schedulerFactory.GetScheduler().Result; scheduler.JobFactory = provider.GetService<IJobFactory>(); scheduler.Start(); return scheduler; }); } #endregion
3.在Startup.cs中注册代码
public void ConfigureServices(IServiceCollection services) { services.AddQuartz(typeof(QuartzJob)); //........ } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { QuartzService.StartJobs<QuartzJob>(); //........ }
ps: 因AddQuartz方法定义的是Type[]类型,所以可传多个执行类,如:services.AddQuartz(typeof(QuartzJob1), typeof(QuartzJob2)); 不过这样做StartJob()方法就得另处理下,这里就略过
OK,大功告成,这个东西折腾了两天终于尘埃落定了,执行结果如下:
上述就是小编为大家分享的Quartz怎么在.net core中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。