Quartz实现定时调用接口(.net core2.0)

1.nuget安装Quartz.AspNetCore

2.startup的ConfigureServices方法中增加注入

复制代码
services.AddQuartz(q =>
            {
                q.UseMicrosoftDependencyInjectionJobFactory();
                //是否开启定时补传打卡记录
                if (Common.Const.IsOpenPostRecordInfosSyn=="1")
                {
                    var jobKey = new JobKey("PostRecordInfosJob");
                    q.AddJob<App_Code.QuartzJob.PostRecordInfosJob>(opts => opts.WithIdentity(jobKey));

                    q.AddTrigger(opts => opts
                        .ForJob(jobKey)
                        .WithIdentity("PostRecordInfosJob-trigger")
                        //每分钟
                        .WithCronSchedule("0 * * * * ?")
                    );
                }
                //是否开启定时同步部门、员工信息
                if (Common.Const.IsOpenBaseInfoSyn == "1")
                {
                    var jobKey = new JobKey("BaseInfoSynJob");
                    q.AddJob<App_Code.QuartzJob.BaseInfoSynJob>(opts => opts.WithIdentity(jobKey));

                    q.AddTrigger(opts => opts
                        .ForJob(jobKey)
                        .WithIdentity("BaseInfoSyn-trigger")
                        //每小时
                        .WithCronSchedule("0 0 * * * ?")
                    );
                }
                //是否开启定时同步设备状态
                if (Common.Const.IsOpenDeviceStateSyn == "1")
                {
                    var jobKey = new JobKey("DeviceStateSynJob");
                    q.AddJob<App_Code.QuartzJob.DeviceStateSynJob>(opts => opts.WithIdentity(jobKey));

                    q.AddTrigger(opts => opts
                        .ForJob(jobKey)
                        .WithIdentity("DeviceStateSynJob-trigger")
                        //每分钟
                        .WithCronSchedule("0 * * * * ?")
                    );
                }
            });

            // ASP.NET Core hosting
            services.AddQuartzServer(options =>
            {
                // when shutting down we want jobs to complete gracefully
                options.WaitForJobsToComplete = true;                
            });

3.实现job接口

复制代码
 [DisallowConcurrentExecution]
    public class PostRecordInfosJob : IJob
    {
        private readonly IConfiguration _config;
        private readonly IHttpClientFactory _factory;
        private readonly IFreeSql _freeSql; 
        public PostRecordInfosJob(IConfiguration config,
            IHttpClientFactory factory, IFreeSql freeSql)
        {
            _config = config;
            _factory = factory;
            _freeSql = freeSql;
        }
        /// <summary>
        /// 补传打卡记录(补传记录每天12点进行一次)
        /// </summary>
        //对于耗时任务,需要上一次执行完成后,才执行下次任务,覆盖之前设置的执行周期 
        public async Task Execute(IJobExecutionContext context)
        {
            try
            {
                postRecordInfos();
            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(Common.LogHelper.GetMethodInfo(), ex.ToString(), XHEnum.LogType.异常Error, ex);
            }
        }
    }

部署到服务器是需要注意引入这四个dll

System.Configuration.ConfigurationManager.dll

相关推荐
睡前要喝豆奶粉2 天前
.NET Core Web API开发需引入的三个基本依赖配置说明
oracle·c#·.netcore
睡前要喝豆奶粉2 天前
.NET Core Web API中数据库相关配置
数据库·c#·.netcore
Archy_Wang_12 天前
Hangfire 入门与实战:在 .NET Core 中实现可靠后台任务处理
c#·.netcore
睡前要喝豆奶粉3 天前
在.NET Core Web Api中使用redis
redis·c#·.netcore
睡前要喝豆奶粉4 天前
多表分页联查——EF Core方式和Dapper方式
c#·.netcore
csdn_aspnet4 天前
.NETCore、.NET 7 和 RabbitMQ 的发布-订阅模式
rabbitmq·.netcore·.net7.
爱吃香蕉的阿豪4 天前
深入理解 .NET Core 中的 IServiceScopeFactory:用法、场景与静态类依赖注入
.netcore
sky-stars5 天前
.NET 泛型编程(泛型类、泛型方法、泛型接口、泛型委托、泛型约束)
c#·.net·.netcore
The Sheep 20235 天前
.NetCoreMVC 开发网页使用sass
.netcore·sass
宝桥南山6 天前
.NET10 - 尝试一下Blazor Web Assembly Standalone App的fingerprint新特性
microsoft·微软·c#·asp.net·.net·.netcore