zoukankan      html  css  js  c++  java
  • 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!"); } } }

     

  • 相关阅读:
    vue-quill-editor的自定义设置字数长度方法和显示剩余数字
    element-ui表格show-overflow-tooltip="true",鼠标移上去显示的宽度设置
    vue + elementui表单重置 resetFields问题(无法重置表单)
    element ui表单验证,validate与resetFields的使用你知道哪些
    前端下载文件(GET、POST方法)
    vue中使用elementui里的table时,需求是前面的勾选框根据条件判断是否可以勾选设置
    流体力学笔记 第一章 向量场的概念及运算
    Gersgorin定理
    奇异值分解的证明和直观理解
    2020机器学习学习笔记
  • 原文地址:https://www.cnblogs.com/axel10/p/8436077.html
Copyright © 2011-2022 走看看