Quartz.net通过配置文件来完成作业调度
点击次数:31 次 发布日期:2008-11-26 10:33:23 作者:源代码网
|
源代码网推荐 将Quartz.NET集成到 Castle中 例子代码使用的Quartz.net版本是0.6,Quartz.NET 0.9 发布了 ,最新版本支持通过配置文件来完成后台的作业调度,不必手工创建Trigger和Scheduler。将QuartzStartable 改造如下: 源代码网推荐 源代码网推荐 using System; 源代码网推荐 using System.Collections.Generic; 源代码网推荐 using System.Text; 源代码网推荐 源代码网推荐 using Castle.Core; 源代码网推荐 using Quartz.Impl; 源代码网推荐 using Quartz; 源代码网推荐 using Common.Logging; 源代码网推荐 using System.Threading; 源代码网推荐 using System.IO; 源代码网推荐 using Quartz.Xml; 源代码网推荐 using System.Collections; 源代码网推荐 源代码网推荐 namespace QuartzComponent 源代码网推荐 { 源代码网推荐 [Transient] 源代码网推荐 public class QuartzStartable : IStartable 源代码网推荐 { 源代码网推荐 private ISchedulerFactory _schedFactory; 源代码网推荐 private JobSchedulingDataProcessor processor; 源代码网推荐 源代码网推荐 private static ILog log = LogManager.GetLogger(typeof(QuartzStartable)); 源代码网推荐 源代码网推荐 public QuartzStartable(ISchedulerFactory schedFactory) 源代码网推荐 { 源代码网推荐 _schedFactory = schedFactory; 源代码网推荐 processor = new JobSchedulingDataProcessor(true, true); 源代码网推荐 } 源代码网推荐 源代码网推荐 public void Start() 源代码网推荐 { 源代码网推荐 log.Info("Starting service"); 源代码网推荐 IScheduler sched = _schedFactory.GetScheduler(); 源代码网推荐 源代码网推荐 //log.Info("------- Scheduling Jobs ----------------"); 源代码网推荐 源代码网推荐 //// jobs can be scheduled before sched.start() has been called 源代码网推荐 源代码网推荐 //// get a "nice round" time a few seconds in the future... 源代码网推荐 //DateTime ts = TriggerUtils.GetNextGivenSecondDate(null, 15); 源代码网推荐 源代码网推荐 //// job1 will only fire once at date/time "ts" 源代码网推荐 //JobDetail job = new JobDetail("job1", "group1", typeof(SimpleQuartzJob)); 源代码网推荐 //SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1"); 源代码网推荐 //// set its start up time 源代码网推荐 //trigger.StartTimeUtc = ts; 源代码网推荐 //// set the interval, how often the job should run (10 seconds here) 源代码网推荐 //trigger.RepeatInterval = 10000; 源代码网推荐 //// set the number of execution of this job, set to 10 times. 源代码网推荐 //// It will run 10 time and exhaust. 源代码网推荐 //trigger.RepeatCount = 100; 源代码网推荐 源代码网推荐 源代码网推荐 //// schedule it to run! 源代码网推荐 //DateTime ft = sched.ScheduleJob(job, trigger); 源代码网推荐 //log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds", 源代码网推荐 // job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval / 1000))); 源代码网推荐 //log.Info("------- Waiting five minutes... ------------"); 源代码网推荐 源代码网推荐 //sched.Start(); 源代码网推荐 Stream s = ReadJobXmlFromEmbeddedResource("MinimalConfiguration.xml"); 源代码网推荐 processor.ProcessStream(s, null); 源代码网推荐 processor.ScheduleJobs(new Hashtable(), sched, false); 源代码网推荐 sched.Start(); 源代码网推荐 try 源代码网推荐 { 源代码网推荐 // wait five minutes to show jobs 源代码网推荐 Thread.Sleep(300 * 1000); 源代码网推荐 // executing... 源代码网推荐 } 源代码网推荐 catch (ThreadInterruptedException) 源代码网推荐 { 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 private static Stream ReadJobXmlFromEmbeddedResource(string resourceName) 源代码网推荐 { 源代码网推荐 string fullName = "QuartzComponent." + resourceName; 源代码网推荐 return new StreamReader(typeof(QuartzStartable).Assembly.GetManifestResourceStream(fullName)).BaseStream; 源代码网推荐 } 源代码网推荐 源代码网推荐 public void Stop() 源代码网推荐 { 源代码网推荐 log.Info("Stopping service"); 源代码网推荐 try 源代码网推荐 { 源代码网推荐 IScheduler scheduler = _schedFactory.GetScheduler(); 源代码网推荐 scheduler.Shutdown(true); 源代码网推荐 } 源代码网推荐 catch (SchedulerException se) 源代码网推荐 { 源代码网推荐 log.Error("Cannot shutdown scheduler.", se); 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 增加一个配置文件MinimalConfiguration.xml,设置为嵌入资源类型。内容如下: 源代码网推荐 源代码网推荐 <?xml version="1.0" encoding="UTF-8"?> 源代码网推荐 <quartz xmlns="http://quartznet.sourceforge.net/JobSchedulingData" 源代码网推荐 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 源代码网推荐 version="1.0" 源代码网推荐 overwrite-existing-jobs="true"> 源代码网推荐 源代码网推荐 <job> 源代码网推荐 <job-detail> 源代码网推荐 <name>jobName1</name> 源代码网推荐 <group>jobGroup1</group> 源代码网推荐 <job-type>QuartzComponent.SimpleQuartzJob, QuartzComponent</job-type> 源代码网推荐 </job-detail> 源代码网推荐 源代码网推荐 <trigger> 源代码网推荐 <simple> 源代码网推荐 <name>simpleName</name> 源代码网推荐 <group>simpleGroup</group> 源代码网推荐 <job-name>jobName1</job-name> 源代码网推荐 <job-group>jobGroup1</job-group> 源代码网推荐 <start-time>2007-12-09T18:08:50</start-time> 源代码网推荐 <repeat-count>100</repeat-count> 源代码网推荐 <repeat-interval>3000</repeat-interval> 源代码网推荐 </simple> 源代码网推荐 </trigger> 源代码网推荐 <trigger> 源代码网推荐 <cron> 源代码网推荐 <name>cronName</name> 源代码网推荐 <group>cronGroup</group> 源代码网推荐 <job-name>jobName1</job-name> 源代码网推荐 <job-group>jobGroup1</job-group> 源代码网推荐 <start-time>1982-06-28T18:15:00+02:00</start-time> 源代码网推荐 <cron-expression>0/10 * * * * ?</cron-expression> 源代码网推荐 </cron> 源代码网推荐 </trigger> 源代码网推荐 </job> 源代码网推荐 </quartz> 源代码网推荐 可以看到,在配置文件中把jobdetail和trigger都作了完整的定义,并组合成一个job。 源代码网推荐 源代码网推荐 当然也可以在quartz.properties文件中设置一个quertz_job.xml文件,例如: 源代码网推荐 源代码网推荐 // First we must get a reference to a scheduler 源代码网推荐 NameValueCollection properties = new NameValueCollection(); 源代码网推荐 properties["quartz.scheduler.instanceName"] = "XmlConfiguredInstance"; 源代码网推荐 源代码网推荐 // set thread pool info 源代码网推荐 properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz"; 源代码网推荐 properties["quartz.threadPool.threadCount"] = "5"; 源代码网推荐 properties["quartz.threadPool.threadPriority"] = "Normal"; 源代码网推荐 源代码网推荐 // job initialization plugin handles our xml reading, without it defaults are used 源代码网推荐 properties["quartz.plugin.xml.type"] = "Quartz.Plugin.Xml.JobInitializationPlugin, Quartz"; 源代码网推荐 properties["quartz.plugin.xml.fileNames"] = "~/quartz_jobs.xml"; 源代码网推荐 ISchedulerFactory sf = new StdSchedulerFactory(properties); 源代码网推荐 源代码网推荐 这样,在启动Castle的时候,Quartz.Plugin.Xml.JobInitializationPlugin就会自动读取quartz.properties这个配置文件,并初始化调度信息,启动Scheduler。 源代码网推荐 源代码网推荐 一个Job类,一个quartz.properties文件,一个quertz_job.xml文件,非常简单灵活。 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
