![]() In this example, we have set the WaitForJobsToComplete property to true so that the application will wait for all scheduled jobs to complete before shutting down. The AddQuartzHostedService method takes a configuration object that allows us to set properties such as whether or not to wait for jobs to complete before shutting down the application. This service is responsible for running the scheduled jobs. Adding the Quartz Hosted Serviceįinally, we need to add the Quartz hosted service to the container. The WithSimpleSchedule method takes a configuration object that allows us to set properties such as the repeat interval and repeat count. WithSimpleSchedule(x => x.WithIntervalInMinutes( 2).RepeatForever())) WithIdentity( "UpdateProductsFileJob-Trigger") q.AddTrigger(cfg => cfg.ForJob(updateProductsFileJobKey) In this example, we will be using a simple trigger that repeats every 2 minutes. There are various types of triggers available in Quartz.NET, such as simple triggers and cron triggers. A trigger is used to specify when the job should be executed. Once the job is registered, we can schedule a trigger for it. Q.AddJob(opt => opt.WithIdentity(updateProductsFileJobKey)) var updateProductsFileJobKey = new JobKey( nameof(UpdateProductsFileJob)) This method takes a configuration object that allows us to set properties such as the job's identity and triggers. To register the job with Quartz, we can use the AddJob method. _logger.LogInformation( "Message written to file.") _logger.LogInformation( "Writing message to file.") įile.WriteAllText( "products.txt", "Hello from Quartz.NET!") ![]() Public Task Execute( IJobExecutionContext context) ![]() Public UpdateProductsFileJob( ILogger logger) Here is an example of a job that writes a message to a file: public class UpdateProductsFileJob : IJob This class should implement the IJob interface from Quartz.NET. To schedule a job, we first need to define the job class. This allows for easy integration with other services in the application. The UseMicrosoftDependencyInjectionScopedJobFactory method tells Quartz to use the built-in dependency injection framework in ASP.NET Core for resolving job instances. ![]() Q.UseMicrosoftDependencyInjectionScopedJobFactory() Once this is done, we can add Quartz to the container by calling the AddQuartz method on the IServiceCollection object. To add Quartz.NET to the container, we first need to install the package. They can also be used to perform tasks that are too resource-intensive to be done in real-time, such as generating a large report or running a complex data analysis. These tasks are typically run asynchronously, meaning that they do not block the main thread of execution and do not prevent the user from interacting with the application.īackground jobs are commonly used in web applications to perform tasks that do not need to be done in real-time, such as sending a daily summary email or cleaning up old data in a database. It can be used to perform tasks such as sending emails, generating reports, or cleaning up data at a specific time or interval. What is a Background JobĪ background job, also known as a scheduled task or a cron job, is a task that runs in the background without the user's direct interaction. This library provides a powerful scheduling framework that can be easily integrated into an ASP.NET Core application using the package. ASP.NET Core allows for the scheduling of background jobs using the Quartz.NET library.
0 Comments
Leave a Reply. |