[英]How to start a scheduled job using Quartz.Net?
我創建了一個.Net 5 worker 服務應用程序並安裝了Quartz.AspNetCore
package。
我想運行基於 cron 表達式的代碼,例如每 5 分鍾一次。 我創建了一個 class MyJob
實現Ijob
並在 DI 設置期間注冊它
public static void Main(string[] args)
{
Host
.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services
.AddQuartz(quartzConfiguration =>
{
quartzConfiguration.ScheduleJob<MyJob>(jobConfiguration =>
{
jobConfiguration.WithCronSchedule("20/20 0 0 ? * * *"); // Every 20 seconds starting at second 20
});
})
.AddQuartzHostedService(quartzConfiguration => // I think this code is useful ?
{
quartzConfiguration.WaitForJobsToComplete = true;
})
.AddHostedService<Worker>();
})
.Build()
.Run();
}
但是作業本身永遠不會執行。 調用quartzConfiguration.AddJob<MyJob>()
時,我無法訪問該方法.WithCronSchedule()
。
那么如何告訴 Quartz 注冊一個作業並根據 cron 表達式執行它呢?
cron 表達式20/20 0 0? * * *
20/20 0 0? * * *
表示“每 20 秒,在前 20 秒之后,在第一個小時的第一分鍾。
所以基本上, 00:00:20
和00:00:40
,或午夜后 20 秒和午夜后 40 秒。
檢查這是否是您想要的 cron。 其他一切看起來都是正確的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.