Quartz Background Job Manager

Quartz is an advanced background job manager. You can integrate Quartz with the ABP Framework to use it instead of the default background job manager. In this way, you can use the same background job API for Quartz and your code will be independent of Quartz. If you like, you can directly use Quartz’s API, too.

See the background jobs document to learn how to use the background job system. This document only shows how to install and configure the Quartz integration.

Installation

It is suggested to use the ABP CLI to install this package.

Using the ABP CLI

Open a command line window in the folder of the project (.csproj file) and type the following command:

  1. abp add-package Volo.Abp.BackgroundJobs.Quartz

Manual Installation

If you want to manually install;

  1. Add the Volo.Abp.BackgroundJobs.Quartz NuGet package to your project:

    1. Install-Package Volo.Abp.BackgroundJobs.Quartz
  2. Add the AbpBackgroundJobsQuartzModule to the dependency list of your module:

  1. [DependsOn(
  2. //...other dependencies
  3. typeof(AbpBackgroundJobsQuartzModule) //Add the new module dependency
  4. )]
  5. public class YourModule : AbpModule
  6. {
  7. }

Configuration

Quartz is a very configurable library,and the ABP framework provides AbpQuartzOptions for this. You can use the PreConfigure method in your module class to pre-configure this option. ABP will use it when initializing the Quartz module. For example:

  1. [DependsOn(
  2. //...other dependencies
  3. typeof(AbpBackgroundJobsQuartzModule) //Add the new module dependency
  4. )]
  5. public class YourModule : AbpModule
  6. {
  7. public override void PreConfigureServices(ServiceConfigurationContext context)
  8. {
  9. var configuration = context.Services.GetConfiguration();
  10. PreConfigure<AbpQuartzOptions>(options =>
  11. {
  12. options.Properties = new NameValueCollection
  13. {
  14. ["quartz.jobStore.dataSource"] = "BackgroundJobsDemoApp",
  15. ["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
  16. ["quartz.jobStore.tablePrefix"] = "QRTZ_",
  17. ["quartz.serializer.type"] = "json",
  18. ["quartz.dataSource.BackgroundJobsDemoApp.connectionString"] = configuration.GetConnectionString("Quartz"),
  19. ["quartz.dataSource.BackgroundJobsDemoApp.provider"] = "SqlServer",
  20. ["quartz.jobStore.driverDelegateType"] = "Quartz.Impl.AdoJobStore.SqlServerDelegate, Quartz",
  21. };
  22. });
  23. }
  24. }

Starting from ABP 3.1 version, we have added Configurator to AbpQuartzOptions to configure Quartz. For example:

  1. [DependsOn(
  2. //...other dependencies
  3. typeof(AbpBackgroundJobsQuartzModule) //Add the new module dependency
  4. )]
  5. public class YourModule : AbpModule
  6. {
  7. public override void PreConfigureServices(ServiceConfigurationContext context)
  8. {
  9. var configuration = context.Services.GetConfiguration();
  10. PreConfigure<AbpQuartzOptions>(options =>
  11. {
  12. options.Configurator = configure =>
  13. {
  14. configure.UsePersistentStore(storeOptions =>
  15. {
  16. storeOptions.UseProperties = true;
  17. storeOptions.UseJsonSerializer();
  18. storeOptions.UseSqlServer(configuration.GetConnectionString("Quartz"));
  19. storeOptions.UseClustering(c =>
  20. {
  21. c.CheckinMisfireThreshold = TimeSpan.FromSeconds(20);
  22. c.CheckinInterval = TimeSpan.FromSeconds(10);
  23. });
  24. });
  25. };
  26. });
  27. }
  28. }

You can choose the way you favorite to configure Quaratz.

Quartz stores job and scheduling information in memory by default. In the example, we use the pre-configuration of options pattern to change it to the database. For more configuration of Quartz, please refer to the Quartz’s documentation.

Exception handling

Default exception handling strategy

When an exception occurs in the background job,ABP provide the default handling strategy retrying once every 3 seconds, up to 3 times. You can change the retry count and retry interval via AbpBackgroundJobQuartzOptions options:

  1. [DependsOn(
  2. //...other dependencies
  3. typeof(AbpBackgroundJobsQuartzModule) //Add the new module dependency
  4. )]
  5. public class YourModule : AbpModule
  6. {
  7. public override void ConfigureServices(ServiceConfigurationContext context)
  8. {
  9. Configure<AbpBackgroundJobQuartzOptions>(options =>
  10. {
  11. options.RetryCount = 1;
  12. options.RetryIntervalMillisecond = 1000;
  13. });
  14. }
  15. }

Customize exception handling strategy

You can customize the exception handling strategy via AbpBackgroundJobQuartzOptions options:

  1. [DependsOn(
  2. //...other dependencies
  3. typeof(AbpBackgroundJobsQuartzModule) //Add the new module dependency
  4. )]
  5. public class YourModule : AbpModule
  6. {
  7. public override void ConfigureServices(ServiceConfigurationContext context)
  8. {
  9. Configure<AbpBackgroundJobQuartzOptions>(options =>
  10. {
  11. options.RetryStrategy = async (retryIndex, executionContext, exception) =>
  12. {
  13. // customize exception handling
  14. };
  15. });
  16. }
  17. }