Quartz .NET - 防止并行作业执行
Quartz .NET - Prevent parallel Job Execution
我正在使用 Quartz .NET 进行作业调度。
所以我创建了一份工作 class(实施 IJob)。
public class TransferData : IJob
{
public Task Execute(IJobExecutionContext context){
string tableName = context.JobDetail.JobDataMap.Get("table");
// Transfer the table here.
}
}
所以我想转移不同的和多个table。为此,我正在做这样的事情:
foreach (Table table in tables)
{
IJobDetail job = JobBuilder.Create<TransferData>()
.WithIdentity(new JobKey(table.Name, "table_transfer"))
.UsingJobData("table", table.Name)
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(new TriggerKey("trigger_" + table.Name, "table_trigger"))
.WithCronSchedule("*/5 * * * *")
.ForJob(job)
.Build();
await this.scheduler.ScheduleJob(job, trigger);
}
所以每 table 应该每 5 分钟传输一次。为此,我创建了多个具有不同作业名称的作业。
问题是:如何防止同一作业名称的并行作业执行? (例如,之前的 运行 需要更长的时间来传输一个 table,所以我不想为相同的 table 开始下一次传输。)
我知道属性@DisallowConcurrentExecution,但这用于防止同一作业的并行执行class。我不想为每个 table 写一个额外的作业 class,因为传输的 "main" 代码总是相同的,唯一的区别是 table 名称.所以我想为此目的使用相同的作业 class。
Quatz .NET 文档有点混乱。
DisallowConcurrentExecution is an attribute that can be added to the
Job class that tells Quartz not to execute multiple instances of a
given job definition (that refers to the given job class)
concurrently. Notice the wording there, as it was chosen very
carefully. In the example from the previous section, if
“SalesReportJob” has this attribute, than only one instance of
“SalesReportForJoe” can execute at a given time, but it can execute
concurrently with an instance of “SalesReportForMike”. The constraint
is based upon an instance definition (JobDetail), not on instances of
the job class. However, it was decided (during the design of Quartz)
to have the attribute carried on the class itself, because it does
often make a difference to how the class is coded.
来源:https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/more-about-jobs.html
但是如果您阅读 API 文档,它会说:粗体文本很重要!
An attribute that marks a IJob class as one that must not have
multiple instances executed concurrently (where instance is based-upon
a IJobDetail definition - or in other words based upon a JobKey).
来源:https://quartznet.sourceforge.io/apidoc/3.0/html/
换句话说:DisallowConcurrentExecution 属性适用于我的目的。
我正在使用 Quartz .NET 进行作业调度。 所以我创建了一份工作 class(实施 IJob)。
public class TransferData : IJob
{
public Task Execute(IJobExecutionContext context){
string tableName = context.JobDetail.JobDataMap.Get("table");
// Transfer the table here.
}
}
所以我想转移不同的和多个table。为此,我正在做这样的事情:
foreach (Table table in tables)
{
IJobDetail job = JobBuilder.Create<TransferData>()
.WithIdentity(new JobKey(table.Name, "table_transfer"))
.UsingJobData("table", table.Name)
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity(new TriggerKey("trigger_" + table.Name, "table_trigger"))
.WithCronSchedule("*/5 * * * *")
.ForJob(job)
.Build();
await this.scheduler.ScheduleJob(job, trigger);
}
所以每 table 应该每 5 分钟传输一次。为此,我创建了多个具有不同作业名称的作业。 问题是:如何防止同一作业名称的并行作业执行? (例如,之前的 运行 需要更长的时间来传输一个 table,所以我不想为相同的 table 开始下一次传输。)
我知道属性@DisallowConcurrentExecution,但这用于防止同一作业的并行执行class。我不想为每个 table 写一个额外的作业 class,因为传输的 "main" 代码总是相同的,唯一的区别是 table 名称.所以我想为此目的使用相同的作业 class。
Quatz .NET 文档有点混乱。
DisallowConcurrentExecution is an attribute that can be added to the Job class that tells Quartz not to execute multiple instances of a given job definition (that refers to the given job class) concurrently. Notice the wording there, as it was chosen very carefully. In the example from the previous section, if “SalesReportJob” has this attribute, than only one instance of “SalesReportForJoe” can execute at a given time, but it can execute concurrently with an instance of “SalesReportForMike”. The constraint is based upon an instance definition (JobDetail), not on instances of the job class. However, it was decided (during the design of Quartz) to have the attribute carried on the class itself, because it does often make a difference to how the class is coded.
来源:https://www.quartz-scheduler.net/documentation/quartz-3.x/tutorial/more-about-jobs.html
但是如果您阅读 API 文档,它会说:粗体文本很重要!
An attribute that marks a IJob class as one that must not have multiple instances executed concurrently (where instance is based-upon a IJobDetail definition - or in other words based upon a JobKey).
来源:https://quartznet.sourceforge.io/apidoc/3.0/html/
换句话说:DisallowConcurrentExecution 属性适用于我的目的。