Click here to Skip to main content
13,260,108 members (48,794 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 7 Jun 2012

Quartz.Net - Custom Base Job

, 7 Jun 2012
Rate this:
Please Sign up or sign in to vote.
Creating a custom base job class for quartz scheduler development


Quartz.Net allows you to schedule jobs to run. These jobs are often configured with variables from a JobDataMap object. I wanted a simple and effective class that will allow me to use .NET properties instead of relying on configuring and setting up a JobDataMap when I needed to schedule a job.


An important aspect that I wanted to address was that although the Quartz.Net JobDataMap class will allow you to store objects, I only wanted to store string values. The problem is that if you store .net objects in the JobDataMap, you will also run the risk of running into type or versioning issues.

Taking advantage of the Json.NET framework, I built a simple QuartzJobBase class that allowed me to only store string values but also allow me to store more complex objects in the JobDataMap.

Using the code   

Before you start, make sure you are referencing the Json.NET assembly.

The QuartzJobBase class is an abstract implementation of a Quartz.Net job. It is in charge of 3 important aspects.

  1. Serializing current property values into a JobDataMap
  2. Deserializing property values from a JobDataMap
  3. Wrapping exceptions thrown by implementations with a JobExecutionException.

To seralize proeprty values in a JobDataMap, you can simple call  BuildJobDataMap() on your object to build a JobDataMap instance with your current property values.

Deserializing the object is being handled automatically by the class because it is executed when the job is executed by the Quartz scheduler.

using System;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;

namespace Quartz.Custom
    public abstract class QuartzJobBase : IJob
        private static readonly Newtonsoft.Json.JsonSerializerSettings JsonSettings;
        static QuartzJobBase()
            JsonSettings = new JsonSerializerSettings();
            JsonSettings.TypeNameHandling = TypeNameHandling.Auto;
            JsonSettings.TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;

        public void Execute(IJobExecutionContext context)
            catch (Exception ex)
                // Jobs should throw JobExecutionException if error occurred.
                // Wrap internal exceptions with JobExecutionException.
                JobExecutionException jex = new JobExecutionException(ex);
                throw jex;
        protected abstract void InternalExecute(IJobExecutionContext context);

        #region JobDataMap & Serialization
        public JobDataMap BuildJobDataMap()
            JobDataMap data = new JobDataMap();

            foreach (var prop in GetType().GetProperties())
                object value = prop.GetValue(this, null);
                string s = GetPropertyValue(prop);
                data.Add(prop.Name, s);

            return data;
        private void ReadFromJobDataMap(JobDataMap data)
            PropertyInfo[] properties = GetType().GetProperties();

            foreach (var key in data.Keys)
                var p = properties.Where(x => x.Name == key).SingleOrDefault();

                if (p != null)
                    SetPropertyValue(p, data.GetString(key));

        private string GetPropertyValue(PropertyInfo property)
            object value = property.GetValue(this, null);
            return JsonConvert.SerializeObject(value, Formatting.None, JsonSettings);
        private void SetPropertyValue(PropertyInfo property, string value)
            object obj = JsonConvert.DeserializeObject(value, property.PropertyType, JsonSettings);
            property.SetValue(this, obj, null);


A sample job implementation would look like this.  

public class HelloWorldJob : QuartzJobBase
       public string Name { get; set; }
       public int FavoriteNumber { get; set; }
       public List<string> CustomList { get; private set; }

       public HelloWorldJob()
           CustomList = new List<string>();

       protected override void InternalExecute(Quartz.IJobExecutionContext context)
           Console.WriteLine("Hello World from {0} ({1})", Name, FavoriteNumber);

           foreach (var item in CustomList)
               Console.WriteLine("{0} CUSTOM LIST: {1}", Name, item);

To schedule a job by providing a JobDataMap

HelloWorldJob j = new HelloWorldJob();
j.Name = "My Name";
j.FavoriteNumber = r.Next(100);
for (int x = 0; x < 10; x++)




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
United States United States
I'm a proud father and a software developer. I'm fascinated by a few particular .Net projects such as Lucene.Net, NHibernate, Quartz.Net, and others. I love learning and studying code to learn how other people solve software problems.

You may also be interested in...

Comments and Discussions

QuestionAbout 2.0 trigger stop problem Pin
jiangmm28-Aug-12 23:28
memberjiangmm28-Aug-12 23:28 
AnswerRe: About 2.0 trigger stop problem Pin
hahaclj29-Aug-12 0:01
memberhahaclj29-Aug-12 0:01 
GeneralRe: About 2.0 trigger stop problem Pin
jiangmm29-Aug-12 1:49
memberjiangmm29-Aug-12 1:49 
QuestionMy vote of 5 Pin
bobfox10-Jun-12 13:06
memberbobfox10-Jun-12 13:06 
AnswerRe: My vote of 5 Pin
AndrewSmith11-Jun-12 18:19
memberAndrewSmith11-Jun-12 18:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 7 Jun 2012
Article Copyright 2012 by AndrewSmith
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid