• .net core .net5 asp.net core mvc 与quartz.net 3.3.3 新版本调用方式


    参照了:https://www.cnblogs.com/LaoPaoEr/p/15129899.html

    1.项目Nuget引用Quartz.AspNetCoreQuartz.Extensions.DependencyInjection,3.3.3 及以上版本。

    2.新建一个MyTestJob类:

    using Quartz;
    using System;
    using System.IO;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AspNetCoreQuartz333.QuartzJob
    {
        [DisallowConcurrentExecution]
        public class MyTestJob : IJob
        {
            public MyTestJob()
            {
                //构造函数里可注入
            }
    
            public Task Execute(IJobExecutionContext context)
            {
                return Task.Run(() =>
                {
                    using (StreamWriter sw = new StreamWriter(@"d:/Quartz-NET.txt", true, Encoding.UTF8))
                    {
                        sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff =>") + "1");
                    }
                });
            }
        }
    }

    3.修改Startup-ConfigureServices方法,加入以下内容:

    string thisJob = "TestJob";
    
                string groupName = "gp" + thisJob;
                string jobName = "job" + thisJob;
                string triggerName = "trigger" + thisJob;
    
                //Quartz的工作单元
                services.AddTransient<MyTestJob>();
                //Quartz调度中心
                services.AddQuartz(q =>
                {
                    //用于注入
                    q.UseMicrosoftDependencyInjectionJobFactory();
                    // 基本Quartz调度器、作业和触发器配置
                    var jobKey = new JobKey(jobName, groupName);
                    q.AddJob<MyTestJob>(jobKey, j => j
                        .WithDescription("My  work")
                    );
                    q.AddTrigger(t => t
                        .WithIdentity(triggerName)
                        .ForJob(jobKey)
                        .StartNow()
                        .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(10))//开始秒数 10s
                        .RepeatForever())//持续工作
                        .WithDescription("My  work trigger")
                    );
                });
    
                // ASP.NET核心托管-添加Quartz服务器
                services.AddQuartzServer(options =>
                {
                    // 关闭时,我们希望作业正常完成
                    options.WaitForJobsToComplete = false;
                });

    Startup完整代码:

    using AspNetCoreQuartz333.QuartzJob;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Quartz;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace AspNetCoreQuartz333
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews();
    
                //避免ConfigureServices方法过长,Quartz相关配置提取到了单独方法
                QuartzConfigureServices(services);
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
                app.UseStaticFiles();
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                });
            }
    
            /// <summary>
            /// 避免ConfigureServices方法过长,Quartz相关配置提取到了单独方法
            /// </summary>
            /// <param name="services"></param>
            public void QuartzConfigureServices(IServiceCollection services)
            {
                #region Quartz
    
                string thisJob = "TestJob";
    
                string groupName = "gp" + thisJob;
                string jobName = "job" + thisJob;
                string triggerName = "trigger" + thisJob;
    
                //Quartz的工作单元
                services.AddTransient<MyTestJob>();
                //Quartz调度中心
                services.AddQuartz(q =>
                {
                    //用于注入
                    q.UseMicrosoftDependencyInjectionJobFactory();
                    // 基本Quartz调度器、作业和触发器配置
                    var jobKey = new JobKey(jobName, groupName);
                    q.AddJob<MyTestJob>(jobKey, j => j
                        .WithDescription("My  work")
                    );
                    q.AddTrigger(t => t
                        .WithIdentity(triggerName)
                        .ForJob(jobKey)
                        .StartNow()
                        .WithSimpleSchedule(x => x.WithInterval(TimeSpan.FromSeconds(10))//开始秒数 10s
                        .RepeatForever())//持续工作
                        .WithDescription("My  work trigger")
                    );
                });
    
                // ASP.NET核心托管-添加Quartz服务器
                services.AddQuartzServer(options =>
                {
                    // 关闭时,我们希望作业正常完成
                    options.WaitForJobsToComplete = false;
                });
    
                #endregion
            }
    
    
        }
    }

    END

  • 相关阅读:
    date题解
    pencil题解
    20155326 2017-2018-1 《信息安全系统设计基础》第5周学习总结
    2017-2018-1 20155326 20155320 实验一 开发环境的熟悉
    2017-2018-1 20155326 《信息安全系统设计基础》第四周学习总结及myod改进版的补交
    2017-2018-1 20155326 《信息安全系统设计基础》第三周学习总结
    20155326 2017-2018-1 《信息安全系统设计基础》第2周学习及课堂总结myod
    20155326 2017-2018-1 《信息安全系统设计基础》第1周学习总结
    20155326 《Java程序设计》实验五网络编程与安全实验报告
    20155326 2016-2017-2《Java程序设计》课程总结
  • 原文地址:https://www.cnblogs.com/runliuv/p/15133600.html
Copyright © 2020-2023  润新知