如何在ASP.NET中使用Quartz.net

我不知道如何在ASP.NET中使用Quartz.dll。 在哪里编写调度作业的代码来每天早上触发邮件? 请如果有些身体知道它plz帮助我…

编辑:我发现如何在PRO方式使用Quartz.NET? 真的很有用。

你有几个select,取决于你想要做什么以及你想如何设置它。 例如,您可以将Quartz.Net服务器作为独立的Windows服务器安装,也可以将其embedded到您的asp.net应用程序中。

如果你想运行它embedded式,那么你可以启动服务器说你的global.asax,就像这样(源代码示例,示例#12):

NameValueCollection properties = new NameValueCollection(); properties["quartz.scheduler.instanceName"] = "RemoteServer"; // set thread pool info properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; properties["quartz.threadPool.threadCount"] = "5"; properties["quartz.threadPool.threadPriority"] = "Normal"; ISchedulerFactory sf = new StdSchedulerFactory(properties); IScheduler sched = sf.GetScheduler(); sched.Start(); 

如果你把它作为一个服务运行,你可以像这样远程连接(例子#12):

 NameValueCollection properties = new NameValueCollection(); properties["quartz.scheduler.instanceName"] = "RemoteClient"; // set thread pool info properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; properties["quartz.threadPool.threadCount"] = "5"; properties["quartz.threadPool.threadPriority"] = "Normal"; // set remoting expoter properties["quartz.scheduler.proxy"] = "true"; properties["quartz.scheduler.proxy.address"] = "tcp://localhost:555/QuartzScheduler"; // First we must get a reference to a scheduler ISchedulerFactory sf = new StdSchedulerFactory(properties); IScheduler sched = sf.GetScheduler(); 

一旦你有了调度程序的引用(通过远程处理或因为你有一个embedded式实例),你可以安排这样的工作:

 // define the job and ask it to run JobDetail job = new JobDetail("remotelyAddedJob", "default", typeof(SimpleJob)); JobDataMap map = new JobDataMap(); map.Put("msg", "Your remotely added job has executed!"); job.JobDataMap = map; CronTrigger trigger = new CronTrigger("remotelyAddedTrigger", "default", "remotelyAddedJob", "default", DateTime.UtcNow, null, "/5 * * ? * *"); // schedule the job sched.ScheduleJob(job, trigger); 

这里是我写给Quartz.Net入门的一些post的链接: http ://jvilalta.blogspot.com/2009/03/getting-started-with-quartznet-part-1.html

几个星期前我写了关于使用Quartz.Net来安排Windows Azure Worker Roles中的作业。 从那时起,我遇到了一个迫使我围绕Quartz.Net IScheduler创build一个包装的需求。 JobSchedule负责从CloudConfigurationManager中读取计划string并计划作业。

CloudConfigurationManager从angular色的configuration文件中读取设置,可以通过Windows服务configuration部分下的Windows Azurepipe理门户进行编辑。

以下示例将安排每天上午6点,上午8点,上午10点,下午12:30和下午4点30分执行的工作。 日程安排是在可以通过Visual Studio编辑的angular色设置中定义的。 要访问angular色设置,请转到您的Windows Azure云服务项目,并在angular色文件夹下find所需的angular色configuration。 双击configuration文件打开configuration编辑器,然后导航到“设置”选项卡。 点击“添加设置”,并命名新的设置“JobDailySchedule”,并将其值设置为6:0; 8:0; 10:0; 12:30; 16:30;

 The code from this Post is part of the Brisebois.WindowsAzure NuGet Package To install Brisebois.WindowsAzure, run the following command in the Package Manager Console PM> Install-Package Brisebois.WindowsAzure Get more details about the Nuget Package. 

然后使用JobSchedule使用angular色configuration文件中定义的日程安排日常工作。

 var schedule = new JobSchedule(); schedule.ScheduleDailyJob("JobDailySchedule", typeof(DailyJob)); 

DailyJob实施如下。 由于这是一个演示,我不会添加任何特定的逻辑的工作。

 public class DailyJob : IJob { public void Execute(IJobExecutionContext context) { //Do your daily work here } } 

JobSchedule包装了Quartz.Net IScheduler。 在之前的文章中,我谈到了封装你的第三方工具的重要性,这是一个很好的例子,因为我包含了作业调度逻辑,我可以改变这个逻辑而不会影响使用JobSchedule的代码。

当angular色启动时JobSchedule应该被configuration,JobSchedule实例应该在angular色的整个生命周期中被维护。 更改日程安排可以通过在云服务的configuration部分下通过Windows Azurepipe理门户来更改“JobDailySchedule”设置来实现。 然后,要应用新的计划,请通过云服务的实例部分下的Windows Azurepipe理门户来重新启动angular色实例。

 public class JobSchedule { private readonly IScheduler sched; public JobSchedule() { var schedFact = new StdSchedulerFactory(); sched = schedFact.GetScheduler(); sched.Start(); } /// <summary> /// Will schedule jobs in Eastern Standard Time /// </summary> /// <param name="scheduleConfig">Setting Key from your CloudConfigurations, /// value format "hh:mm;hh:mm;"</param> /// <param name="jobType">must inherit from IJob</param> public void ScheduleDailyJob(string scheduleConfig, Type jobType) { ScheduleDailyJob(scheduleConfig, jobType, "Eastern Standard Time"); } /// <param name="scheduleConfig">Setting Key from your CloudConfigurations, /// value format "hh:mm;hh:mm;"</param> /// <param name="jobType">must inherit from IJob</param> public void ScheduleDailyJob(string scheduleConfig, Type jobType, string timeZoneId) { var schedule = CloudConfigurationManager.GetSetting(scheduleConfig); if (schedule == "-") return; schedule.Split(';') .Where(s => !string.IsNullOrWhiteSpace(s)) .ToList() .ForEach(h => { var index = h.IndexOf(':'); var hour = h.Substring(0, index); var minutes = h.Substring(index + 1, h.Length - (index + 1)); var job = new JobDetailImpl(jobType.Name + hour + minutes, null, jobType); var dh = Convert.ToInt32(hour, CultureInfo.InvariantCulture); var dhm = Convert.ToInt32(minutes, CultureInfo.InvariantCulture); var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId); var cronScheduleBuilder = CronScheduleBuilder .DailyAtHourAndMinute(dh, dhm) .InTimeZone(tz); var trigger = TriggerBuilder.Create() .StartNow() .WithSchedule(cronScheduleBuilder) .Build(); sched.ScheduleJob(job, trigger); }); } /// <summary> /// Will schedule jobs in Eastern Standard Time /// </summary> /// <param name="scheduleConfig">Setting Key from your CloudConfigurations, /// value format "hh:mm;hh:mm;"</param> /// <param name="jobType">must inherit from IJob</param> public void ScheduleWeeklyJob(string scheduleConfig, Type jobType) { ScheduleWeeklyJob(scheduleConfig, jobType, "Eastern Standard Time"); } /// <param name="scheduleConfig">Setting Key from your CloudConfigurations, /// value format "hh:mm;hh:mm;"</param> /// <param name="jobType">must inherit from IJob</param> public void ScheduleWeeklyJob(string scheduleConfig, Type jobType, string timeZoneId) { var schedule = CloudConfigurationManager.GetSetting(scheduleConfig); schedule.Split(';') .Where(s => !string.IsNullOrWhiteSpace(s)) .ToList() .ForEach(h => { var index = h.IndexOf(':'); var hour = h.Substring(0, index); var minutes = h.Substring(index + 1, h.Length - (index + 1)); var job = new JobDetailImpl(jobType.Name + hour + minutes, null, jobType); var dh = Convert.ToInt32(hour, CultureInfo.InvariantCulture); var dhm = Convert.ToInt32(minutes, CultureInfo.InvariantCulture); var tz = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId); var builder = CronScheduleBuilder .WeeklyOnDayAndHourAndMinute(DayOfWeek.Monday, dh, dhm) .InTimeZone(tz); var trigger = TriggerBuilder.Create() .StartNow() .WithSchedule(builder) .Build(); sched.ScheduleJob(job, trigger); }); } }