原文:https://cloud.tencent.com/developer/article/1030346

Castle是针对.NET平台的一个开源项目,从数据访问框架ORM到IOC容器,再到WEB层的MVC框架、AOP,基本包括了整个开发过程中的所有东西,为我们快速的构建企业级的应用程序提供了很好的服务.具体可参看TerryLee的Castle 开发系列文章。      可以通过称为 Facility 的组件用控制反转 (Inversion of Control, IoC) 和依赖注入将 第三方组件插入内核中。Startable Facility当一个组件满足一定的依赖关系之后,让它自动运行,比如说启动一个窗体或者启动某种服务。 Startable Facility的使用可以说是非常地简单,只要我们的组件实现了IStartable接口就可以了,关于Startable Facility具体可参看Castle IOC容器实践之Startable Facility(一)Castle IOC容器实践之Startable Facility(二)。Quartz 是一个要与 Castle集成的大项目,因为它仅需要您用 Castle的生命周期来启动和停止它。这意味着,当 Castle启动时,您想要 Quartz 启动,当 Castle关闭时,您想要 Quartz 停止。

为了保持本示例的简单性,Quartz 配置使用 Quartz 发行版附带的默认值。这些默认值位于 quartz.properties 文件中,该文件是 dll 文件的一部分。要配置 Quartz 以将数据库用于持久层、远程调度和其他高级选项,必须创建自定义的 quartz.properties 文件。

Quartz 调度器易于启动和关闭;它只通过调用 StdSchedulerFactory.DefaultScheduler 来检索调度器对象。要启动 Quartz,执行 Scheduler.Start() 方法。要停止 Quartz,执行 Scheduler.Shutdown() 方法。要使 Quartz 的生命周期跟随 Castle,将 Start() 调用放入 IStartable的 Start() 方法中,并将 Shutdown() 调用放入 IStartable的 Stop() 方法中。清单 3 展示了添加 Quartz 代码之后完整的实现。

   1:  using Castle.Core;
2: using Quartz.Impl;
3: using Quartz;
4: using Common.Logging;
5: using System.Threading;
6:
7: namespace QuartzComponent
8: {
9: [Transient]
10: public class QuartzStartable : IStartable
11: {
12: private ISchedulerFactory _schedFactory;
13:
14: private static ILog log = LogManager.GetLogger(typeof(QuartzStartable));
15:
16: public QuartzStartable(ISchedulerFactory schedFactory)
17: {
18: _schedFactory = schedFactory;
19: }
20:
21: public void Start()
22: {
23: log.Info("Starting service");
24: IScheduler sched = _schedFactory.GetScheduler();
25:
26: log.Info("------- Scheduling Jobs ----------------");
27:
28: // jobs can be scheduled before sched.start() has been called
29:
30: // get a "nice round" time a few seconds in the future...
31: DateTime ts = TriggerUtils.GetNextGivenSecondDate(null, 15);
32:
33: // job1 will only fire once at date/time "ts"
34: JobDetail job = new JobDetail("job1", "group1", typeof(SimpleQuartzJob));
35: SimpleTrigger trigger = new SimpleTrigger("trigger1", "group1");
36: // set its start up time
37: trigger.StartTime = ts;
38: // set the interval, how often the job should run (10 seconds here)
39: trigger.RepeatInterval = 10000;
40: // set the number of execution of this job, set to 10 times.
41: // It will run 10 time and exhaust.
42: trigger.RepeatCount = 100;
43:
44:
45: // schedule it to run!
46: DateTime ft = sched.ScheduleJob(job, trigger);
47: log.Info(string.Format("{0} will run at: {1} and repeat: {2} times, every {3} seconds",
48: job.FullName, ft.ToString("r"), trigger.RepeatCount, (trigger.RepeatInterval / 1000)));
49: log.Info("------- Waiting five minutes... ------------");
50:
51: sched.Start();
52: try
53: {
54: // wait five minutes to show jobs
55: Thread.Sleep(300 * 1000);
56: // executing...
57: }
58: catch (ThreadInterruptedException)
59: {
60: }
61:
62:
63: }
64:
65: public void Stop()
66: {
67: log.Info("Stopping service");
68: try
69: {
70: IScheduler scheduler = _schedFactory.GetScheduler();
71: scheduler.Shutdown(true);
72: }
73: catch (SchedulerException se)
74: {
75: log.Error("Cannot shutdown scheduler.", se);
76: }
77:
78: }
79: }
80: }

将Quartz.net集成到Castle容器中,只需要几行代码就可以了,就会在Castle容器启动的时候自动启用Quartz.net的作业调度。

   1:  namespace QuartzComponent
2: {
3: class ConsoleMain
4: {
5: static ILog log = LogManager.GetLogger(typeof(ConsoleMain));
6:
7: [STAThread]
8: public static void Main(string[] args)
9: {
10: IWindsorContainer container = new WindsorContainer();
11: //添加Facility
12:
13: container.AddFacility("startable", new StartableFacility());
14:
15: container.AddComponent("Scheduler", typeof(ISchedulerFactory), typeof(StdSchedulerFactory));
16:
17: container.AddComponent("QuartzStartable", typeof(QuartzStartable));
18:
19: //Console.Read();
20: }
21: }
22: }

