• 任务调度quartz


    using Quartz;
    using Quartz.Impl;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ConsoleApp10
    {
        [DisallowConcurrentExecution()]
        public abstract class JobBase : IJob
        {
            #region IJob 成员
    
            public Task Execute(IJobExecutionContext context)
            {
                try
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(DateTime.Now.ToString() + "{0}这个Job开始执行", context.JobDetail.Key.Name);
                    ExcuteJob(context);
                    return Task.CompletedTask; ;
                }
                catch (Exception ex)
                {
                    //LoggerFactory.CreateLog().Logger_Debug(this.GetType().Name + "error:" + ex.Message);
                    throw;
                }
            }
    
            #endregion
            /// <summary>
            /// 执行计划,子类可以重写
            /// </summary>
            public virtual string Cron => "0/1 * * * * ?";
            /// <summary>
            /// Job具体类去实现自己的逻辑
            /// </summary>
            protected abstract void ExcuteJob(IJobExecutionContext context);
        }
    }
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Threading;
    using Quartz;
    
    namespace ConsoleApp10
    {
        class Job : JobBase
        {
    
    
            protected override void ExcuteJob(IJobExecutionContext context)
            {
                Console.WriteLine("执行"+DateTime.Now.ToString());
            }
        }
    }
    using Quartz;
    using Quartz.Impl;
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApp10
    {
        public class QuartzManager
        {
            public static Action<Type> JoinToQuartz = (type) =>
            {
                StdSchedulerFactory.GetDefaultScheduler().Result.Start();
                var obj = Activator.CreateInstance(type);
                string cron = type.GetProperty("Cron").GetValue(obj).ToString();
                var jobDetail = JobBuilder.Create(type)
                                          .WithIdentity(type.Name)
                                          .Build();
    
                var jobTrigger = TriggerBuilder.Create()
                                               .WithIdentity(type.Name + "Trigger")
                                               .StartNow()
                                               .WithCronSchedule(cron)
                                               .Build();
    
                StdSchedulerFactory.GetDefaultScheduler().Result.ScheduleJob(jobDetail, jobTrigger);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine($"新添加了一个服务{nameof(type)},通过心跳Job自动被加载!");
            };
        }
    }
    using System;
    
    namespace ConsoleApp10
    {
        class Program
        {
            static void Main(string[] args)
            {
                
                QuartzManager.JoinToQuartz(typeof(Job));
    
                Console.ReadLine();
            }
        }
    }

    https://www.cnblogs.com/mushroom/p/4067037.html

    http://cron.qqe2.com/

  • 相关阅读:
    VS.NET 2005 常用的快捷键
    路径,文件,目录,I/O常见操作汇总
    c#中cookies的存取操作
    ASP.NET AJAX入门系列(1):概述
    RS2008中控件ID冲突问题
    [书名]各种计算机语言的经典书籍
    终于把课件做好了~~
    还是两个数的交换问题
    自制简易图片尺寸调整工具[源]
    被点名了~~~[游戏]
  • 原文地址:https://www.cnblogs.com/chenyishi/p/8417708.html
Copyright © 2020-2023  润新知