当前位置:网站首页>Using quartz under. Net core -- a simple trigger of [7] operation and trigger
Using quartz under. Net core -- a simple trigger of [7] operation and trigger
2022-04-23 17:11:00 【Tomato Warrior】
1、 Set up triggers for a specific time , Don't repeat
// Execute once at a certain point in time , No repetition
ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.StartAt(DateTime.Parse("2020-10-27 15:41"))
.Build();
2、 every other 3 Second trigger once , Repeat three times ( Trigger 4 Time )
// every other 3 Second trigger once , Repeat three times
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger3", "group1")
.StartAt(DateTime.Parse("2020-10-27 15:48")) // If you don't write the start time , Will start at the current time
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(3)
.WithRepeatCount(3)) // Written 3 Time , But it triggers 4 Time ( Count the first time )
.Build();
3、 some time 5 Second trigger once
// some time 5 Second trigger once
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger5", "group1")
.StartAt(DateBuilder.FutureDate(5, IntervalUnit.Second))
.Build();
4、 Every time 5s Do it once , until 22 spot
// Every time 5s Do it once , until 22 spot
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger7", "group1")
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(5)
.RepeatForever())
.EndAt(DateBuilder.DateOf(22, 0, 0))
.Build();
5、 Start with the head in the next minute , Every time 5 Once per second
// Start with the head in the next minute , Every time 5 Once per second
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger8")
.StartAt(DateBuilder.EvenMinuteDate(null))
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(5)
.RepeatForever())
.Build();
Complete code :
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
// Register scheduler factory
services.AddSingleton<ISchedulerFactory>(new StdSchedulerFactory());
}
HomeController.cs
using Microsoft.AspNetCore.Mvc;
using Quartz;
using Quartz.Impl.Calendar;
using System;
using System.Threading.Tasks;
namespace QuartzLearn.Controllers
{
public class HomeController : Controller
{
// Dispatcher factory
private readonly ISchedulerFactory _schedulerFactory;
// Constructor injection
public HomeController(ISchedulerFactory schedulerFactory)
{
// Injection scheduler factory
_schedulerFactory = schedulerFactory;
}
public async Task<IActionResult> Index()
{
IScheduler scheduler = await _schedulerFactory.GetScheduler();
await scheduler.Start();
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("job1", "jobGroup1")
.UsingJobData("name", "zhangsan")
.Build();
// Execute once at a certain point in time , No repetition
//ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create()
// .WithIdentity("trigger1", "group1")
// .StartAt(DateTime.Parse("2020-10-27 15:41"))
// .Build();
// every other 3 Second trigger once , Repeat three times
//ITrigger trigger = TriggerBuilder.Create()
// .WithIdentity("trigger3", "group1")
// .StartAt(DateTime.Parse("2020-10-27 15:48")) // If you don't write the start time , Will start at the current time
// .WithSimpleSchedule(x => x
// .WithIntervalInSeconds(3)
// .WithRepeatCount(3)) // Written 3 Time , But it triggers 4 Time ( Count the first time )
// .Build();
// some time 5 Second trigger once
//ITrigger trigger = TriggerBuilder.Create()
// .WithIdentity("trigger5", "group1")
// .StartAt(DateBuilder.FutureDate(5, IntervalUnit.Second))
// .Build();
// Every time 5s Do it once , until 22 spot
//ITrigger trigger = TriggerBuilder.Create()
// .WithIdentity("trigger7", "group1")
// .WithSimpleSchedule(x => x
// .WithIntervalInSeconds(5)
// .RepeatForever())
// .EndAt(DateBuilder.DateOf(22, 0, 0))
// .Build();
// Start with the head in the next minute , Every time 5 Once per second
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger8")
.StartAt(DateBuilder.EvenMinuteDate(null))
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(5)
.RepeatForever())
.Build();
await scheduler.ScheduleJob(job, trigger);
return View();
}
}
[DisallowConcurrentExecution]
[PersistJobDataAfterExecution]
public class HelloJob : IJob
{
public string Name { private get; set; }
public async Task Execute(IJobExecutionContext context)
{
await Console.Out.WriteLineAsync($"This is HelloJob {DateTime.Now}");
}
}
}
版权声明
本文为[Tomato Warrior]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230553457692.html
边栏推荐
猜你喜欢

1-1 NodeJS

快时钟同步慢时钟域下的异步控制信号slow clk to fast clk

SQL database

Node access to Alipay open platform sandbox to achieve payment function

Quick install mongodb
![[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof](/img/71/14a17128dbe0f02edb4db3da479ef2.jpg)
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
![[WPF binding 3] listview basic binding and data template binding](/img/2e/fbdb4175297bb4964a8ccfd0b909ae.png)
[WPF binding 3] listview basic binding and data template binding

Shell脚本——Shell编程规范及变量

VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN

Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
随机推荐
Use of shell cut command
Shell-sort命令的使用
Lock锁
Nacos + aspnetcore + Ocelot actual combat code
Some problems encountered in recent programming 2021 / 9 / 8
Basic case of Baidu map
Getting started with JDBC
Variable length parameter__ VA_ ARGS__ Macro definitions for and logging
JS to find the character that appears three times in the string
Input file upload
1-1 NodeJS
websocket
Shell - introduction, variables, and basic syntax
Interface document yaml
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof
Self use learning notes - connectingstring configuration
freeCodeCamp----shape_ Calculator exercise
Nifi fast installation and file synchronization
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
网络安全之渗透靶场实战详解