quartz.net 3.x 使用总结

Posted axel10

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了quartz.net 3.x 使用总结相关的知识,希望对你有一定的参考价值。

quartz文档:https://www.quartz-scheduler.net/documentation/index.html

这里用新建的控制台项目进行演示。

目标效果为每隔一秒在控制台上输出一句

Greetings from HelloJob!
using System;
using System.Collections.Specialized;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;
using Quartz.Logging;

namespace ConsoleApp1
{
        public class Program
    {
        private static void Main(string[] args)
        {
            LogProvider.SetCurrentLogProvider(new ConsoleLogProvider());    

            RunProgramRunExample().GetAwaiter().GetResult();

            Console.WriteLine("Press any key to close the application");
            Console.ReadKey();
        }

        private static async Task RunProgramRunExample()
        {
            try
            {
                // Grab the Scheduler instance from the Factory
                NameValueCollection props = new NameValueCollection
                {
                    { "quartz.serializer.type", "binary" }
                };
                StdSchedulerFactory factory = new StdSchedulerFactory(props);
                IScheduler scheduler = await factory.GetScheduler();

                // and start it off
                await 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(1)            //在这里配置执行延时
                        .RepeatForever())
                    .Build();

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

                // some sleep to show what‘s happening
//                await Task.Delay(TimeSpan.FromSeconds(5));
// and last shut down the scheduler when you are ready to close your program
//                await scheduler.Shutdown();           

//如果解除
await Task.Delay(TimeSpan.FromSeconds(5))和await scheduler.Shutdown()的注释,
//5秒后输出"Press any key to close the application",
//scheduler里注册的任务也会停止。

 

            }
            catch (SchedulerException se)
            {
                Console.WriteLine(se);
            }
        }

        // simple log provider to get something to the console
        private class ConsoleLogProvider : ILogProvider
        {
            public Logger GetLogger(string name)
            {
                return (level, func, exception, parameters) =>
                {
                    if (level >= LogLevel.Info && func != null)
                    {
                        Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] [" + level + "] " + func(), parameters);
                    }
                    return true;
                };
            }

            public IDisposable OpenNestedContext(string message)
            {
                throw new NotImplementedException();
            }

            public IDisposable OpenMappedContext(string key, string value)
            {
                throw new NotImplementedException();
            }
        }
    }

    public class HelloJob : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
        //任务主体,这里强制要求必须是异步方法,如果不想用异步可以使用quartz 2.x版本
await Console.Out.WriteLineAsync("Greetings from HelloJob!"); } } }

 

 






以上是关于quartz.net 3.x 使用总结的主要内容,如果未能解决你的问题,请参考以下文章

Quartz.net 3.x使用总结——Db持久化和集群

Quartz.net 3.x使用总结——Db持久化和集群

Quartz.NET总结Quartz 远程调度

Quartz.net使用总结

Quartz.Net间隔N周/日/年定时触发器写法总结

Quartz.NET总结如何根据自己需要配置Topshelf 服务