Simple, working example of Quartz.net

Tomer picture Tomer · Jan 11, 2012 · Viewed 40.6k times · Source

I am looking for a working simple example of Quartz.net for Console application (it can be any other application as long as it simple enough...). And while I am there, is there any wrapper that could help me avoid implementing IJobDetail, ITrigger, etc.

Answer

Bjørn Otto Vasbotten picture Bjørn Otto Vasbotten · Nov 13, 2013

There is a guy who made the exact same observation as you, and he has published a blog post with a simple working example of a Quartz.net Console application.

The following is a working Quartz.net example that is built against Quartz.net 2.0 (Latest). What this job does is write a text message, “Hello Job is executed” in the console every 5 sec.

Start a Visual Studio 2012 project. Select Windows Console Application. Name it Quartz1 or what ever you like.

Requirements Download Quartz.NET assembly using NuGet. Right click on project, select “Manage Nuget Packages”. Then search for Quartz.NET. Once found select and install.

using System;
using System.Collections.Generic;
using Quartz;
using Quartz.Impl;
     
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);
         
        }
    }
     
    /// <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, JOb executed");
        }
    }
} 

Sources