• 任务调度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/

  • 相关阅读:
    ThinkPHP-5.0.23新的RCE漏洞测试和POC
    利用DNS日志进行MySQL盲注
    Prometheus自动发现
    relabel_config
    Maven安装
    Redis安装
    Node.js安装
    MySQL-5.7安装
    设计模式【1】------>单例模式
    Java设计模式概念以及六大设计原则
  • 原文地址:https://www.cnblogs.com/chenyishi/p/8417708.html
Copyright © 2020-2023  润新知