在我的Windows服务中,我将Quartz.net与Autofac一起使用。为了帮助我,我正在使用Nuget Autofac.Extras.Quartz。 到目前为止一切顺利,但是当我尝试应用DisallowConcurrencyExecution属性时,它会被忽略并生成多个作业。
我使用如下:
注册
builder.RegisterModule(new QuartzAutofacFactoryModule());
builder.RegisterType<CompanyDatabaseProcessor>().As<IJob>().InstancePerLifetimeScope();
SETUP
var fact = container.Resolve<IJobFactory>();
_scheduler = StdSchedulerFactory.GetDefaultScheduler();
_scheduler.JobFactory = fact;
_scheduler.Start();
var job = JobBuilder.Create<IJob>()
.WithIdentity("QProcJob", "A3Group")
.Build();
var trigger = TriggerBuilder.Create()
.WithIdentity("QProcTrigger", "A3Group")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(executeEvery)
.RepeatForever())
.Build();
_scheduler.ScheduleJob(job, trigger);
当我在我的IJob中时,我检查了context.JobDetail.ConcurrentExecutionDisallowed它总是设置为false。
答案 0 :(得分:1)
我看到这个问题已经过时了,但我遇到了同样的问题,我将发布我的解决方案,因此对某人有用。 问题是当你使用JobFactory时,你的作业类型对于quartz来说是未知的,所以他指定了一个没有用DisallowConcurrentExecution属性修饰的内部类型(NoOpJob),导致JobDetailImpl(quartz.net使用的类) IJobDetail)在ConcurrentExecutionDisallowed属性中返回false。 我所做的是一个简单的类,它继承自JobDetailImpl,并在JobBuilder类中添加一个扩展方法Build,以在作业创建中保持相同的流畅代码。这是代码:
public class SingleJobDetail : JobDetailImpl
{
public SingleJobDetail() : base()
{
}
public override bool ConcurrentExecutionDisallowed => true;
}
public static class JobHelper
{
public static IJobDetail Build(this JobBuilder builder, bool disallowConcurrent)
{
var job = builder.Build();
if (!disallowConcurrent)
return job;
var singleJob = new SingleJobDetail();
singleJob.JobType = job.JobType;
singleJob.Description = job.Description;
singleJob.Key = job.Key;
singleJob.Durable = job.Durable;
singleJob.RequestsRecovery = job.RequestsRecovery;
if (!singleJob.JobDataMap.IsEmpty)
singleJob.JobDataMap = job.JobDataMap;
return singleJob;
}
}
在创造就业机会中:
var job = JobBuilder.Create().WithIdentity(id).Build(true);