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

  • 相关阅读:
    怎样不重启设置字体边缘平滑立即生效! 以下注册表导入后不能立即生效。。
    Delphi下实现全屏快速找图找色 一、数据提取
    delphi2006语言新特性:Record类型高级用法
    delphi之模糊找图
    delphi之精确找图
    delphi2006语言新特性——类静态字段、类属性
    Delphi程序开启XP的ClearType显示效果
    delphi Createthread的线程传参数
    修改窗体非客户区大小更改窗体标题栏高度
    windows 匿名管道读取子进程输出
  • 原文地址:https://www.cnblogs.com/ziteng1807/p/11364712.html
Copyright © 2011-2022 走看看