Thursday, December 24, 2015

Hello World Quartz Job

A few days back I needed to use Quartz for some task and I really struggled quite a bit to get running with my hello world quartz service. Now since I am up and running I thought to share and save an hour or two for someone else :)

Next are steps for creating a simple Hello World Quartz Job.

Step 1: Create a new windows service project in Visual Studio, you may also use an console application or whatever else you can think of :)



Step 2: Add Quartz to your project using nugget, you may add nugget reference or add quartz reference from somewhere else if you want. Doing nugget way is better it also automatically installs the required dependencies.














.

Step 3: Every quartz job has two important things, first is "what needs be done" and second is "when it needs be done"

To tackle first part that is "what needs be done", we need to create a class file to define Job. So add a new .cs file and name the class whatever your want. For this sample I'll take it as SampleJob. This class needs to implement IJob interface present in Quartz namespace.















Execute method as shown in above image comes when you implement IJob interface. Here in this sample I simply added a line to append some text to a text file.

To tackle second part that is "when it needs be done", check step 4

Step 4: Quartz provide two ways to schedule jobs, one is through code and other is using XML. Second approach is what I'll go with as I find it more useful. Here you will add an XML file and specify which job needs be executed and when it needs be triggered.



























In this xml file we do define job schedule, that is when a job needs be executed. For each job you want to schedule add one schedule node. Each schedule node will have their respective job and trigger nodes.

Under job node we have job-type node which wasted loads of time and prompted me to write this post. In this node we have two comma separated values, first one is class name of job that you want to schedule and second value is assembly name. Be careful here I firstly added namespace of Service class file with class name and banged my head for quite some time before figuring out why my jobs were not running.


Step 5: Next is to wire-up quartz scheduler with windows service/console app (or whatever else you are using), just add three lines in OnStart method as shown in below image












Step 6: For scheduling jobs using XML file you need to do a few configurations in App.config file, below are those settings













Value of setting with name quartz.plugin.xml.fileNames is path of jobs xml file(one that we created in step 4 above).

Now you are all set from coding side of things and ready to install your service. You may use whatever way you want to deploy windows service, I used installutil.exe utility for the same.

using installutil.exe:

1. Build your project
2. Open visual studio command prompt
3. Execute command
    installutil.exe
4. Now open service manager console using services.msc command in run box
5. Find your service and click start

That's it, you are done and your small hello world quartz job is up and running.


Merry Christmas :)

About Me

My photo
Delhi, India
Fun, music, travel and nature loving, always smiling, computer addict!!