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();
                }
            }
        }
    }

  • 相关阅读:
    ionic的start-y属性初始化页面
    AngularJS时间轴指令
    [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys
    常用CSS样式
    angularjs ng-click
    不是SELECTed表达式
    跨域资源共享(CORS)问题解决方案
    AngularJS身份验证:Cookies VS Tokens
    Javascript跨域
    Vmware虚拟机centos7命令登录模式与成桌面模式切换
  • 原文地址:https://www.cnblogs.com/ziteng1807/p/11364712.html
Copyright © 2011-2022 走看看