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>
    &lt;/job&gt;

    &lt;trigger&gt;
        &lt;cron&gt;
            &lt;name&gt;MyJob&lt;/name&gt;
            &lt;group&gt;MyJob&lt;/group&gt;
            &lt;description&gt;Cron trigger for MyJob&lt;/description&gt;
            &lt;job-name&gt;MyJob&lt;/job-name&gt;
            &lt;job-group&gt;MyJob&lt;/job-group&gt;
            &lt;misfire-instruction&gt;SmartPolicy&lt;/misfire-instruction&gt;
            &lt;cron-expression&gt;0 0 2 * * ?&lt;/cron-expression&gt;
        &lt;/cron&gt;
    &lt;/trigger&gt;
&lt;/schedule&gt;

</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.