结束语

对于大多数开源项目,实现少量工作就可以集成到Castle容器中,类似 Quartz.net 的应用程序是简单集成的优秀候选项,因为它只需要启动和关闭。有很多与 Quartz.net 一样简单的有助于集成的开源项目。

下载例子代码: QuartzComponent.zip

将Quartz.NET集成到 Castle中的更多相关文章

  1. quartz 集成到Spring中

    记录一下,防止忘记. 需要的jar包,quartz-2.2.3.jar,commons-collection-3.1.jar,spring-context-support-4.3.4.RELEASE. ...

  2. Quartz.NET集成UI版

    Quartz.NET Quartz.NET是NET的开源作业调度系统. Quartz.NET是一个功能齐全的开源作业调度系统,可用于从最小的应用程序到大型企业系统. Quartz.NET目前支持NET ...

  3. Castle中AdditionalInterfaces用法介绍

    首先见下图(图一),其中FooController是一个没有实现任何Interface的空类.需要实现的效果是:通过FooController对象调用FooService的Do方法.设置这一不常见的场 ...

  4. 【笔记】android sdk集成的eclipse中导入项目

    android sdk集成的eclipse中导入项目 想要把旧的ADT项目,一模一样的导入进来,需要: 1.把项目放到,非当前ADT的workspace目录下: 2.从Project中Import,选 ...

  5. Spark Streaming揭秘 Day28 在集成开发环境中详解Spark Streaming的运行日志内幕

    Spark Streaming揭秘 Day28 在集成开发环境中详解Spark Streaming的运行日志内幕 今天会逐行解析一下SparkStreaming运行的日志,运行的是WordCountO ...

  6. 持续集成:TestNG中case之间的关系

    持续集成:TestNG中case之间的关系   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq: ...

  7. 第三百五十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中

    第三百五十八节,Python分布式爬虫打造搜索引擎Scrapy精讲—将bloomfilter(布隆过滤器)集成到scrapy-redis中,判断URL是否重复 布隆过滤器(Bloom Filter)详 ...

  8. 第三百五十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—将selenium操作谷歌浏览器集成到scrapy中

    第三百五十一节,Python分布式爬虫打造搜索引擎Scrapy精讲—将selenium操作谷歌浏览器集成到scrapy中 1.爬虫文件 dispatcher.connect()信号分发器,第一个参数信 ...

  9. 如何将SLIC集成到ESXi中

    如何将SLIC集成到ESXi中 参考 http://forums.mydigitallife.info/threads/12982-ESX-ESXi-Bios-Tools/page34?p=72183 ...

随机推荐

  1. python3 修改excel 单元格的值(xlsx)

    excel code #coding=utf- import os.path import os from openpyxl.reader.excel import load_workbook # e ...

  2. 【cf补题记录】Codeforces Round #608 (Div. 2)

    比赛传送门 再次改下写博客的格式,以锻炼自己码字能力 A. Suits 题意:有四种材料,第一套西装需要 \(a\).\(d\) 各一件,卖 \(e\) 块:第二套西装需要 \(b\).\(c\).\ ...

  3. 上传文件到新浪云Storage的方法

    上传文件到新浪云Storage的方法,兼容本地服务器 if (!empty($_FILES['sharepic']['name'])){ $tmp_file = $_FILES['sharepic'] ...

  4. 1-7docke的网络模式

    1.Bridge模式 bridge 模式是 docker 的默认⽹络模式,不写 –net 参数,就是 bridge 模式.比如使⽤ docker run - p 时 工作模式从网上找了一个,如下 例子 ...

  5. pytest学习笔记二 fixtrue

    前言 官方文档关于fixture功能的解释如下: The purpose of test fixtures is to provide a fixed baseline upon which test ...

  6. POJ-图论-最小生成树模板

    POJ-图论-最小生成树模板 Kruskal算法 1.初始时所有结点属于孤立的集合. 2.按照边权递增顺序遍历所有的边,若遍历到的边两个顶点仍分属不同的集合(该边即为连通这两个集合的边中权值最小的那条 ...

  7. 关于linux 执行权限的理解

    关于linux 执行权限的理解他这个执行权限不是 执行什么命令的 是这个文件 是否能被执行的权限 比方说<pre>shell_exec('/home/crontabtest12.sh'); ...

  8. 043 用户注册功能03--Redis安装及完成短信发送功能

    1.Redis安装 (1)下载地址:https://github.com/MicrosoftArchive/redis/releases/tag/win-3.2.100   ( redis官网: ht ...

  9. Unary模式下客户端创建 default-executor 和 resolver-executor 线程和从启动到执行grpc_connector_connect的主要流程

    (原创)C/C/1.25.0-dev grpc-c/8.0.0, 使用的例子是自带的例子GreeterClient 创建 default-executor 和 resolver-executor 线程 ...

  10. ASp.net Core EF ActionFilterAttribute AOP

    在项目中经常遇到一些数据的修改,很多时候业务方需要一个修改日志记录,这里我们计划用mssql数据库来存放日志记录,用EF来操作,记录日志可以用mvc的ActionFilterAttribute 来完成 ...