作业调度无效

时间:2014-11-05 09:40:24

标签: c# quartz.net job-scheduling

我想改变其中一个工作的触发器。所以我要做的是首先看看我的工作是否有任何与之相关的触发器。如果是,我Reschedule使用用户选择的新触发器。但是当我查看我的AdoJobStore时,新的触发器不存在,并且重新安排后作业将停止运行。

我已将整个代码放在try-catch块中,但我没有收到任何Exception,但这可能是因为我应该抓住JobExecutionException而不是Exception 1}} S'

var jobKey = new JobKey(jobName, jobGroup);
IScheduler sched = scheduler.GetScheduler();
IList<ITrigger> triggers = sched.GetTriggersOfJob(jobKey);
if (triggers.Count != 0)
{
    ITrigger existingTrigger = triggers[0];
    sched.UnscheduleJob(existingTrigger.Key);
    ITrigger newTrigger = BuildTrigger(triggerName, triggerGroup);
    IJobDetail job = sched.GetJobDetail(jobKey);
    DialogResult dr = MsgBox.Show(string.Format("Confirm campaign '{0}' with the schedule '{1}'?", lblCampaignName.Text, txtbxMessage.Text), "Confirm Schedule", MsgBox.Buttons.YesNo, MsgBox.Icon.Question);
    if (dr == DialogResult.Yes)
    {

        sched.RescheduleJob(existingTrigger.Key, newTrigger);
        int updateResult = UpdateCampaignSchedule();
        if (updateResult == 1)
            MsgBox.Show("Campaign schedule successfully updated!", "Edit schedule", MsgBox.Buttons.OK, MsgBox.Icon.Info);
        else
            MsgBox.Show("Unable to update campaign schedule in the database.", "Edit schedule", MsgBox.Buttons.OK, MsgBox.Icon.Error);
    }
}
else
{
..
}

1 个答案:

答案 0 :(得分:0)

这是因为呼叫sched.UnscheduleJob(existingTrigger.Key。这会从作业存储中删除触发器。因此,当调用sched.RescheduleJob(existingTrigger.Key, newTrigger) quartz无法找到旧的触发器来替换它时(如果查看返回值它将为null)也不会抛出异常,除非你的新触发器无效。

删除sched.UnscheduleJob(existingTrigger.Key, newTrigger,它应该有用。