记录Quarter的基本使用

using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;
using Quartz.Logging;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Transactions;
using System.Web;
using WebApplication3.Models;
using WebApplication3.Service;

namespace WebApplication3
{
    public static class QuartzHelper
    {
        public 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")
                    //将在以后的整点触发
                    .StartAt(DateBuilder.EvenHourDate(null))
                    //.StartAt(DateBuilder.FutureDate(1, IntervalUnit.Day))
                    //.StartNow()
                    //.WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(19, 36))//每天几点几分触发
                    .WithSimpleSchedule(x => x
                                               .WithIntervalInHours(1)//每1小时触发
                                                                      //.WithIntervalInMinutes(1)//每分钟
                                                                      //.WithIntervalInSeconds(10)
                        .RepeatForever())
                    .Build();

                ///监听任务
                //scheduler.ListenerManager.AddJobListener(new MyJobListener(), KeyMatcher<JobKey>.KeyEquals(new JobKey("job1", "group1")));

                scheduler.ListenerManager.AddJobListener(new MyJobListener(), GroupMatcher<JobKey>.AnyGroup());

                // Tell quartz to schedule the job using our trigger
                await scheduler.ScheduleJob(job, trigger);

                // and last shut down the scheduler when you are ready to close your program
                //await scheduler.Shutdown();
            }
            catch (SchedulerException se)
            {
                Console.WriteLine(se);
            }
        }
    }
    public class MyJobListener : IJobListener
    {
        public string Name
        {
            get
            {
                return "监听任务";
            }
        }
        public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
        {
            return Task.Run(() =>
            {
                JobToBeExecuted();
            });
        }

        public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
        {
            return Task.Run(() =>
            {
                JobExecutionVetoed();
            });
        }

        public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
        {
            return Task.Run(() =>
             {
                 JobWasExecuted();
             });
        }
        /// <summary>
        /// 执行前
        /// </summary>
        public void JobToBeExecuted()
        {
        }
        /// <summary>
        /// 触发失效
        /// </summary>
        public void JobExecutionVetoed()
        {
            string path4 = System.AppDomain.CurrentDomain.BaseDirectory;
            new Common.CommonFun().WriteLog("位置:QuartzHelper,定时器触发失效!", "autoUpdate");
        }
        /// <summary>
        /// 触发完成
        /// </summary>
        public void JobWasExecuted()
        {
        }
    }
    /// <summary>
    /// 定时任务
    /// </summary>
    public class HelloJob : IJob
    {
        public async Task Execute(IJobExecutionContext context)
        {
            await Task.Run(() =>
            {
                   
            });
        }
    }
}

记录Quartz的基本使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值