Quartz Enterprise Scheduler .NET (Mail Notification)
Join the DZone community and get the full member experience.
Join For FreeIn my article, I’ll talk about Quartz Enterprise Scheduler .NET which is quite a popular library for scheduled tasks (CRON with the name in unix) and do a great sample project using this library.
Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems. Basically this structure occurs two phase.Firstly you must specify interval in which the job will run on. It is used cron expressions for this, you can retrieve more information here. Secondly, you must specify jobs (tasks) to work. This jobs (namely tasks) can be web services or executable files. Windows Task Scheduler providers this mechanism on the windows. You can access it via control panel:
Let’s take a look at a simple scheduled task with Quartz.NET
// nuget package http://www.nuget.org/packages/Quartz/ // http://www.quartz-scheduler.net/ public class SampleJob : IJob { public void Execute(IJobExecutionContext context) { Console.WriteLine("OK"); } } public class SampleJobExecuter { public virtual void Run() { // Scheduler // First we must get a reference to a scheduler ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); IScheduler scheduler = schedulerFactory.GetScheduler(); // Job IJobDetail job = JobBuilder.Create() //.WithIdentity("jobx", "groupx") .Build(); // Trigger // http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/crontrigger to CronSchedule ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("triggerx1", "groupx1") .WithCronSchedule("0/10 * * * * ?") .Build(); scheduler.ScheduleJob(job, trigger); scheduler.Start(); try { // wait five minutes to show jobs Thread.Sleep(10000); // executing... } catch (ThreadInterruptedException) { } scheduler.Shutdown(); } }
Sample business scenarios for scheduled jobs
- Reading from a database of payment details in international money transfers and creation output files (payment files) of it
- Creation of credit card details and sending to related customers
- Creation of daily, monthly, yearly report data and files
- Executing written script for database archiving and cleaning works
- Watching directories of error files generated by the system and sending mail to developer team
- Saving database daily of data generated by Google Analytics
- Running of mail-sms engines for system notifications
- Automatically changing system user accounts once every 6 months and sending mail to administrations
You can add Windows Task Scheduler and so on to many of the jobs above. If you want to include these type operations in the windows task scheduler, you must design as executable file (.exe). The above mentioned scenarios are enterprise and real life samples. Companies mostly design as web service to these and similar scheduled jobs and code web-based task scheduler (in other words web-based cron) for it. This tool is a great sample for enterprise software tools
Sample Project
Here’s our sample scenario: We don’t want to directly send to mail. Because system life can not continue when error occurred while sending mail. If performed operation is within a transaction, all operations can be rollback. This is not a desirable case for ideal system. Thus we are creating a table for mail notification or general notifications (sms). We’re saving these table mail details when you want to send mail to someone. A controller mechanism send specif periodically (10 seconds) active mail records in table, the status of each sent mail record set passive.This prevent sending mail in next control
Let’s try to explain this sample. System consists of 5 libraries:
- Notifier.DataAccess: I used RavenDb which I talked about before and it became the NoSQL database for data access layer. It contains classes about it in this library and I configured it as an embedded database.
- Notifier.Scheduler: This library contains related classes Quartz.NET that we mentioned above. I want to do more generic this library
- Notifier: It’s main library. This contains helper extension classes (i.e : sending mail, validating URL), repository classes for data access, notification controller class for mail table’s checking and data models.
- Notifier.Server.WebController: It reads active records (namely unsent mail records) in mail table then send mail and update status of record. It’s designed as a web service for this.
- Notifier.Server.WindowsController: This library designed as Windows service, unlike Notifier.Server.WebController. It’s not necessary, but optional. You can easily include this library into Windows task scheduler.
You can access codes of this sample project via this github repository. Also below you will see generic scheduler class that I coded with Quartz.NET:
public abstract class BaseNotifierJobExecuter : INotifierJobExecuter where TJob : IJob { private ISchedulerFactory schedulerFactory = new StdSchedulerFactory(); public IScheduler scheduler = null; protected IJob jobAction = null; protected string CronExpression = String.Empty; protected string jobKey = String.Empty; protected string jobGroupKey = String.Empty; protected string triggerKey = String.Empty; protected string triggerGroupKey = String.Empty; public BaseNotifierJobExecuter() { this.scheduler = schedulerFactory.GetScheduler(); } public virtual void StartJob() { //this.scheduler.Start(); this.scheduler.WithTryCatch(scheduler.Start); } public virtual void StopJob() { //this.scheduler.Shutdown(); this.scheduler.WithTryCatch(scheduler.Shutdown); } public abstract void ScheduleIt(string CronExpression, string jobKey = null, string jobGroupKey = null, string triggerKey = null, string triggerGroupKey = null); public virtual void Execute() { StartJob(); ScheduleIt(this.CronExpression, this.jobKey, this.jobGroupKey, this.triggerKey, this.triggerGroupKey); StopJob(); } } public class NotifierJobExecuter : BaseNotifierJobExecuter where TJob : IJob { private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); public override void ScheduleIt(string CronExpression, string jobKey = null, string jobGroupKey = null, string triggerKey = null, string triggerGroupKey = null) { base.CronExpression = CronExpression; base.jobKey = String.IsNullOrEmpty(jobKey) ? Guid.NewGuid().ToString() : jobKey; base.jobGroupKey = String.IsNullOrEmpty(jobGroupKey) ? Guid.NewGuid().ToString() : jobGroupKey; base.triggerKey = String.IsNullOrEmpty(triggerKey) ? Guid.NewGuid().ToString() : triggerKey; base.triggerGroupKey = String.IsNullOrEmpty(triggerGroupKey) ? Guid.NewGuid().ToString() : triggerGroupKey; if (base.jobAction.Equals(null)) { log.Error(""); throw new ApplicationException(""); } if (String.IsNullOrEmpty(base.CronExpression)) { log.Error(""); throw new ApplicationException(""); } // Job IJobDetail job = JobBuilder.Create() .WithIdentity(jobKey, jobGroupKey) .Build(); // Trigger // http://www.quartz-scheduler.org/documentation/quartz-2.2.x/tutorials/crontrigger to CronSchedule // http://www.cronmaker.com ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity(triggerKey, triggerGroupKey) .WithCronSchedule(CronExpression) .Build(); try { scheduler.ScheduleJob(job, trigger); } catch (Exception) { log.Error(""); throw new ApplicationException(""); } } }
Let’s code: Code in following sample scenario
I want to code web-based cron you. In here, supported job types will be the following: exe file, web service, a known database information stored procedure. Additionally, we’ll be able to load .dll file inside system and list void methods in it. Then we select one of the methods here and this method runs automatically.
Related links
- http://www.cronjobservices.com
- http://postcron.com
- http://www.schedulix.org/en
- https://github.com/quartznet/quartznet
- http://www.quartz-scheduler.net
- http://quartz-scheduler.org
- http://www.visualcron.com
- http://juiceboxjobs.com/index.html
- http://www.skybotsoftware.com/automated-operations-products/skybot-scheduler
- http://forum.shiftdelete.net/nasil-yapilir/268433-ucretsiz-ve-sinirsiz-cron-jobs-google-docs.html
I hope it may be useful, see you in another post.
Opinions expressed by DZone contributors are their own.
Trending
-
Scaling Site Reliability Engineering (SRE) Teams the Right Way
-
Never Use Credentials in a CI/CD Pipeline Again
-
How To Integrate Microsoft Team With Cypress Cloud
-
Transactional Outbox Patterns Step by Step With Spring and Kotlin
Comments