出售域名 11365.com.cn
有需要请联系 16826375@qq.com
在手机上浏览
在手机上浏览

Quartz使用方法

发布日期:2020-05-18

topshelf要以实现Windows服务的操作。Quartz替代了Timer的功能。

一、调度任务

Quartz的功能挺简洁的,一个Job(被执行的任务),一个Trigger,一个IScheduler(调试器)

using log4net;
using Quartz;
using Quartz.Impl;

namespace Quartz2._0
{
    public class ServiceRunner
    {
        #region 调度
        private readonly IScheduler scheduler;

        public ServiceRunner()
        {
            scheduler = StdSchedulerFactory.GetDefaultScheduler();
        }

        public bool Start()
        {
            // and start it off
            scheduler.Start();

            // define the job and tie it to our HelloJob class
            IJobDetail job = JobBuilder.Create<HelloJob>()
                .WithIdentity("job1", "group1")
                .Build();

            // Trigger the job to run now, and then repeat every 10 seconds
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .StartNow()
                .WithSimpleSchedule(x => x
                    .WithIntervalInSeconds(10)
                    .RepeatForever())
                .Build();

            // Tell quartz to schedule the job using our trigger
            scheduler.ScheduleJob(job, trigger);

            return true;
        }

        public bool Stop()
        {
            scheduler.Shutdown(false);
            return true;
        }

        public bool Continue()
        {
            scheduler.ResumeAll();
            return true;
        }

        public bool Pause()
        {
            scheduler.PauseAll();
            return true;
        }
        #endregion
    }

    public class HelloJob : IJob
    {
        ILog log = LogManager.GetLogger(typeof(HelloJob));

        public void Execute(IJobExecutionContext context)
        {
            log.Info("这是一个测试1");
        }
    }
}

详情的可以参数官网文档:https://www.quartz-scheduler.net/documentation/quartz-2.x/quick-start.html

二、实现服务

using System;
using System.IO;
using System.Threading;
using log4net.Config;
using Topshelf;

namespace Quartz2._0
{
    public class Program
    {
        private static void Main(string[] args)
        {
            var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            XmlConfigurator.ConfigureAndWatch(logCfg);

            var rc = HostFactory.Run(x =>                                   //1
            {
                x.Service<ServiceRunner>(s =>                                   //2
                {
                    s.ConstructUsing(name => new ServiceRunner());                //3
                    s.WhenStarted(tc => tc.Start());                         //4
                    s.WhenStopped(tc => tc.Stop());                          //5
                });
                x.RunAsLocalSystem();                                       //6

                x.SetDescription("Sample Topshelf Host");                   //服务描述
                x.SetDisplayName("Stuff");                                  //服务显示名称
                x.SetServiceName("Stuff");                                  //服务名称
            });                                                             //10

            var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());  //11
            Environment.ExitCode = exitCode;
        }
    }
}

点击下载源码