DisallowConcurrentExecution prevents Quartz.NET from starting another instance of a job while the same job definition is already running. It is especially useful for long-running jobs whose triggers fire frequently. section 34.3.2 section 34.4
Preventing overlapping exchange-rate updates
Decorate the Quartz.NET job with the attribute: [DisallowConcurrentExecution] public class UpdateExchangeRatesJob : IJob. If its trigger fires every five minutes while an update is still running, Quartz.NET waits instead of starting another instance. section 34.3.2 section 34.4
Listing 34.12 Configuring a Quartz.NET IJob and trigger
using Quartz;
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddQuartz(q =>
{
q. UseMicrosoftDependencyInjectionJobFactory();
var jobKey = new JobKey("Update exchange rates"); #1
q.AddJob<UpdateExchangeRatesJob>(opts => #2
opts.WithIdentity(jobKey)); #2
q.AddTrigger(opts => opts #3
.ForJob(jobKey) #3
.WithIdentity(jobKey.Name + " trigger") #4
.StartNow() #5
.WithSimpleSchedule(x => x #6
.WithInterval(TimeSpan.FromMinutes(5)) #6
.RepeatForever())
);
});
services.AddQuartzHostedService(
q => q.WaitForJobsToComplete = true);
})
.Build();
host.Run();
#1 Creates a unique key for the job, used to associate it with a trigger
#2 Adds the IJob to the DI container and associates it with the job key
#3 Registers a trigger for the IJob via the job key
#4 Provides a unique name for the trigger for use in logging and in clustered scenarios
#5 Fires the trigger as soon as the Quartz.NET scheduler runs on app startup
#6 Fires the trigger every 5 minutes until the app shuts down
Shows the Quartz.NET job registration and trigger configuration that determine how often a protected job is requested. section 34.3.2
