• Quartz 自动定时任务


    class job1:IJob
        {
          async   Task IJob.Execute(IJobExecutionContext context)
            {
                await Console.Out.WriteLineAsync("作业执行1!");
                //await new Task(() => {
                //    Console.WriteLine("作业执行1!");
                //});
            }

        }

    class job2:IJob
        {
            async Task IJob.Execute(IJobExecutionContext context)
            {
                await Console.Out.WriteLineAsync("作业执行2!");
                //await new Task(() =>
                //{
                //    Console.WriteLine("作业执行2!");
                //});
            }
        }

    using Quartz;
    using Quartz.Impl;
    using Quartz.Logging;
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

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

                RunProgram().GetAwaiter().GetResult();

                Console.WriteLine("Press any key to close the application");
                Console.ReadKey();
            }
            private static async Task RunProgram()
            {
                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<job1>()
                        .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();


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

                    // Trigger the job to run now, and then repeat every 10 seconds
                    ITrigger trigger2 = TriggerBuilder.Create()
                        .WithIdentity("trigger2", "group2")
                        .StartNow()
                        .WithSimpleSchedule(x => x
                            .WithIntervalInSeconds(4)
                            .RepeatForever())
                        .Build();
                    // Tell quartz to schedule the job using our trigger
                    await scheduler.ScheduleJob(job, trigger);
                    await scheduler.ScheduleJob(job2, trigger2);
                    // some sleep to show what's happening
                    await Task.Delay(TimeSpan.FromSeconds(60));

                    // and last shut down the scheduler when you are ready to close your program
                    //await scheduler.Shutdown();
                }
                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();
                }
            }
        }
    }

  • 相关阅读:
    nginx的proxy_pass后面是否带/的区别总结
    SharePoint 通过各种对象触发工作流
    SharePoint 模拟审批Nintex工作流
    SharePoint Online 部件InjectionScriptWebpart制作图片轮播
    SharePoint Modern Page 的脚本引入部件
    SharePoint Online 为Modern Page添加脚本
    SharePoint Online 触发Teams审批
    SharePoint Online InjectionScriptWebpart部署介绍
    SharePoint Online 部件InjectionScriptWebpart制作新闻详情
    SharePoint Online 触发的Automate工作流的调试
  • 原文地址:https://www.cnblogs.com/ziteng1807/p/11364712.html
Copyright © 2020-2023  润新知