Scheduling a Quartz.Net Job Using Xml
Quartz.Net ships with a plugin that allows you to schedule jobs using an xml configuration file. By default this configuration file is called quartz_jobs.xml. In this post I will describe how to schedule a job with a cron trigger using this xml format.
Here are the contents of the XML file:
<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0">
<processing-directives>
<overwrite-existing-data>true</overwrite-existing-data>
</processing-directives>
<schedule>
<job>
<name>MyJob</name>
<group>MyJob</group>
<description>My Job</description>
<job-type>MyAssembly.MyJob, MyAssembly</job-type>
<durable>true</durable>
<recover>false</recover>
<job-data-map>
<entry>
<key>ConnectionString1</key>
<value>data source=dbserver;initial catalog=db1;user id=userid;pwd=password;</value>
</entry>
<entry>
<key>ConnectionString2</key>
<value>data source=dbserver;initial catalog=db2;user id=userid;pwd=password;</value>
</entry>
</job-data-map>
</job>
<trigger>
<cron>
<name>MyJob</name>
<group>MyJob</group>
<description>Cron trigger for MyJob</description>
<job-name>MyJob</job-name>
<job-group>MyJob</job-group>
<misfire-instruction>SmartPolicy</misfire-instruction>
<cron-expression>0 0 2 * * ?</cron-expression>
</cron>
</trigger>
</schedule>
</job-scheduling-data>
I’ll point out a few things of interest and we’ll call this post done, since as you can see the xml is quite self explanatory.
1. Notice the <job-data-map> element at line 14. This is how we can pass parameters to our job via the JobDataMap. This JobDataMap can be accessed from context. In this case we are passing 2 connection strings to our job so that it can do its thing.
2. Notice in the trigger element how we are creating a cron trigger on line 28, instead of a simple trigger. In this case, the trigger will fire every day at 2AM.
That’s it! This example illustrates how to schedule a job with a cron trigger and with some data being passed in to the JobDataMap.