前言
总结了一下quartz组件的一些常用方法,以备将来不时之需。哈哈,看着挺简单有些好是值得笔记一下的。好记性不如烂笔头吗?
代码部分:
方法类内容:
public class ExamplesGo
{
static ISchedulerFactory schedFact = new StdSchedulerFactory();
public static IScheduler sched = schedFact.GetScheduler();
}
View Code
任务触发器分离实现:
#region 1.运行任务【所有】
///
/// 运行任务
///
public void Run()
{
if (sched != null)
sched.Start();
}
#endregion
#region 2.添加job
///
/// 添加job
///
public void AddJob()
{
IJobDetail job = JobBuilder.Create()
.WithIdentity("HelloJob", "group1")
.StoreDurably()
.Build();
sched.AddJob(job, true);
}
#endregion
#region 3.添加触发器
///
/// 添加触发器
///
///
需要添加触发器的job
public
void
AddTrigger(IJobDetail job) { ITrigger trigger =
TriggerBuilder.Create() .WithIdentity(
"
myTrigger
",
"
group1
"
) .WithCronSchedule(
"
0/5 * * * * ?
")
//
5秒执行一次
.ForJob(job) .Build(); sched.ScheduleJob(trigger); }
public
void
AddTrigger1(IJobDetail job) { ITrigger trigger =
TriggerBuilder.Create() .WithIdentity(
"
myTrigger2
",
"
group1
"
) .WithCronSchedule(
"
0/1 * * * * ?
")
//
5秒执行一次
.ForJob(job) .Build(); sched.ScheduleJob(trigger); }
#endregion
#region 4.根据key返回Job
///
///
根据key返回Job
///
///
需要查找的job名称
///
public IJobDetail GetJob(
string
jobName) { JobKey jobKey = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList().Where(b => b.Name ==
jobName).FirstOrDefault();
return
sched.GetJobDetail(jobKey); }
#endregion
View Code
基本的暂停,恢复,删除:
#region 5.暂停任务[全部暂停]
///
/// 暂停任务
///
public void PauseAll()
{
if (sched != null)
sched.PauseAll();
}
#endregion
#region 6.暂停任务【单个任务】
///
/// 暂停任务
///
///
需要暂停任务的名称
public
void Shutdown(
string
jobName) {
if (sched !=
null
) { JobKey jobKey = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList().Where(b => b.Name ==
jobName).FirstOrDefault(); sched.PauseJob(jobKey); } }
#endregion
#region 7.恢复当前任务
///
///
恢复当前任务
///
///
需要恢复任务的名称
public
void ResumeJob(
string
jobName) {
if (sched !=
null
) { JobKey jobKey = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList().Where(b => b.Name ==
jobName).FirstOrDefault(); sched.ResumeJob(jobKey); } }
#endregion
#region 8.恢复所有任务
///
///
恢复所有任务
///
public
void
ResumeJobAll() {
if (sched !=
null
) { sched.ResumeAll(); } }
#endregion
#region 9.删除当前任务
///
///
删除当前任务
///
///
删除任务名称
public
void DeleteJob(
string
jobName) {
if (sched !=
null
) { JobKey jobKey = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList().Where(b => b.Name ==
jobName).FirstOrDefault(); sched.DeleteJob(jobKey); } }
#endregion
#region 10.删除所有任务
///
///
删除所有任务
///
public
void
DeleteJobAll() {
if (sched !=
null
) { sched.Clear(); } }
#endregion
View Code
最后一些复杂点的查询:
#region 11.返回所有任务键
///
/// 返回所有任务键
///
///
public List GetJobAll()
{
//获取所有job
List jobKeys = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList();
return jobKeys;
}
#endregion
#region 12.返回所有的触发器
///
/// 返回所有的触发器
///
///
public List GetTrigAll()
{
List trigKeys = sched.GetTriggerKeys(GroupMatcher.AnyGroup()).ToList();
return trigKeys;
}
#endregion
#region 13.返回job对应的trigger
///
/// 返回job对应的trigger
///
///
任务名称
///
public List GetJobToTrig(
string
jobName) { JobKey jobKey = sched.GetJobKeys(GroupMatcher.AnyGroup()).ToList().Where(b => b.Name ==
jobName).FirstOrDefault(); List triggerList =
sched.GetTriggersOfJob(jobKey).ToList();
return
triggerList; }
#endregion
View Code
最后添加的两个一个关闭组件和修改触发器:
#region 14.关闭所有任务
///
/// 关闭所有任务
///
public void Shutdown()
{
if (sched != null)
sched.Shutdown();
}
#endregion
#region 15.修改任务触发器
public void ModiyTrig()
{
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("myTrigger", "group1")
// .StartAt(runTime)
.WithCronSchedule("0/10 * * * * ?") //10秒执行一次
.Build();
sched.RescheduleJob(trigger.Key, trigger);
sched.Start();
}
#endregion
View Code