zoukankan      html  css  js  c++  java
  • quartz简单使用

    using Quartz;
    using Quartz.Impl;
    using System;
    using System.Threading.Tasks;
    
    namespace quartzTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                ///一般定时任务
                Run();
                //传参方式循环
                Run3();
                Console.ReadKey();
            }
    
    
            public async static Task Run3()
            {
                // 1.创建scheduler的引用
                ISchedulerFactory schedFact = new StdSchedulerFactory();
                IScheduler sched = await schedFact.GetScheduler();
    
                //2.启动 scheduler
                await sched.Start();
            
                ///传参数方式
                IJobDetail job = JobBuilder.Create<DumbJob>()
                                 .WithIdentity("myJob", "group1") // name "myJob", group "group1"
                                 .UsingJobData("jobSays", "Hello World!")
                                 .UsingJobData("myFloatValue", 3.141f)
                                 .Build();
    
                ITrigger trigger = TriggerBuilder.Create()
                              .WithIdentity("myTrigger", "group1")
                              .StartNow()
                              .WithSimpleSchedule(x => x
                                  .WithIntervalInSeconds(3)
                                  .RepeatForever())
                              .Build();
                await sched.ScheduleJob(job, trigger);
            }
    
    
    
    
            /// <summary>
            /// 任务调度的使用过程
            /// </summary>
            /// <returns></returns>
            public async static Task Run()
            {
                // 1.创建scheduler的引用
                ISchedulerFactory schedFact = new StdSchedulerFactory();
                IScheduler sched = await schedFact.GetScheduler();
    
                //2.启动 scheduler
                await sched.Start();
    
                // 3.创建 job
                IJobDetail job = JobBuilder.Create<SimpleJob>()
                        .WithIdentity("job1", "group1")
                        .Build();
    
                // 4.创建 trigger
                ITrigger trigger = TriggerBuilder.Create()
                    .WithIdentity("trigger1", "group1")
                    .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
                    .Build();
    
                // 5.使用trigger规划执行任务job
                await sched.ScheduleJob(job, trigger);
            }
        }
        /// <summary>
        /// 任务
        /// </summary>
        public class SimpleJob : IJob
        {
            public virtual Task Execute(IJobExecutionContext context)
            {
                return Console.Out.WriteLineAsync($"job工作了 在{DateTime.Now}");
            }
    
        }
        public class DumbJob : IJob
        {
            public async Task Execute(IJobExecutionContext context)
            {
                JobKey key = context.JobDetail.Key;
    
                JobDataMap dataMap = context.JobDetail.JobDataMap;
    
                string jobSays = dataMap.GetString("jobSays");
                float myFloatValue = dataMap.GetFloat("myFloatValue");
    
                await Console.Error.WriteLineAsync("Instance " + key + " of DumbJob says: " + jobSays + ", and val is: " + myFloatValue);
            }
        }
    }

    至于定时,周期的方式都很简单,百度下都有了

  • 相关阅读:
    PAT Advanced 1067 Sort with Swap(0, i) (25分)
    PAT Advanced 1048 Find Coins (25分)
    PAT Advanced 1060 Are They Equal (25分)
    PAT Advanced 1088 Rational Arithmetic (20分)
    PAT Advanced 1032 Sharing (25分)
    Linux的at命令
    Sublime Text3使用指南
    IntelliJ IDEA创建第一个Groovy工程
    Sublime Text3 安装ftp插件
    Sublime Text3配置Groovy运行环境
  • 原文地址:https://www.cnblogs.com/wangchuang/p/14652171.html
Copyright © 2011-2022 走看看