基础篇
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using Common.Logging;
using Quartz.Impl;
namespace ConAppTestQuartzDemo
{
class Program
{
static void Main(string[] args)
{
new ScheduleingFactory().Job();
Console.ReadKey();
}
}
}
RewardJob.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
namespace ConAppTestQuartzDemo
{
public class RewardJob : IJob
{
public virtual void Execute(JobExecutionContext context)
{
Console.WriteLine("\n=================================================================\n");
Console.WriteLine("现在的时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine("\n=================================================================\n");
}
}
}
ScheduleingFactory.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quartz;
using Common.Logging;
using Quartz.Impl;
namespace ConAppTestQuartzDemo
{
public class ScheduleingFactory
{
private static ILog _log = LogManager.GetLogger(typeof(ScheduleingFactory));
private ISchedulerFactory sf = null;
private IScheduler scheduler = null;
public ScheduleingFactory()
{
//初始化调度器工厂
sf = new StdSchedulerFactory();
}
private IScheduler GetScheduler()
{
return sf.GetScheduler();
}
/// <summary>
/// 停止调度工作
/// </summary>
/// <param name="inScheduler"></param>
public void ClearJob(IScheduler inScheduler)
{
_log.Info("--------开始讲任务暂停--------");
string[] groups = inScheduler.TriggerGroupNames;
for (int i = 0; i < groups.Length; i++)
{
String[] names = inScheduler.GetTriggerNames(groups[i]);
for (int j = 0; j < names.Length; j++)
{
inScheduler.UnscheduleJob(names[j], groups[i]);
}
}
_log.Info("开始删除相关工作");
}
public void Job()
{
try
{
scheduler = GetScheduler();
JobDetail rewardJobDetails = new JobDetail("RewardDo", typeof(RewardJob));
SimpleTrigger rewardDoTrigger = new SimpleTrigger("RewardDoTrigger",SimpleTrigger.RepeatIndefinitely , new TimeSpan(0, 0, 3));
scheduler.ScheduleJob(rewardJobDetails, rewardDoTrigger);
scheduler.Start();
}
catch (Exception ex)
{
Console.WriteLine("发生了异常!" + ex.Message);
throw ex;
}
}
}
}
Demo 下载:http://files.cnblogs.com/Music/TestQuartzDemo.rar
进阶篇之挂载到 Windows 服务
1.
谢谢浏览!