Documentation

Quartz Integration

    Introduction

    Quartz is a is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems. Abp.Quartz package simply integrates Quartz to ASP.NET Boilerplate.

    ASP.NET Boilerplate has a built-in persistent background job queue and background worker system. Quartz can be a good alternative if you have advanced scheduling requirements for your background workers. Also, Hangfire can be a good alternative for persistent background job queue.

    Installation

    Install Abp.Quartz nuget package to your project and add a DependsOn attribute to your module for AbpQuartzModule:

    [DependsOn(typeof (AbpQuartzModule))]
    public class YourModule : AbpModule
    {
        //...
    }

    Creating Jobs

    To create a new job, you can either implement Quartz's IJob interface, or derive from JobBase class (defined in Abp.Quartz package) that has some helper properties/methods (for logging and localization for example). A simple job class is shown below:

    public class MyLogJob : JobBase, ITransientDependency
    {
        public override void Execute(IJobExecutionContext context)
        {
            Logger.Info("Executed MyLogJob :)");
        }
    }

    We simply implemented the Execute method to write a log. You can see Quartz's documentation for more.

    Schedule Jobs

    IQuartzScheduleJobManager is used to schedule jobs. You can inject it to your class (or you can Resolve and use it in your module's PostInitialize method) to schedule jobs. An example Controller that schedules a job:

    public class HomeController : AbpController
    {
        private readonly IQuartzScheduleJobManager _jobManager;
    
        public HomeController(IQuartzScheduleJobManager jobManager)
        {
            _jobManager = jobManager;
        }
            
        public async Task<ActionResult> ScheduleJob()
        {
            await _jobManager.ScheduleAsync<MyLogJob>(
                job =>
                {
                    job.WithIdentity("MyLogJobIdentity", "MyGroup")
                        .WithDescription("A job to simply write logs.");
                },
                trigger =>
                {
                    trigger.StartNow()
                        .WithSimpleSchedule(schedule =>
                        {
                            schedule.RepeatForever()
                                .WithIntervalInSeconds(5)
                                .Build();
                        });
                });
    
            return Content("OK, scheduled!");
        }
    }   

    More

    Please see Quartz's documentation for more information about Quartz.