Quartz怎么在.netcore中使用-创新互联
这期内容当中小编将会给大家带来有关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() where TJob : IJob { var scheduler = new StdSchedulerFactory().GetScheduler().Result; var job = JobBuilder.Create () .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 () where TJob : IJob { var scheduler = new StdSchedulerFactory().GetScheduler().Result; var job = JobBuilder.Create () .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 > { {job, new HashSet {trigger1, trigger2}} }; scheduler.ScheduleJobs(dictionary, true); scheduler.Start(); } #endregion #region 配置 public static void AddQuartz(this IServiceCollection services, params Type[] jobs) { services.AddSingleton (); 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 (); 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(); //........ }
ps: 因AddQuartz方法定义的是Type[]类型,所以可传多个执行类,如:services.AddQuartz(typeof(QuartzJob1), typeof(QuartzJob2)); 不过这样做StartJob()方法就得另处理下,这里就略过
OK,大功告成,这个东西折腾了两天终于尘埃落定了,执行结果如下:
上述就是小编为大家分享的Quartz怎么在.net core中使用了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
标题名称:Quartz怎么在.netcore中使用-创新互联
当前路径:http://pwwzsj.com/article/csjjid.html