日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Quartz.net通过配置文件来完成作业调度

發(fā)布時(shí)間:2024/4/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Quartz.net通过配置文件来完成作业调度 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

將Quartz.NET集成到 Castle中 例子代碼使用的Quartz.net版本是0.6,Quartz.NET 0.9 發(fā)布了 ,最新版本支持通過配置文件來完成后臺(tái)的作業(yè)調(diào)度,不必手工創(chuàng)建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);
??????????? }

??????? }
??? }
}
增加一個(gè)配置文件MinimalConfiguration.xml,設(shè)置為嵌入資源類型。內(nèi)容如下:

<?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都作了完整的定義,并組合成一個(gè)job。

當(dāng)然也可以在quartz.properties文件中設(shè)置一個(gè)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);

這樣,在啟動(dòng)Castle的時(shí)候,Quartz.Plugin.Xml.JobInitializationPlugin就會(huì)自動(dòng)讀取quartz.properties這個(gè)配置文件,并初始化調(diào)度信息,啟動(dòng)Scheduler。

一個(gè)Job類,一個(gè)quartz.properties文件,一個(gè)quertz_job.xml文件,非常簡(jiǎn)單靈活。

下載例子代碼 :QuartzComponentWithXml.zip

轉(zhuǎn)載于:https://www.cnblogs.com/shanyou/archive/2007/12/09/988502.html

總結(jié)

以上是生活随笔為你收集整理的Quartz.net通过配置文件来完成作业调度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。