Quartz.Net JobListener Example

Download Code – > Quartz_Net_Example

JobListeners in Quartz.NET are kind of event listeners but in t his case they listen to the JOB. They are executed when the job is about to be executed or when the JOB has executed. The following is a simple C# Console Application that demonstrates the concept. The main JOB simply prints a msg that says “Hello, this message is from the JOB”. The listeners also print messages to the console when the job is about to be executed and when the JOB has finished executing.

/* Example:     Job Listener in Quartz.NET
 * Explanation: Create a simple JOB that writes a text message to console application.
 *              Listens also print their message to console, before and after job execution.
 * Example by:  Hammad Khan
 * Copy Right:  You can use this program for tutorial and learning purposes. If you want copy it, please provide a linke to my blog http://www.hammadk.com
 *              
 */
using System;
using System.Collections.Generic;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;

namespace Quartz1
{
    class Program
    {
        static void Main(string[] args)
        {
            // construct a scheduler factory
            ISchedulerFactory schedFact = new StdSchedulerFactory();

            // get a scheduler, start the schedular before triggers or anything else
            IScheduler sched = schedFact.GetScheduler();
            sched.Start();

            // create job
            IJobDetail job = JobBuilder.Create<SimpleJob>()
                        .WithIdentity("job1", "group1")
                        .Build();

            // create trigger
            ITrigger trigger = TriggerBuilder.Create()
                .WithIdentity("trigger1", "group1")
                .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).RepeatForever())
                .Build();

            // Schedule the job using the job and trigger 
            sched.ScheduleJob(job, trigger);

            MyJobListener myJobListener = new MyJobListener();
            myJobListener.Name = "MyJobListener1";

            sched.ListenerManager.AddJobListener(myJobListener, KeyMatcher<JobKey>.KeyEquals(new JobKey("job1", "group1")));

        }
    }

    /// <summary>
    /// SimpleJOb is just a class that implements IJOB interface. It implements just one method, Execute method
    /// </summary>
    public class SimpleJob : IJob
    {
        void IJob.Execute(IJobExecutionContext context)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Hello, This message is from the JOB");
        }
    }

    public class MyJobListener:IJobListener
    {

        void IJobListener.JobExecutionVetoed(IJobExecutionContext context)
        {
            throw new NotImplementedException();
        }

        void IJobListener.JobToBeExecuted(IJobExecutionContext context)
        {
            Console.WriteLine("rnJob is about to be executed...");
        }

        void IJobListener.JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException)
        {
            Console.WriteLine("Job was executed...");
        }

        // this property is REQUIRED and must be SET before you use the joblistener.
        public string Name { get; set; }
    }
}

Note that you will need to download Quartz.NET library to run this example. Simply right click on project, select NUGET Packages, type Quartz and download the very first entry Quartz.NET.

Links: Job Listeners in Quartz.NET

This entry was posted in Tutorials/Tips and tagged . Bookmark the permalink.

1 Response to Quartz.Net JobListener Example

  1. Philippe says:

    Very clear simple example. All what I needed.
    Perfect.
    Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s