Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of ASP.NET Core lightweight Job scheduling Job Task Framework

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the example analysis of ASP.NET Core lightweight job scheduling Job task framework, the content is very detailed, interested friends can refer to, hope to be helpful to you.

In the ASP.NET Core MVC/WebApi project, you need a task to be executed on time, one every few minutes, or every few hours, and so on. At this time, you need a regular function. The simplest thing is to write one with Timer yourself, but the performance you write may not be sound, and so on. Although ASP.NET Core now provides built-in Worker Service functions. However, job scheduling with complex time does not support it.

You may think of using the powerful Quartz.Net framework, why not choose it, mainly because it is too complex, it is not easy to get started quickly, and now there are more and more open source libraries, and the function is getting better and better. So Ben today recommends a lightweight job scheduling Job task framework, simple and powerful, very convenient to combine with ASP.NET Core, so what is its name.

At the end of the article, A Bun has encapsulated the sample code and used it right out of the box. You don't have to worry about how to perform multiple tasks in a project anymore.

II. Introduction to FluentScheduler

We are a group of people standing on the shoulders of giants. We also learn to use good things and directly borrowlism. FluentScheduler timing task library, through the nuget reference, you can set a variety of event intervals, super convenient and simple.

FluentScheduler is a simple task scheduling framework, which is very easy to use. The source code of the author and the address of the example:

Https://github.com/fluentscheduler/FluentScheduler

1. The first reference to the FluentScheduler.dll,dll data source is available through the NuGet package. Open the NuGet package that manages the solution and enter FluentScheduler.

Step: select a tool on the status bar-NuGet package Manager-manage the NuGet package for the solution, and then enter FluentScheduler.

III. Mode of use of FluentScheduler

Step one:

Register for the FluentSchedule service

Step 2:

Define the tasks we want to perform in Job. For example, here Aben uses two Job tasks, "order" and "mail", as examples.

Step 3:

Inherit the Registry class, which can be understood as the scheduled job scheduling time policy for the execution of the task. For example: cycle every 5 seconds, cycle every hour.

Step 4:

Inherit and implement the IJob interface, which can be understood as: what is the business logic of the specific task?

Step 5: direct F5 permission.

It is important to note that the NonReentrantAsDefault method is particularly useful: it means that each task must be completed before you can start the next execution, preventing problems caused by concurrent execution, that is, preventing repeated execution. The next task must wait until the last task is completed.

Here is a list of several job scheduling plans that we commonly use.

/ / Schedule an IJob to run at an interval

/ / perform scheduled tasks every 10 seconds immediately. (specify a time interval to run, according to your own needs, can be seconds, minutes, hours, days, months, years, etc. )

Schedule (). ToRunNow (). AndEvery (10). Seconds ()

/ / perform scheduled tasks every 10 seconds immediately. If this task is not finished, the next task will not start, and parallel running is prohibited.

Schedule (). Www.leyouzaixan.cnNonReentrant (). ToRunNow (). AndEvery (10). Seconds ()

/ / perform scheduled tasks at 21:15 every day

Schedule (() = > Console.WriteLine ("www.taohuayuan178.com It's 9:15 PM now.")) .ToRunEvery (1). Days (). At (21 PM now 15)

/ / immediately perform a scheduled task at 3:00 on the first Monday of each month

Schedule () = > Console.WriteLine ("It's 3:00 AM now."). ToRunNow (). AndEvery (1). Months (). OnTheFirst (DayOfWeek.Monday) .at (3mem0)

/ / perform scheduled tasks at 21:15 every Monday

Schedule (() = > Console.WriteLine ("It's 9:15 PM now.")) .ToRunEvery (1). Weeks (). On (DayOfWeek.Monday) .at (21 Magi 15)

The example analysis of ASP.NET Core lightweight job scheduling Job task framework is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report