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

相关推荐
时光追逐者13 小时前
.NET 9 中 LINQ 新增功能实操
开发语言·开源·c#·.net·.netcore·linq·微软技术
宝桥南山2 天前
.NET 9 - BinaryFormatter移除
microsoft·微软·c#·asp.net·.net·.netcore
PasteSpider3 天前
贴代码框架PasteForm特性介绍之datetime,daterange
前端·html·.netcore·crud
csdn_aspnet4 天前
ASP.NET Core Webapi 返回数据的三种方式
.netcore
技术拾荒者6 天前
Net.Core Mvc 添加 log 日志
c#·asp.net·mvc·.netcore
一包烟电脑面前做一天6 天前
.netcore + postgis 保存地图围栏数据
.netcore·postgis·geometry·polygon
yz-俞祥胜7 天前
杨中科 .Net Core 笔记 DI 依赖注入2
笔记·.netcore
鸠摩智首席音效师7 天前
.NET Core 应用程序如何在 Linux 中创建 Systemd 服务 ?
linux·运维·.netcore
丨我是张先生丨8 天前
Windows VSCode .NET CORE WebAPI Debug配置
.netcore