当前位置:网站首页>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-2 JSX syntax rules
- Milvus 2.0 質量保障系統詳解
- PostgreSQL column storage and row storage
- Decimal format decimal / datetime conversion processing
- C listens for WMI events
- Go language, array, string, slice
- Shell-入门、变量、以及基本的语法
- 【WPF绑定3】 ListView基础绑定和数据模板绑定
- Solution architect's small bag - 5 types of architecture diagrams
- VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
猜你喜欢
2.Electron之HelloWorld
【生活中的逻辑谬误】稻草人谬误和无力反驳不算证明
VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
【WPF绑定3】 ListView基础绑定和数据模板绑定
Milvus 2.0 质量保障系统详解
STM32__ 03 - beginner timer
RPC核心概念理解
Node access to Alipay open platform sandbox to achieve payment function
[PROJECT] small hat takeout (8)
随机推荐
Go language RPC communication
Further study of data visualization
How to implement distributed locks with redis?
Aiot industrial technology panoramic structure - Digital Architecture Design (8)
Use of shell cut command
【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
About stream flow, write it down briefly------
Bottom processing of stack memory in browser
Collect blog posts
Detailed explanation of Niuke - Gloves
oracle 中快速获取表的列名列表
1-3 nodejs installation list configuration and project environment
Redis docker installation
ClickHouse-SQL 操作
ASP. Net core reads the configuration file in the class library project
Signalr can actively send data from the server to the client
SQL database
VsCode-Go
Lock lock
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof