定時器_在.net core3.0 webapi中添加自定義定時器功能

  前言:想在.net framework環境使用自定義定時器的話,參考個人另外一篇文章:http://www.javashuo.com/article/p-gtmbgzui-de.htmlhtml

  想在.net core中使用定時器功能,須要藉助一個服務接口:IHostedService,   繼承並實現對應方法,最後再setup.cs類中添加註冊服務:services.AddHostedService<實現服務類>();   既然要寫計時器的服務器,那麼該實現類就要包含定時器,本篇博客也是藉助System.Timers.Timer類封裝的。web

 

  下面展現具體代碼:服務器

  1-公用基類:多線程

  

public class ModelBase
    {
        protected IServiceProvider Services { get; set; }
        protected IWebHostEnvironment WebHostEnvironment { get; set; }
        /// <summary>
        /// 配置幫助類
        /// </summary>
        protected ConfigHelper ConfigHelper { get; set; }
        /// <summary>
        /// 等同於ASP.NET裏面的WebCache(HttpRuntime.Cache)
        /// </summary>
        protected IMemoryCache MemoryCache { get; set; }

        /// <summary>
        /// 日誌
        /// </summary>
        protected ILogger Logger { get; set; }

        /// <summary>
        /// 受權幫助
        /// </summary>
        protected OAuthHelper OAuthHelper { get; set; }

        /// <summary>
        /// HttpClient幫助工廠
        /// </summary>
        protected IHttpClientFactory HttpClientFactory { get; set; }


        public ModelBase(params object[] @params)
        {
            foreach (var item in @params)
            {
                if (item is IServiceProvider)
                {
                    this.Services = (IServiceProvider)item;
                }
                else if (item is IWebHostEnvironment)
                {
                    this.WebHostEnvironment = (IWebHostEnvironment)item;
                }
                else if (item is ConfigHelper)
                {
                    this.ConfigHelper = (ConfigHelper)item;
                }
                else if (item is IMemoryCache)
                {
                    this.MemoryCache = (IMemoryCache)item;
                }
                else if (item is ILogger)
                {
                    this.Logger = (ILogger)item;
                }
                else if (item is OAuthHelper)
                {
                    this.OAuthHelper = (OAuthHelper)item;
                }
                else if (item is IHttpClientFactory)
                {
                    this.HttpClientFactory = (IHttpClientFactory)item;
                }
                
            }

        }
    }

  

  2-計時器封裝類:框架

  相對於.net framework文章計時器部分的類作了對應優化,更加簡化了:ide

using Microsoft.Extensions.Logging;
using PaymentAccountAPI.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;

namespace PaymentAccountAPI.Helper
{
    /// <summary>
    /// 定時週期幫助類
    /// </summary>
    public class TimeCycleHelp : ModelBase
    {
        public TimeCycleHelp(ILogger<TimeCycleHelp> logger) : base(logger)
        {
            this.Timer = new System.Timers.Timer();
        }

        /// <summary>
        /// 服務專屬計時器
        /// </summary>
        private System.Timers.Timer Timer;

        /// <summary>
        /// 默認計時器時間間隔1秒(提升計時器開始時間準確度)
        /// </summary>
        private double DefaultTimerInterval = 1 * 1000;

        /// <summary>
        /// 設置多個循環週期
        /// </summary>
        private List<TimeCycle> TimeCycleList { get; set; }


        /// <summary>
        /// 更新一個計時器的計時週期
        /// </summary>
        /// <param name="newTimerInterval">新的計時週期</param>
        /// <param name="isFirstStart">是不是首次更新計時器週期</param>
        private void UpdateTimeInterval(double newTimerInterval, bool isFirstStart = false)
        {
            if (this.Timer != null && newTimerInterval > 0)
            {
                this.Timer.Stop();
                if (this.Timer.Interval != newTimerInterval)
                {
                    this.Timer.Interval = newTimerInterval;
                }
                if (isFirstStart)
                {
                    this.Timer.Elapsed += new System.Timers.ElapsedEventHandler(this.ServiceAction);
                }
                this.Timer.AutoReset = true;
                this.Timer.Start();
            }
        }

        /// <summary>
        /// 內部輔助方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ServiceAction(object sender, ElapsedEventArgs e)
        {
            List<TimeCycle> currentTimeCycleList = new List<TimeCycle>(0);

            DateTime now = DateTime.Now;
            DateTime cycleBeginTime;
            DateTime cycleEndTime;
            foreach (TimeCycle timeCycle in this.TimeCycleList)
            {
                cycleBeginTime = Convert.ToDateTime(timeCycle.BeginTime);
                cycleBeginTime = now.Date.AddHours(cycleBeginTime.Hour).AddMinutes(cycleBeginTime.Minute).AddSeconds(cycleBeginTime.Second);
                cycleEndTime = Convert.ToDateTime(timeCycle.EndTime);
                cycleEndTime = now.Date.AddHours(cycleEndTime.Hour).AddMinutes(cycleEndTime.Minute).AddSeconds(cycleEndTime.Second);
                if (cycleEndTime < cycleBeginTime)
                {
                    cycleEndTime = cycleEndTime.AddDays(1);
                }

                if (now >= cycleBeginTime && now <= cycleEndTime)
                {
                    //有最大執行次數限制或者沒有限制
                    if (timeCycle.ActionExecutionTimes < timeCycle.MaxActionTimes || timeCycle.MaxActionTimes == 0)
                    {
                        TimeSpan timeSpan = now - cycleBeginTime;
                        bool isCanAction = (int)timeSpan.TotalSeconds % timeCycle.ActionSeconds == 0 ? true : false;
                        if (isCanAction)
                        {
                            timeCycle.ActionExecutionTimes++;
                            currentTimeCycleList.Add(timeCycle);
                        }
                    }
                }
                else
                {
                    //不在計時週期內,已執行次數清零
                    timeCycle.ActionExecutionTimes = 0;
                }
            }
            //找到當前循環週期後,執行週期內動做
            if (currentTimeCycleList.Count > 0)
            {
                currentTimeCycleList.ForEach(item =>
                {
                    //使用多線程執行任務,讓代碼快速執行
                    Task.Run(() => item.Action());
                });
            }
        }

