• [Scheduled Timer]第六回:定时器作业(TimerJob)


    1.引言

    实际应用中,我们是定义一个作业,定时器然后定时去调用。前面章节都介绍的时间调度、事件过滤(EventFilter)任务方法(MethodCall) 这些都是这个作业零部件,这就是这节要介绍的Job。

    2.Job

    Scheduled Timer的Job是由时间调度器、任务方法,事件过滤器等组成,共同来达到预期的目的,单个不行,好像一个团队,主要代码如下

        /// <summary>
        /// 定时器作业
        /// </summary>
        public class TimerJob
        {
            public IScheduledItem Schedule;
            //是否同步
            public bool SyncronizedEvent = true;
            public IResultFilter Filter;
            public IMethodCall Method;
            //public IJobLog Log;
            public bool Enabled = true;
    
            private delegate void ExecuteHandler(object sender, DateTime EventTime, ExceptionEventHandler Error);
            private ExecuteHandler _ExecuteHandler;
    
            public TimerJob(IScheduledItem schedule, IMethodCall method)
            {
                Schedule = schedule;
                Method = method;
                _ExecuteHandler = new ExecuteHandler(ExecuteInternal);
            }
    
            public DateTime NextRunTime(DateTime time, bool includeStartTime)
            {
                if (!Enabled)
                    return DateTime.MaxValue;
                return Schedule.NextRunTime(time, includeStartTime);
            }
    
            public void Execute(object sender, DateTime begin, DateTime end, ExceptionEventHandler error)
            {
                if (!Enabled)
                    return;
    
                List<DateTime> eventList = new List<DateTime>();
                Schedule.AddEventsInInterval(begin, end, eventList);
    
                if (Filter != null)
                    Filter.FilterResultsInInterval(begin, end, eventList);
    
                foreach (DateTime eventTime in eventList)
                {
                    if (SyncronizedEvent)
                        _ExecuteHandler(sender, eventTime, error);
                    else
                        _ExecuteHandler.BeginInvoke(sender, eventTime, error, null, null);
                }
            }
    
            private void ExecuteInternal(object sender, DateTime eventTime, ExceptionEventHandler error)
            {
                try
                {
                    TimerParameterSetter setter = new TimerParameterSetter(eventTime, sender);
                    Method.Execute(setter);
                }
                catch (Exception ex)
                {
                    if (error != null)
                        try { error(this, new ExceptionEventArgs(eventTime, ex)); }
                        catch { }
                }
            }
        }

    TimerJob的大致工作流程是,首先初始化时间调度器、任务方法、事件过滤器,再在执行作业时,先获取这个时间调度的时间集合,再进行事件过滤,再在有效的事件集合里循环执行任务方法。Scheduled Timer中一个定时器Timer不是只对应一个Job,而是一个Job集合中,Scheduled Timer的Job集合,代码很简单,对一个集合的操作,声明如下

        /// <summary>
        /// 计时器作业管理组
        /// </summary>
        public class TimerJobList
        {
            private List<TimerJob> _List;
    
            public TimerJobList()
            {
                _List = new List<TimerJob>();
            }
    
            public void Add(TimerJob Event)
            {
                _List.Add(Event);
            }
    
            public void Clear()
            {
                _List.Clear();
            }
    
            public DateTime NextRunTime(DateTime time)
            {
                DateTime next = DateTime.MaxValue;
                //从列表中,获得最低的日期时间。
                foreach (TimerJob Job in _List)
                {
                    DateTime Proposed = Job.NextRunTime(time, true);
                    next = (Proposed < next) ? Proposed : next;
                }
                return next;
            }
    
            public TimerJob[] Jobs
            {
                get { return _List.ToArray(); }
            }
        }

    3.总结

    作业比较简单,就是把之前的章节进行组合,主要看Job执行方法Execute,这也是定时器Timer的公共事件Elapsed事件将要调用的方法。

  • 相关阅读:
    Spring bean的实例化
    提交本地代码到github
    ORM框架——SQLAlchemy
    代码发布项目(三)——python操作git、代码发布流程(服务器管理、项目管理)
    代码发布项目(二)——django实现websocket(使用channels)、基于channels实现群聊功能、gojs插件、paramiko模块
    代码发布项目(一)——实现服务端主动给客户端推送消息(websocket)
    索引补充(索引种类,正确使用索引,其他注意事项,慢日志查询)
    mysql索引种类(索引种类和建立索引)
    centos6安装Docker遇到的问题(升级centos6内核)
    Django1.11下载安装xadmin
  • 原文地址:https://www.cnblogs.com/qqlin/p/2694110.html
Copyright © 2020-2023  润新知