码迷,mamicode.com
首页 > 其他好文 > 详细

任务调度quartz

时间:2018-02-05 16:01:56      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:通过   res   build   roo   ram   now()   for   prot   www.   

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

任务调度quartz

标签:通过   res   build   roo   ram   now()   for   prot   www.   

原文地址:https://www.cnblogs.com/chenyishi/p/8417708.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!