Quartz.net-控制檯例項-Program
Quartz.net-控制檯例項-Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Quartz;
using Quartz.Impl;
namespace QuartzConsole
{
class Program
{
static void Main(string[] args)
{
// Grab the Scheduler instance from the Factory
IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
// and start it off
scheduler.Start();
//使用簡單觸發器
SimpleTriggers(scheduler);
//使用cron表示式觸發器
CronTriggers(scheduler);
Console.WriteLine("排程已經構建完成,按任意鍵退出應用程式...");
Console.ReadKey();
scheduler.Shutdown();
}
/// <summary>
/// 使用簡單觸發器
/// </summary>
static void SimpleTriggers(IScheduler scheduler)
{
try
{
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
.WithIdentity("job1", "group1")
.Build();
Console.WriteLine("構建簡單觸發器.開始..");
// Trigger the job to run now, and then repeat every 10 seconds
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "group1")
.UsingJobData("note", "簡單觸發器") //向觸發器資料字典中新增資料 key 不能重複
//.StartNow() //馬上執行
.StartAt(DateBuilder.FutureDate(2, IntervalUnit.Second)) //等待2秒後執行
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(2)//每次間隔1秒
//.WithRepeatCount(5) //重複5次
.RepeatForever() //永遠重複
)
.Build();
// Tell quartz to schedule the job using our trigger
scheduler.ScheduleJob(job, trigger);
}
catch (SchedulerException se)
{
Console.WriteLine(se);
Console.ReadKey();
}
}
/// <summary>
/// 使用Cron表示式觸發器
/// </summary>
static void CronTriggers(IScheduler scheduler)
{
//Seconds(秒)
//Minutes(分)
//Hours(時)
//Day-of-Month(天)
//Month(月)
//Day-of-Week(每週)
//年份(1970-2099)
try
{
// define the job and tie it to our HelloJob class
IJobDetail job = JobBuilder.Create<HelloJob>()
//Use a JobKey with the given name and group to identify the JobDetail.
.WithIdentity("job2", "group2")
.Build();
Console.WriteLine("構建簡單觸發器.開始..");
// Trigger the job to run now, and then repeat every 10 seconds
ITrigger trigger = TriggerBuilder.Create()
//Use a TriggerKey with the given name and group to identify the Trigger.
.WithIdentity("trigger2", "group2")
.UsingJobData("note", "cron 觸發器")
//
.WithCronSchedule("1/1 * * ? * *") //cron 表示式
//Set the identity of the Job which should be fired by the produced Trigger - a JobKey will be produced with the given name and group.
.ForJob("job2", "group2")
.Build();
Console.WriteLine("構建簡單觸發器,完成...");
// Tell quartz to schedule the job using our trigger
scheduler.ScheduleJob(job, trigger);
}
catch (SchedulerException se)
{
Console.WriteLine(se);
Console.ReadKey();
}
}
}
public class HelloJob : IJob
{
public void Execute(IJobExecutionContext context)
{
var dataMap = context.MergedJobDataMap;
JobKey key = context.JobDetail.Key;
string note = dataMap.GetString("note");
Console.WriteLine("名稱:{0};備註:{1}。",key,note);
//Console.WriteLine("Greetings from HelloJob!");
}
}
}
相關文章
- .NET控制檯獲取依賴注入例項依賴注入
- win xp故障恢復控制檯應用例項(轉)
- Windows XP 故障恢復控制檯應用例項(轉)Windows
- QT 控制檯訊號與槽簡例QT
- zookeeper控制檯
- 嵌入式產品例項分析-智慧檯燈
- 【例項】增加控制檔案
- JAVA控制檯下:控制檯商城購物系統Java
- Oracle:PL/SQL--開啟控制檯or關閉控制檯OracleSQL
- rocketmq 管理控制檯MQ
- Thread 控制資源例項thread
- Chrome控制檯技巧篇Chrome
- rocketmq控制檯安裝MQ
- Domino控制檯指令大全
- 有趣的控制檯玩法:一行程式碼繪製控制檯影像!行程
- 不一樣的控制檯程式—–在控制檯中輸出圖片薦
- Oracle單例項+ASM新增控制檔案Oracle單例ASM
- Sentinel 實戰-控制檯篇
- Chrome控制檯實用指南Chrome
- dubbo管理控制檯安裝
- Chrome控制檯使用詳解Chrome
- Firebug控制檯詳解
- 控制檯安全性破解
- win10 啟動項有program怎麼辦 win10 啟動項program怎麼解決Win10
- 方向鍵控制元素移動程式碼例項
- js控制貝塞爾曲線程式碼例項JS線程
- oracle的單例項併發控制:Latch(二)Oracle單例
- oracle的單例項併發控制:Lock(一)Oracle單例
- asm例項有沒有控制檔案嗎?ASM
- 控制檯實時檢視 sqlSQL
- EMQ 管理控制檯功能簡介MQ
- 在Chrome控制檯注入npm模組ChromeNPM
- 收集Chrome控制檯使用技巧點Chrome
- Dubbo管理控制檯的安裝
- idea控制檯中文亂碼Idea
- gitlab 如何進入控制檯Gitlab
- C++ 控制檯讀寫excelC++Excel
- Java控制檯常用命令Java