Wednesday, March 9, 2011

Scheduling Jobs Programmatically in Quartz.Net 2.0

NOTE: I'm now blogging at http://jayvilalta.com/blog and not updating this blog anymore. For information on the latest version of Quartz.Net, visit me there.

Scheduling jobs programmatically in Quartz.Net 2.0 is very similar to scheduling a job in Quartz.Net 1.0. The overall process is the same, but the scheduler API has changed for version 2.0, so the code used will be significantly different.
In this post we will only highlight the differences between the two versions, so if you’re not familiar with the process, then read this post first. Then come back to this post to understand what has changed in version 2.0.

What Has Changed

Only a couple of things have changed in Quartz.Net 2.0 when it comes to scheduling a job programmatically: the way you build jobs and triggers.
The new Quartz.Net scheduler API uses more interfaces now. It also provides builder objects to create triggers (ITrigger now) and job details (IJobDetail now). I think the simplest way to highlight the differences is to take the code from my original post and convert it to the new API. Here’s the updated code:
public void ScheduleOneTimeJob(Type jobType, JobDataMap dataMap)
{
  string name = DateTime.Now.ToString("yyyyMMddHHmmss"));
  string group = "OneTimeJobs";
  // create job
  IJobDetail jobDetail = JobBuilder
    .Create()
    .OfType(jobType)
    .WithIdentity(new JobKey(name, group))
    .UsingJobData(dataMap)
    .Build();

  // create trigger
  ITrigger trigger = TriggerBuilder
    .Create()
    .ForJob(jobDetail)
    .WithIdentity(new TriggerKey(name, group));
    .WithSchedule(SimpleScheduleBuilder.Create())
    .StartNow()
    .Build();
  // get reference to scheduler (remote or local) and schedule job
  GetScheduler().ScheduleJob(jobDetail, trigger);
}



As you can see, the changes to the code are pretty significant, but the overall process remains the same. I’d like to provide you with some links to documentation or some other examples, but I’m unaware of any at this point, as Quartz.Net 2.0 has not yet been released.

8 comments:

Fathom Savvy said...

Thanks J. But how do we create a SchedulerFactory and add Job and Trigger Listeners? Maybe next post...no worries if not.

J said...

Thanks for the idea. My next post will be about job and trigger listeners.

Eric van den Bos said...

Great work on this project. Unfortunately I get errors when I try to save a job. When I execute the code you provided in this sample I get "Job's key cannot be null" in Quartz.Impl.RemoteScheduler.ScheduleJob (on the line "return GetRemoteScheduler().ScheduleJob(jobDetail, trigger);"). The key of the jobDetail passed to this function is filled.
I have Quartz.Net running as a service on my local machine. Adding or updating triggers works just fine.
I also tried to use the AddJob method. That results in the error "Couldn't determine job existence (): Object reference not set to an instance of an object." Again the key is empty, although it is available in the jobDetail instance in the calling method. Any help will be appreciated.

J said...

Eric, are you using the latest code available on github or did you get a binary build from somewhere else?

Eric van den Bos said...

J, I used the latest code from github. I also tried again yesterday, as I saw that github had been updated two days ago. But I keep having this problem.
I had failed to mention that I was using a SQL database for holding the Quartz data. But yesterday, as I forgot to configure the service correctly after the update, I had the same problem with the RamJobStore methods.

J said...

Well, you've found a bug. That was working before... I get the same error you are getting now. I was trying to log an issue for it somewhere but I guess they've turned write access to the opensymphony JIRA off. I'll ask on the mailing list. Feel free to join it to find out what happens with this.

Eric van den Bos said...

Thanks for your confirmation and for redirecting me to the issue tracking and the mailing list. I thought the sourceforce site only dealt with Quartz.NET version 1.0, and didn't look too closely at the links there.

Anonymous said...

here is blog post i came across on this topic, it might me helpful in addition to the info already shared here. http://bit.ly/14z6067