【发布时间】:2016-08-26 06:48:05
【问题描述】:
我正在使用 Quartz.net 调度程序将数据从一个数据库迁移到另一个数据库。我想为每个数据库表数据传输启动一个单独的工作。我有一个用于数据迁移的父作业,它会触发每个表的子作业。我希望父作业等待其所有子作业完成,但是现在当我开始父作业时,它会触发所有继续运行的子作业,但一旦触发所有作业,父作业就会完成。 提前谢谢..
示例代码如下
public class DataMigrationJob : IJob
{
public void Execute(IJobExecutionContext context)
{
List<string> tableList = somelist;
foreach (var table in tableList)
{
JobDetailImpl job = new JobDetailImpl(jobKey, groupName, tableJobType, true, true);
if (!SchedulerProvider.Scheduler.CheckExists(job.Key))
{
context.Scheduler.AddJob(job, true, true);
}
context.Scheduler.TriggerJob(job.Key);
}
}
}
是否有适当的方法让父作业继续运行,直到所有子作业完成?
我有一个想法通过使用 while 循环来使其工作,该循环将在条件中调用一个函数并添加等待,如下所示
public boolena AllChildrenJobCompleted(IJobExecutionContext context, List<JobKey> jobKeys)
{
runningJobsList = context.Scheduler.GetCurrentlyExecutingJobs();
//check if children jobs are part of this list
//return false until list contains any of the child job
//return true when list does not contain child jobs
}
稍后可以像这样在父作业的执行函数中调用此函数
while(!AllChildrenJobCompleted(context,childJobs))
{wait(1000);}
不确定这是否是最佳实践。
【问题讨论】:
标签: c# quartz-scheduler quartz.net