• Quartz任务管理


      public static class SchedulerHelper
        {
            private static List<SchedulerInfo> jobs;
            private static IScheduler sched;
    
            public static void InitalScheduler()
            {
    
                var sched = GetScheduler();
    
    
                var list = GetJobs();
    
    
                foreach (var s in list)
                {
    
                    sched.ScheduleJob(s.Job, s.Trigger);
                }
    
                sched.Start();
            }
    
            public static void Restart()
            {
    
                var sched = GetScheduler();
    
                sched.Start();
    
            }
    
            public static List<SchedulerInfo> GetJobs()
            {
                if (jobs == null)
                {
                    jobs = new List<SchedulerInfo>();
    
                    //从数据库读定时任务信息
    
                    TimerService ts = new TimerService();
                    var timers = ts.GetAllTimers();
                    foreach (var t in timers)
                    {
                        AddJob(t);
    
                    }
                }
    
    
                return jobs;
            }
    
            public static IScheduler GetScheduler()
            {
                if (sched == null)
                {
    
                    ISchedulerFactory sf = new StdSchedulerFactory();
                    sched = sf.GetScheduler();
                }
                return sched;
            }
    
            public static void AddJob(Timer t)
            {
                IJobDetail job;
                switch (t.Action)
                {
                    case TimerAction.MeterRecord:
                        job = JobBuilder.Create<MeterJob>().WithIdentity(Guid.NewGuid().ToString("n"), "group").Build(); break;
                    case TimerAction.MultiPowerOn:
                        job = JobBuilder.Create<PowerOnJob>().UsingJobData("lampIds", t.Parameter).WithIdentity(Guid.NewGuid().ToString("n"), "group").Build(); break;
                    case TimerAction.MultiPowerOff:
                        job = JobBuilder.Create<PowerOffJob>().UsingJobData("lampIds", t.Parameter).WithIdentity(Guid.NewGuid().ToString("n"), "group").Build(); break;
                    default:
                        job = JobBuilder.Create<OtherJob>().WithIdentity(Guid.NewGuid().ToString("n"), "group").Build(); break;
    
                }
                ITrigger trigger = TriggerBuilder.Create()
                                       .WithIdentity(Guid.NewGuid().ToString("n"), "group").StartNow()
                                       .WithCronSchedule("0/30 * * * * ?")
                                       .Build();
                if (job != null)
                {
                    jobs.Add(new SchedulerInfo() { Job = job, Trigger = trigger, TimerId = t.TimerId });
                    Restart();
                }
    
            }
    
            public static void DelJob(Timer t)
            {
                var sched = GetScheduler();
                var list = GetJobs();
                SchedulerInfo si = list.Where(a => a.TimerId == t.TimerId).FirstOrDefault();
                JobKey key = si.Job.Key;
                sched.DeleteJob(si.Job.Key);
    
            }
        }
    
        class SchedulerInfo
        {
            public IJobDetail Job { get; set; }
            public ITrigger Trigger { get; set; }
            public int TimerId { get; set; }
        }
    
        public class PowerOnJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
                string lampIds = context.JobDetail.JobDataMap.Values.FirstOrDefault().ToString();
    
                List<int> ids = lampIds.Split(',').Select(i => int.Parse(i)).ToList();
    
    
                //调用业务层的断电方法
    
                Business.PowerService powerService = new Business.PowerService();
                var isSuccess = powerService.PowerOffDevices(ids);
                IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<BaseHub>();
                foreach (var item in ids)
                {
                    hubContext.Clients.All.PowerOn(new { lampId = item, isSuccess = isSuccess });
                }
            }
        }
    
        public class PowerOffJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
                string lampIds = context.JobDetail.JobDataMap.Values.FirstOrDefault().ToString();
                List<int> ids = lampIds.Split(',').Select(i => int.Parse(i)).ToList();
    
                //调用业务层的断电方法
    
                Business.PowerService powerService = new Business.PowerService();
                var isSuccess = powerService.PowerOffDevices(ids);
                IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<BaseHub>();
                foreach (var item in ids)
                {
                    hubContext.Clients.All.PowerOff(new { lampId = item, isSuccess = isSuccess });
                }
            }
        }
    
        public class MeterJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
                List<string> areas = new List<string>();
                AreaService aService = new AreaService();
                aService.GetGlobalAreas(1).Where(a => a.AreaId > 1).ToList().ForEach(a => areas.Add(a.AreaId.ToString()));
    
                //调用ExportService的导出方法
                string path = System.Web.HttpContext.Current.Server.MapPath("~/Files/");
                ExportService service = new ExportService();
                service.ExportMeters(path, DateTime.Now.ToShortDateString() + ".xls", areas.ToList());
            }
        }
    
        public class OtherJob : IJob
        {
            public void Execute(IJobExecutionContext context)
            {
    
            }
        }
  • 相关阅读:
    swift textfiled 输入完毕 return 隐藏键盘 方法
    第一篇,仅是为了纪念
    ASP.NET MVC之验证终结者篇
    ASP.NET MVC扩展之HtmlHelper辅助方法
    Java历程-初学篇 Day04选择结构(1)
    Java历程-初学篇 Day03扫描仪与类型转换
    Java历程-初学篇 Day02变量,数据类型和运算符
    Java历程-初学篇 Day01初识java
    简单工厂模式和策略模式理解
    工厂模式和策略模式的区别
  • 原文地址:https://www.cnblogs.com/tgdjw/p/4991739.html
Copyright © 2020-2023  润新知