using Microsoft.Extensions.DependencyInjection; using Quartz; using Quartz.Spi; using System; namespace EntrustSettle.Tasks { public class JobFactory : IJobFactory { /// /// 注入反射获取依赖对象 /// private readonly IServiceProvider _serviceProvider; public JobFactory(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; } /// /// 实现接口Job /// /// /// /// public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) { try { var serviceScope = _serviceProvider.CreateScope(); var job = serviceScope.ServiceProvider.GetService(bundle.JobDetail.JobType) as IJob; return job; } catch (Exception) { throw; } } public void ReturnJob(IJob job) { var disposable = job as IDisposable; if (disposable != null) { disposable.Dispose(); } } } }