2

I'm trying to get started with Quartz.Net 2.0. A very simple appearing test application is failing with a SchedulerException

Trigger's related Job's name cannot be null

The code is adapted from the Version 2.0 Migration Guide

ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler classSched = schedFact.GetScheduler();
classSched.Start();
IJobDetail job = JobBuilder.Create<ClassificationJob>()
                    .WithIdentity("myJob", "My Group")
                    .WithDescription("My Description")
                    .Build();

TimeZoneInfo tzUtc = TimeZoneInfo.Utc;
DateTime startTime;

startTime = DateTime.UtcNow;

ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("myTrigger", "My Group")
    .WithDescription("My Description")
    .StartAt(startTime)
    .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())
    .Build();

classSched.ScheduleJob(trigger); // Exception on this line

Why is this failing?

Eric J.
  • 147,927
  • 63
  • 340
  • 553

1 Answers1

7

I think there are two issues.

Firstly, (despite the example in the migration guide), I think that you need to say which job the trigger is related to, ie call the .ForJob method , for example

ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("myTrigger", "My Group")
    .WithDescription("My Description")
    .StartAt(startTime)
    .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())
    .ForJob(job)
    .Build();

Secondly, the syntax you are using to schedule the job only works if the job has previously been added (eg if you have previously called classSched.AddJob(job,true); // or classSched.AddJob(job,false);

If that hasn't already been done, then you need to use the syntax

  classSched.ScheduleJob(job, trigger); 
sgmoore
  • 15,694
  • 5
  • 43
  • 67
  • Doesn't requiring `ForJob(job)` belie the loose coupling between jobs and triggers that was an explicit design goal? I'll give your suggestions a try in a few minutes. – Eric J. Mar 26 '13 at 17:28
  • You could be right, but in that case if you simply call `classSched.ScheduleJob(trigger);` Quartz would have no idea which job would want to run. Perhaps you only need to change your last line to `classSched.ScheduleJob(job, trigger); ` – sgmoore Mar 26 '13 at 19:24
  • Turns out I only had to provide the `job` parameter to `ScheduleJob()`. – Eric J. Mar 26 '13 at 19:56