Quartz.Net 核心代码参考:
https://www.cnblogs.com/miskis/p/8487634.html
码云:https://github.com/ice-ko/QuartzNetJob
有兴趣的朋友可以去点击链接去瞧瞧~~
-
因为核心代码参考以上,所以不再赘述。图示是项目中参考以上代码的抽离
Snipaste_2019-12-05_14-32-42.png
2.关于NetCore的Autofac的配置不再赘述,网上有很多的方法。这里我只贴出我的配置方法,Startup.cs
的ConfigureServices
方法中。
注:至于是构造函数注入还是属性注入,这个看个人喜好以及实际应用场景
var builder = new ContainerBuilder();
builder.Populate(services);
var assemblys = Assembly.Load("XXXXXX.Service");
var baseType = typeof(IDependency);
builder.RegisterAssemblyTypes(assemblys).Where(i => baseType.IsAssignableFrom(i) && i != baseType)
.AsImplementedInterfaces().InstancePerLifetimeScope();
var assemblysC = Assembly.Load("XXXXXX.Web");
var IControllerType = typeof(ControllerBase);
builder.RegisterAssemblyTypes(assemblysC).Where(t =>
IControllerType.IsAssignableFrom(t) && t != IControllerType);
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IBaseRepository<>)).InstancePerDependency();
var container = builder.Build();
//设置服务容器
ServiceLocator.SetContainer(container);
var autofacServiceProvider = new AutofacServiceProvider(ServiceLocator.GetContainer()).RegisterServiceProvider();
//启动任务
new StartSchedule().Start();
return autofacServiceProvider;
autofac容器设置
public class ServiceLocator
{
private static IContainer _container;
/// <summary>
/// 设置Ico容器
/// </summary>
/// <param name="container"></param>
public static void SetContainer(IContainer container)
{
_container = container;
}
/// <summary>
/// 获取服务
/// </summary>
/// <param name="serviceType"></param>
/// <returns></returns>
public static object GetService(Type serviceType)
{
return _container.Resolve(serviceType);
}
/// <summary>
/// 获取服务
/// </summary>
/// <typeparam name="TService"></typeparam>
/// <returns></returns>
public static TService GetService<TService>()
{
return _container.Resolve<TService>();
}
/// <summary>
/// 获取容器对象
/// </summary>
/// <returns></returns>
public static IContainer GetContainer()
{
return _container;
}
}
启动任务的方法
public class StartSchedule
{
private readonly IScheduleService ScheduleService = ServiceLocator.GetService<IScheduleService>();
public void Start()
{
//获取数据库中保存的任务数据
var schedule = ScheduleService.GetSchedules(i => i.JobStatus == 1);
foreach (var item in schedule)
{
if (!string.IsNullOrEmpty(item.Url) || !string.IsNullOrEmpty(item.AssemblyName))
{
//自定义映射Model转换,可采用自己的实现方式
var scheduleEntity = HelperMapper<Schedule, ScheduleEntity>.Map(item);
ScheduleManage.Instance.AddScheduleList(scheduleEntity);
var result = SchedulerCenter.Instance.RunScheduleJob<ScheduleManage>(item.JobGroup, item.JobName).Result;
}
}
}
}
任务执行者
public class ScheduleLog : IJob
{
private readonly IScheduleService ScheduleService = ServiceLocator.GetService<IScheduleService>();
public async Task Execute(IJobExecutionContext context)
{
var manage = new ScheduleManage();
var schedule = new ScheduleEntity
{
JobGroup = context.JobDetail.Key.Group,
JobName = context.JobDetail.Key.Name,
RunStatus = JobRunStatus.执行中
};
manage.UpdateScheduleRunStatus(schedule);
var model = manage.GetScheduleModel(context.JobDetail.Key.Group, context.JobDetail.Key.Name);
//调用真正的任务处理者
ScheduleService.ScheduleLog();
await Task.CompletedTask;
}
}
注意事项:
1.在SchedulerCenter
类中有一行的改动,适合于自己。
在AddScheduleJob
方法中,
//反射获取任务执行类
//这个是原写法
//var jobType = HelperFile.GetAbsolutePath(m.AssemblyName, m.AssemblyName + "." + m.ClassName);
//这个是适合我的,可以不修改或修改成适合自己的。
//原因:ClassName,我这里是写的全称,原代码写的不是全称
var jobType = HelperFile.GetAbsolutePath(m.AssemblyName, m.ClassName);
网友评论