zoukankan      html  css  js  c++  java
  • .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

  • 相关阅读:
    Nginx的proxy_cache缓存
    linux服务器优化
    LVS+keepalived负载均衡实战
    bash history(history命令)
    APACHE默认模块功能说明
    MySQL配置文件例子翻译
    Microsoft JET Database Engine (0x80004005) 未指定的错误的完美解决[转贴]
    entity framework 新增 修改 删除 查询
    Flash Builder 找不到所需的 Adobe Flash Player 调试器版本
    sql server 2008 远程连接
  • 原文地址:https://www.cnblogs.com/runliuv/p/15133600.html
Copyright © 2011-2022 走看看