Scheduled Tasks in Asp.Net with Quartz.Net

In this post i will show you how to create a scheduled tasks easily with Quartz.Net library.As a first step we will create  an empty asp.net web application.

We can manually download quartz library dll and add as a reference or we can use nuget package manager to install necessary files. You can open nuget package manager console like below image.

packagemanager

Package manager console is very helpful to manage packages and we just need to type “Install-Package Quartz” and hit enter to download necessary dll files, when you see message like “Successfully installed ‘Quartz” that means dll files are downloaded and added as a reference to your project.

Now we have Quartz.Net but how we will implement this library to our project?

Answer is very easy because we just need to implement “IJob” Interface’s  Execute method in our class.

I am personally using Quartz for sending push notification to my mobile apps, basically quartz runs every hour and checks database for news,  if there is something new then system sends a push notification to mobile devices.

Quartz working very well on this process. 🙂

In above scenerio i’ve created a Push class with below code

using Quartz;
using System;


namespace QuartzNetScheduledTask.App_Code
{
    public class Push :IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            //This method will run every 10 seconds which set in JobScheduler 
            //class with following code
            //ITrigger trigger = TriggerBuilder.Create()
            //.WithIdentity("trigger1", "group1")
            //.StartNow()
            //.WithSimpleSchedule(x => x
            //.WithIntervalInSeconds(10)
            //.RepeatForever())
            //.Build();
        }
    }
}

As you can see in above code , Push class inherited from IJob interface and implementing Execute method of interface. We wrote our codes in execute method to run when task executed but we didn’t do  anything about task settings therefore i’ve created JobScheduler class  to set task settings with following codes.

using Quartz;
using Quartz.Impl;
using System;

namespace QuartzNetScheduledTask.App_Code
{
    public class JobScheduler
    {
        public static void Start()
        {
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();

            IJobDetail job = JobBuilder.Create<Push>().Build();

            ITrigger trigger = TriggerBuilder.Create()
            .WithIdentity("trigger", "group")
            .StartNow()
            .WithSimpleSchedule(x => x
            .WithIntervalInSeconds(10)
            .RepeatForever())
            .Build();

            scheduler.ScheduleJob(job, trigger);
        }
    }
}

At above code we said to quartz , we want  tasks to start now , repeat every 10 second to  forever, you can use your own settings to configure your tasks.

As last step , we will add a global.asax file to start Quartz in Application_Start method like below codes

using QuartzNetScheduledTask.App_Code;
using System;

namespace QuartzNetScheduledTask
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            JobScheduler.Start();
        }
    }
}

Finally we are done. You can change execute method and test in your machine.

You can find source code here : https://github.com/hamdiceylan/QuartzNetScheduledTask

I hope quartz helps you on your projects ,see you on next tutorial.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *