Quartz.NET 和 Autofac 每个 JobExecutionContext 的实例
Instance per JobExecutionContext with Quartz.NET and Autofac
我正在寻找的是在每次执行 quartz 作业期间都有一个生命周期。有什么方法可以让 Quartz.NET 和 Autofac 有一个 InstancePerJobExecutionContext
生命周期?
这是我已经在使用的片段:
public class AutofacJobFactory : IJobFactory {
private readonly ILifetimeScope _container;
private static readonly ConcurrentDictionary<IJob, ILifetimeScope>
_childScopesMap = new ConcurrentDictionary<IJob, ILifetimeScope>();
public AutofacJobFactory(ILifetimeScope container) {
_container = container;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) {
var childScope = _container.BeginLifetimeScope();
var job = (IJob)childScope.Resolve(bundle.JobDetail.JobType);
_childScopesMap.TryAdd(job, childScope);
return job;
}
public void ReturnJob(IJob job) {
if (!_childScopesMap.TryRemove(job, out var scope))
return;
try {
scope.Dispose();
} catch (Exception ex) {
// TODO: handle/log
}
}
}
但我在想是否有 better/performant/stable 方法来做到这一点?
似乎使用的代码片段运行良好:
public class AutofacJobFactory : IJobFactory {
private readonly ILifetimeScope _container;
private static readonly ConcurrentDictionary<IJob, ILifetimeScope>
_childScopesMap = new ConcurrentDictionary<IJob, ILifetimeScope>();
public AutofacJobFactory(ILifetimeScope container) {
_container = container;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) {
var childScope = _container.BeginLifetimeScope();
var job = (IJob)childScope.Resolve(bundle.JobDetail.JobType);
_childScopesMap.TryAdd(job, childScope);
return job;
}
public void ReturnJob(IJob job) {
if (!_childScopesMap.TryRemove(job, out var scope))
return;
try {
scope.Dispose();
} catch (Exception ex) {
// TODO: handle/log
}
}
}
我正在寻找的是在每次执行 quartz 作业期间都有一个生命周期。有什么方法可以让 Quartz.NET 和 Autofac 有一个 InstancePerJobExecutionContext
生命周期?
这是我已经在使用的片段:
public class AutofacJobFactory : IJobFactory {
private readonly ILifetimeScope _container;
private static readonly ConcurrentDictionary<IJob, ILifetimeScope>
_childScopesMap = new ConcurrentDictionary<IJob, ILifetimeScope>();
public AutofacJobFactory(ILifetimeScope container) {
_container = container;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) {
var childScope = _container.BeginLifetimeScope();
var job = (IJob)childScope.Resolve(bundle.JobDetail.JobType);
_childScopesMap.TryAdd(job, childScope);
return job;
}
public void ReturnJob(IJob job) {
if (!_childScopesMap.TryRemove(job, out var scope))
return;
try {
scope.Dispose();
} catch (Exception ex) {
// TODO: handle/log
}
}
}
但我在想是否有 better/performant/stable 方法来做到这一点?
似乎使用的代码片段运行良好:
public class AutofacJobFactory : IJobFactory {
private readonly ILifetimeScope _container;
private static readonly ConcurrentDictionary<IJob, ILifetimeScope>
_childScopesMap = new ConcurrentDictionary<IJob, ILifetimeScope>();
public AutofacJobFactory(ILifetimeScope container) {
_container = container;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) {
var childScope = _container.BeginLifetimeScope();
var job = (IJob)childScope.Resolve(bundle.JobDetail.JobType);
_childScopesMap.TryAdd(job, childScope);
return job;
}
public void ReturnJob(IJob job) {
if (!_childScopesMap.TryRemove(job, out var scope))
return;
try {
scope.Dispose();
} catch (Exception ex) {
// TODO: handle/log
}
}
}