我写了一个JobListener
来跟踪WPF应用启动的Quartz作业。
该应用会创建一个作业列表,调用BackgroundWorker
来调度所有作业,并为每个作业注册JobListener
。 JobListener
旨在更新将在应用用户界面中动态显示的不同列表。
正确安排和执行作业,但由于某些未知原因,JobListener
事件永远不会被触发。
JobListener:
class DummyJobListener : IJobListener
{
private ObservableCollection<JobKey> _pendingJobs;
private ObservableCollection<JobKey> _runningJobs;
private ObservableCollection<JobKey> _finishedJobs;
public readonly Guid Id = Guid.NewGuid();
public StressJobListener(ObservableCollection<JobKey> pendingJobs, ObservableCollection<JobKey> runningJobs, ObservableCollection<JobKey> finishedJobs)
{
_pendingJobs = pendingJobs;
_runningJobs = runningJobs;
_finishedJobs = finishedJobs;
}
public void JobToBeExecuted(IJobExecutionContext context)
{
JobKey jobKey = context.JobDetail.Key;
_pendingJobs.Remove(jobKey);
_runningJobs.Add(jobKey);
}
public void JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
{
JobKey jobKey = context.JobDetail.Key;
_runningJobs.Remove(jobKey);
_finishedJobs.Add(jobKey);
}
public void JobExecutionVetoed(IJobExecutionContext context)
{
}
public string Name
{
get { return "DummyJobListener" + Id; }
}
}
App:
private BackgroundWorker _runJobs = new BackgroundWorker();
ObservableCollection<IJobDetail> jobList = new ObservableCollection<IJobDetail>();
ObservableCollection<JobKey> _pendingJobs = new ObservableCollection<JobKey>();
ObservableCollection<JobKey> _runningJobs = new ObservableCollection<JobKey>();
ObservableCollection<JobKey> _finishedJobs = new ObservableCollection<JobKey>();
public MainWindow()
{
InitializeComponent();
// fill jobList with jobs
_runJobs.DoWork += RunJobs;
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
_runJobs.RunWorkerAsync();
}
private void RunJobs(object sender, DoWorkEventArgs e)
{
IScheduler scheduler = // GetScheduler
foreach (IJobDetail x in jobList)
{
scheduler.ListenerManager.AddJobListener(new DummyJobListener(_pendingJobs, _runningJobs, _finishedJobs), KeyMatcher<JobKey>.KeyEquals(x.Key));
scheduler.ScheduleJob(x, someTrigger);
_pendingJobs.Add(x.Key);
}
scheduler.Start();
}
我在这里错过了什么吗?
答案 0 :(得分:3)
我明白了。
我错过了配置中的quartz.threadPool.type
密钥,该密钥是在Quartz.Simpl.ZeroSizeThreadPool, Quartz
上设置的(此Quartz配置主要用于Web环境)。当您的调度程序只想安排作业而不运行它们时,将使用此设置,将此任务留给作为服务运行的Quartz Server。 “使用非启动调度程序执行的作业”部分应该引导我朝这个方向发展。
将此密钥更改为Quartz.Simpl.SimpleThreadPool, Quartz
解决了问题。
答案 1 :(得分:0)
您是否正在运行 scheduler.Start()?我认为默认情况下会禁用调度程序,您需要明确启动它。