You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.8 KiB
C#

4 years ago
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Quartz.Core;
using Quartz.Impl;
namespace TopshelfStudy.NetCoreDemo
{
public static class QuartzNetExtensions
{
public static IServiceCollection AddCustomQuartz(this IServiceCollection services)
{
var config = services.BuildServiceProvider().GetRequiredService<Microsoft.Extensions.Configuration.IConfiguration>();
//配置
var option = new NameValueCollection
{
//实例名
["quartz.scheduler.instanceName"] = "MissionQuartzScheduler",
//线程数
["quartz.threadPool.threadCount"] = "10",
//线程超时
["quartz.jobStore.misfireThreshold"] = "60000",
//Job数据可以使用序列化数据而非只能使用字符串
["quartz.serializer.type"] = "json",
//配置以使用作业StoreTx
["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
//将 AdoJobStore 配置为使用驱动程序委托
["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.StdAdoDelegate, Quartz",
//使用表前缀配置 AdoJobStore
["quartz.jobStore.tablePrefix"] = "QRTZ_",
//使用“要使用的数据源名称”配置 AdoJobStore
["quartz.jobStore.dataSource"] = "quarztDS",
//设置数据源的连接字符串
["quartz.dataSource.quarztDS.connectionString"] = config.GetConnectionString("SolutionConnection"),
//设置数据库提供程序
["quartz.dataSource.quarztDS.provider"] = "MySql",
//将 AdoJobStore 配置为将字符串用作作业数据映射值(建议)
["quartz.jobStore.useProperties"] = "false",
// 负载均衡
["quartz.jobStore.clustered"] = "true",
["quartz.scheduler.instanceId"] = "AUTO",
};
//使用quartz.config配置文件
var factory = new StdSchedulerFactory();
//或者:使用程序配置项
//var factory = new StdSchedulerFactory(option);
var scheduler = factory.GetScheduler().Result;
services.AddSingleton<ISchedulerFactory>(factory);
services.AddSingleton<IScheduler>(scheduler);
services.AddSingleton<CustomSchedulerListener>();
services.AddSingleton<CustomJobListener>();
services.AddSingleton<CustomTriggerListener>();
return services;
}
}
}