Implementing IJob In Quartz.net

This blog is for showcasing my way of implementing IJob interface of Quartz.net. IJob interface is needed for any class that needs to run in Quartz scheduler.

IJob interface has the following definition,
  1. namespace Quartz {  
  2.     //  
  3.     // Summary:  
  4.     // The interface to be implemented by classes which represent a 'job' to be performed.  
  5.     //  
  6.     // Remarks:  
  7.     // Instances of this interface must have a public no-argument constructor. Quartz.JobDataMap  
  8.     // provides a mechanism for 'instance member data' that may be required by some  
  9.     // implementations of this interface.  
  10.     public interface IJob {  
  11.         //  
  12.         // Summary:  
  13.         // Called by the Quartz.IScheduler when a Quartz.ITrigger fires that is associated  
  14.         // with the Quartz.IJob.  
  15.         //  
  16.         // Parameters:  
  17.         // context:  
  18.         // The execution context.  
  19.         //  
  20.         // Remarks:  
  21.         // The implementation may wish to set a result object on the JobExecutionContext  
  22.         // before this method exits. The result itself is meaningless to Quartz, but may  
  23.         // be informative to Quartz.IJobListeners or Quartz.ITriggerListeners that are watching  
  24.         // the job's execution.  
  25.         void Execute(IJobExecutionContext context);  
  26.     }  
  27. }  

If we try to take this interface and inherit it for other classes every class needs to implement Execute method which is not very manageable and brings a  redundancy of code. So in order to avoid it I created an abstract class which inherits from IJob and has the implemented Execute Method. Let us name it AbstractJob

  1. namespace Quartz_Net_Example {  
  2.     public abstract class AbstractJob: IJob {  
  3.         public abstract void PreProcessing(IJobExecutionContext context);  
  4.         public abstract void PostProcessing(IJobExecutionContext context);  
  5.         public void Execute(IJobExecutionContext context) {  
  6.             PreProcessing(context);  
  7.             // Implement you code to execute job.  
  8.             PostProcessing(context);  
  9.         }  
  10.     }  
  11. }  

Any job that inherits from Abstract Job needs to worry about execution of job and gets a standardized base class code. However it needs to override PreProcessing and Post Processing methods so that any changes can be incorporated. Another option is to make PreProcessing and PostProcessing virtual and any class inheriting it if it wants to override the default implementation can override it.

In this way I have tried to showcase how to implement IJob for Quartz.Net

Next Recommended Reading Onion Architecture In ASP.NET MVC