-
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的基本使用