If you use Quartz.Net and you want to do anything other than run a batch file, then chances are you’ll want to create a custom job. Fortunately, creating it is not hard at all. Let’s get started.
Here’s a quick rundown of what you need to do. First, you’ll need to add a reference to Quartz.dll in your project, Second, create a class that implements IJob. Third, put the code you want to run inside the Execute method. That’s it! That’s all you really need to create a custom job.
However, in order to get this job to execute, there are a couple more steps to follow and a few more things to be aware of, including some not-so-evident gotchas.
I’m going to assume that at this point, you have created your MyJob class and that it implements the IJob interface. Let’s talk about what you need to do in addition to implementing the logic that you want your job to execute.
Handle Exceptions
You should wrap the code in the Execute method and handle any exceptions that you can. For exceptions that you cannot handle but that can be solved by running the job again, you should wrap your exception inside a JobExecutionException.If you want the scheduler to try running the job again, then set the RefireImmediately property to true. Otherwise, set it to false. You also have other options, such as un-scheduling triggers, so take a look at the documentation for JobExecutionException.
If your job doesn’t know how to handle the exception that was thrown, then don’t catch it and let the scheduler handle it.
Deploy Your Custom Job
Deploying the custom job is as simple as copying the dll to the same folder where the Quartz.dll is. You can also deploy your dll in any manner that allows the runtime to locate the dll per the normal rules for dll resolution.Beware of ConfigurationManager
Finally, I’d like to give you a heads up about using the ConfigurationManager to provide your job with configuration information. Don’t do it! It is best to include all the information that your job needs to execute in the job’s JobDataMap. The reason for this is simple. If you use ConfigurationManager and you are running Quartz.Net as a windows service, then the ConfigurationManager will try to look for configuration information in the Quartz.Net config file. If you are running Quartz.Net embedded inside your application, then this might not be an issue for you, but then again… it might.Let’s wrap up this post by summarizing the steps needed to create a custom Quartz.Net job.
- Add a reference to Quartz.dll in your project.
- Create a class that implements IJob
- Implement the Execute method of the IJob interface.
- Catch exceptions and throw a JobExecutionException as needed.
- Copy the CustomJob dll to the same folder as Quartz.dll or somewhere that the runtime can locate it.
6 comments:
The series of blogs you have written on quartz.net are best we can get in Internet. It is so simple to follow. Thanks very much for your effort.
Just one another request, if you can write another blog on adoJobStore, it will be great. I will be eagerly waiting for your blog. Thanks again.:)
I'll write one up this weekend. Sorry for the delayed response. For some reason I wasn't notified that a comment was posted...
Hi i am not clear on deployment and invoking the job from the Quartz.net Service.
Your blog posts are definitely much better than the tutorials or any other resource online.
Using your first post I setup Quartz as as windows service. Now reading this post of yours I think creating a custom job works best for me.
The question I have, however, is during deployment how can I control the configuration of any new jobs? I suppose I can deploy the quartz_jobs.xml file as well but if the windows service is already running then how will the new jobs get picked up?
Is it also possible to put the custom job assemblies in another folder? I will be ftping the assemblies and so wanted to see if there is an option to to deploy to a separate directory for security reasons.
I think if you add a post to show how Quartz.NET could be run embedded in ASP.NET with possibly solutions for the worker process recycling issue then this blog may as well become an authoritative source for all Quartz.NET information.
It isn't clear to me on how to get the custom job to be invoked by the scheduler.
My base scenario is an assembly full of classes that each implement IJOB.
How do I then relate each one of these classes to a job/group in the config file?
Do any of these answer your question: http://jvilalta.blogspot.com/2011/03/scheduling-jobs-programmatically-in.html or http://jvilalta.blogspot.com/2009/04/getting-started-with-quartznet-part-3.html
Post a Comment