我有一个容器(父)工作。它包含子作业。ParentJob 将在所有 childJobs 完成后完成。问题在代码中。
public class ParentJob : InterruptableJob
{
private childrenAreCompleted = false;
private ChildJobListener _listener;
public override void Execute(IJobExecutionContext context)
{
var childs = new int[] { 1, 2, 3 };
_listener = new ChildJobListener(childs.Length);
_listener.OnCompleted += new Action(_listener_OnCompleted);
JobKey jk = typeof(ChildJob).JobKey(); // My Type extension :)
IMatcher<JobKey> matcher = KeyMatcher<JobKey>.KeyEquals(jk);// this is wrong because ParentJob could be called concurrently.
context.Scheduler.ListenerManager.AddJobListener(_listener, matcher);
foreach (var child in childs)
{
JobDataMap jobData = new JobDataMap();
jobData.Add("ParentId", context.FireInstanceId);//TODO: suspected I want to use this with listener for matching.But I could not find Matcher<JobDataMap>.
context.Scheduler.TriggerJob(jk, jobData);
}
// Wait Till _listenerOnCompleted
while (true)
{
Thread.Sleep(2000);
if (childrenAreCompleted)
break;
}
}
void _listener_OnCompleted()
{
childrenAreCompleted=true;
}
}
public class ChildJobListener : IJobListener
{
private int _executedChildCount = 0;
private int _totalChildCount = 0;
public int ActiveChildCount
{
get
{
return _totalChildCount - _executedChildCount;
}
}
public event Action OnCompleted;
public ChildJobListener(int childCount)
{
_totalChildCount = childCount;
}
public void JobExecutionVetoed(IJobExecutionContext context)
{
throw new NotImplementedException();
}
public void JobToBeExecuted(IJobExecutionContext context)
{
}
public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
{
_executedChildCount++;
if (_totalChildCount == _executedChildCount)
{
if (OnCompleted != null)
OnCompleted();
}
}
public string Name
{
get { return "ChildJobListener"; }
}
}