        /// <summary>
        /// 開啓計時器
        /// </summary>
        /// <param name="timeCycleArray"></param>
        public void Start(params TimeCycle[] timeCycleArray)
        {
            if (timeCycleArray != null && timeCycleArray.Length > 0)
            {
                if (this.TimeCycleList == null)
                {
                    this.TimeCycleList = new List<TimeCycle>(100);
                }
                this.TimeCycleList = timeCycleArray.ToList();

                //設置首次計時器週期(首次動做執行,是在計時器啓動後在設置的時間間隔後作出的動做)
                this.UpdateTimeInterval(this.DefaultTimerInterval, true);
            }
        }

        /// <summary>
        /// 結束計時器
        /// </summary>
        public void Stop()
        {
            this.Timer.Stop();
        }

    }

    /// <summary>
    /// 計時週期類
    /// </summary>
    public class TimeCycle
    {
        /// <summary>
        /// 惟一標識
        /// </summary>
        public int ID { get; set; }
        /// <summary>
        /// 開始時間(偏差1秒=取決於計時器默認時間間隔)
        /// </summary>
        public string BeginTime { get; set; }
        /// <summary>
        /// 結束時間
        /// </summary>
        public string EndTime { get; set; }
        /// <summary>
        /// 最大執行次數
        /// </summary>
        public int MaxActionTimes { get; set; }
        /// <summary>
        /// 計時週期內執行的動做(動做會在到達開始時間後的)
        /// </summary>
        public Action Action { get; set; }
        /// <summary>
        /// 動做執行時間間隔(秒)
        /// </summary>
        public int ActionSeconds { get; set; }
        /// <summary>
        /// 方法執行次數
        /// </summary>
        internal int ActionExecutionTimes { get; set; }

        public TimeCycle(int id, Action action, int actionSeconds) : this(id, "00:00:00", action, actionSeconds)
        {
        }

        public TimeCycle(int id, string beginTime, Action action, int actionSeconds) : this(id, beginTime, action, actionSeconds, 0)
        {
        }

        public TimeCycle(int id, string beginTime, Action action, int actionSeconds, int maxActionTimes) : this(id, beginTime, "23:59:59", action, actionSeconds, maxActionTimes)
        {
        }

        /// <summary>
        /// 基本構造器
        /// </summary>
        /// <param name="id">惟一標識</param>
        /// <param name="beginTime">開始時間</param>
        /// <param name="endTime">結束時間</param>
        /// <param name="action">要執行的任務</param>
        /// <param name="actionSeconds">任務執行時間間隔</param>
        /// <param name="maxActionTimes">最大執行次數</param>
        public TimeCycle(int id, string beginTime, string endTime, Action action, int actionSeconds, int maxActionTimes)
        {
            this.ID = id;
            this.BeginTime = beginTime;
            this.EndTime = endTime;
            this.Action = action;
            this.ActionSeconds = actionSeconds;
            this.MaxActionTimes = maxActionTimes;
        }
    }
}

 

  3-webAPI服務封裝類:post

  

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using PaymentAccountAPI.Common;
using PaymentAccountAPI.Helper;
using PaymentAccountAPI.PaymentAccountHelper;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace PaymentAccountAPI.Hangfire
{
    public class SyncToCMSService : ModelBase, IHostedService
    {private TimeCycleHelp _TimeCycleHelp { get; set; }

        public SyncToCMSService(ConfigHelper configHelper,
                                TimeCycleHelp timeCycleHelp, ILogger<SyncToCMSService> logger) : base(logger, configHelper)
        {this._TimeCycleHelp = timeCycleHelp;
        }

        public void SyncData()
        {
           //...須要執行的任務代碼

            this.Logger.LogInformation($"定時任務:完成{currentDate.ToShortDateString()}的交易記錄同步!");
        }



        public Task StartAsync(CancellationToken cancellationToken)
        {
            this.Logger.LogInformation($"定時任務:同步服務已啓動...");

            //正式代碼
            string syncBeginTime = this.ConfigHelper.GetAppSettingValue("SyncCMSBeginTime");
            string syncEndTime = this.ConfigHelper.GetAppSettingValue("SyncCMSEndTime");
            this._TimeCycleHelp.Start(new TimeCycle(999, syncBeginTime, syncEndTime, this.SyncData, 3600, 2));

            //測試代碼
            //this._TimeCycleHelp.Start(new TimeCycle(999, "12:00:00", () =>
            //{
            //    this.Logger.LogInformation("test123");
            //}, 10, 3));

            return Task.CompletedTask;
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            this.Logger.LogInformation($"同步服務已中止...");
            this._TimeCycleHelp.Stop();

            return Task.CompletedTask;
        }

    }
}

 

  4-在startup.cs添加註冊服務:測試

  

            #region 添加定時任務

            services.AddSingleton<TimeCycleHelp>();

            services.AddHostedService<SyncToCMSService>();

            #endregion

 

 

  最後感謝一篇道友的文章:.Net Core 簡單定時任務框架封裝優化

相關文章
相關標籤/搜索