Quartz是Java平台的一个开源的作业调度框架。Quartz.net是从java版本移植到.net版本的。.net项目使用Quartz来执行批处理等定时任务非常方便。

(1)从nuget上可以安装Quartz.net

(2)quartz配置:

  1. <configSections>
  2. <section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
  3. </configSections>
  4. <quartz>
  5. <add key="quartz.scheduler.instanceName" value="ExampleDefaultQuartzScheduler"/>
  6. <add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz"/>
  7. <add key="quartz.threadPool.threadCount" value="10"/>
  8. <add key="quartz.threadPool.threadPriority" value="2"/>
  9. <add key="quartz.jobStore.misfireThreshold" value="60000"/>
  10. <add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz"/>
  11. </quartz>
  12. <appSettings>
  13. <!--cronexpression表达式:从每分钟的第2秒开始,每间隔5秒执行-->
  14. <add key="cronExpr" value="2/5 * * * * ?"/>
  15. </appSettings>

(3)创建一个普通类,实现Quartz.IJob接口

接口很简单,只有一个Execute()方法(跟java里一样),在这个方法里写你要做的处理逻辑。

  1. public class MyJob : Quartz.IJob
  2. {
  3. public void Execute(Quartz.IJobExecutionContext context)
  4. {
  5. // 你的处理逻辑,也就是“工作”
  6. Console.WriteLine(DateTime.Now);
  7. }
  8. }

(4)启动工作调度

  1.  

using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // Initializes a new instance of the Quartz.Impl.StdSchedulerFactory class.
  6. ISchedulerFactory sf = new Quartz.Impl.StdSchedulerFactory();
  7. // Returns a client-usable handle to a Quartz.IScheduler.
  8. IScheduler sched = sf.GetScheduler();
  9. // 定义一个Job(即你的处理逻辑,也就是“工作”)
  10. IJobDetail job = new JobDetailImpl("job1", "group1", typeof(MyJob));
  11.  
  12. // 定义触发器(读取AppSettings)
  13. string cronExpr = ConfigurationManager.AppSettings["cronExpr"];
  14. ITrigger trigger = new CronTriggerImpl("trigger1", "group1", "job1", "group1", cronExpr);
  15.  
  16. // 将给定的Job添加到调度器里
  17. sched.AddJob(job, true, true);
  18. // 为Job指派触发器
  19. sched.ScheduleJob(trigger);
  20. // 启动调度器线程
  21. sched.Start();
  22.  
  23. Console.Read();
  24. }
  25. }

我这里是一个控制台程序。对于web程序或服务程序,当Application_End的时候,需要调用Scheduler的Shutdown()方法来关闭Quartz的工作。

注意IScheduler有两个重载的AddJob方法:

  1. namespace Quartz
  2. {
  3. public interface IScheduler
  4. {
  5. //
  6. // 摘要:
  7. // Add the given Quartz.IJob to the Scheduler - with no associated Quartz.ITrigger.
  8. // The Quartz.IJob will be 'dormant' until it is scheduled with a Quartz.ITrigger,
  9. // or Quartz.IScheduler.TriggerJob(Quartz.JobKey) is called for it.
  10. //
  11. // 备注:
  12. // The Quartz.IJob must by definition be 'durable', if it is not, SchedulerException
  13. // will be thrown.
  14. void AddJob(IJobDetail jobDetail, bool replace);
  15. //
  16. // 摘要:
  17. // Add the given Quartz.IJob to the Scheduler - with no associated Quartz.ITrigger.
  18. // The Quartz.IJob will be 'dormant' until it is scheduled with a Quartz.ITrigger,
  19. // or Quartz.IScheduler.TriggerJob(Quartz.JobKey) is called for it.
  20. //
  21. // 备注:
  22. // With the storeNonDurableWhileAwaitingScheduling parameter set to true, a
  23. // non-durable job can be stored. Once it is scheduled, it will resume normal
  24. // non-durable behavior (i.e. be deleted once there are no remaining associated
  25. // triggers).
  26. void AddJob(IJobDetail jobDetail, bool replace, bool storeNonDurableWhileAwaitingScheduling);
  27. }
  28. }

上面的AddJob要调用void AddJob(IJobDetail jobDetail, bool replace, bool storeNonDurableWhileAwaitingScheduling);把storeNonDurableWhileAwaitingScheduling参数设置为true。否则会抛出SchedulerException异常:

  1. 未处理Quartz.SchedulerException
  2. HResult=-
  3. Message=Jobs added with no trigger must be durable.
  4. Source=Quartz
  5. StackTrace:
  6. Quartz.Core.QuartzScheduler.AddJob(IJobDetail jobDetail, Boolean replace, Boolean storeNonDurableWhileAwaitingScheduling) 位置 c:\Program Files (x86)\Jenkins\workspace\Quartz.NET\src\Quartz\Core\QuartzScheduler.cs:行号
  7. Quartz.Core.QuartzScheduler.AddJob(IJobDetail jobDetail, Boolean replace) 位置 c:\Program Files (x86)\Jenkins\workspace\Quartz.NET\src\Quartz\Core\QuartzScheduler.cs:行号
  8. Quartz.Impl.StdScheduler.AddJob(IJobDetail jobDetail, Boolean replace) 位置 c:\Program Files (x86)\Jenkins\workspace\Quartz.NET\src\Quartz\Impl\StdScheduler.cs:行号
  9. UnitTest.Program.Main(String[] args) 位置 d:\SourceProject\infrastructure.sms\trunk\UnitTest\Program.cs:行号
  10. System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
  11. System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
  12. Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  13. System.Threading.ThreadHelper.ThreadStart_Context(Object state)
  14. System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  15. System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  16. System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  17. System.Threading.ThreadHelper.ThreadStart()
  1.  

(5)接下来,就可以允许程序,查看效果了。

关于quartz的CronExpression表达式:

常用示例:

0 0 12 * * ?    每天12点触发

0 15 10 ? * *    每天10点15分触发

0 15 10 * * ?    每天10点15分触发

0 15 10 * * ? *    每天10点15分触发

0 15 10 * * ? 2005    2005年每天10点15分触发

0 * 14 * * ?    每天下午的 2点到2点59分每分触发

0 0/5 14 * * ?    每天下午的 2点到2点59分(整点开始,每隔5分触发)

0 0/5 14,18 * * ?    每天下午的 2点到2点59分、18点到18点59分(整点开始,每隔5分触发)

0 0-5 14 * * ?    每天下午的 2点到2点05分每分触发

0 10,44 14 ? 3 WED    3月份每周三下午的 2点10分和2点44分触发

0 15 10 ? * MON-FRI    从周一到周五每天上午的10点15分触发

0 15 10 15 * ?    每月15号上午10点15分触发

0 15 10 L * ?    每月最后一天的10点15分触发

0 15 10 ? * 6L    每月最后一周的星期五的10点15分触发

0 15 10 ? * 6L 2002-2005    从2002年到2005年每月最后一周的星期五的10点15分触发

0 15 10 ? * 6#3    每月的第三周的星期五开始触发

0 0 12 1/5 * ?    每月的第一个中午开始每隔5天触发一次

0 11 11 11 11 ?    每年的11月11号 11点11分触发(光棍节)

好脑袋不如烂笔头-Quartz使用总结的更多相关文章

  1. 【烂笔头】git常用命令篇

    前言 常言道,好记性不如烂笔头,更何况笔者的记性也不是太好,于是就有了这篇“烂笔头”系列之一的git命令记录.本篇主要记录了笔者在工作当中使用过的相关命令,以方便平时查看,同时也供同行们参考.当然,读 ...

  2. Common lang一些边界方法总结(好记性不如烂笔头,需要慢慢积累).一定要利用好现有的轮子,例如Apache common与Google Guava

    好记性真是不如烂笔头啊!!!! 如下代码: List<String> list = new ArrayList<String>(); list.add("1" ...

  3. 好记性不如烂笔头-linux学习笔记1

    好记性不如烂笔头-linux学习笔记1 linux的文件系统有ext2,ext3,ext4,目前主流是ext4 linux主要用于服务器级别的操作系统,安装时需要至少2个分区 一个是交换分区,swap ...

  4. 好记性不如烂笔头-Mysql查找如何判断字段是否包含某个字符串

    好记性不如烂笔头-Mysql查找如何判断字段是否包含某个字符串 利用mysql 字符串函数 find_in_set(); SELECT * FROM users WHERE find_in_set(' ...

  5. [nodejs]修改全局包位置,修复npm安装全局模块命令失效。好记性不如烂笔头

    修复npm -g 全局安装命令失效,好的吧不得不承认,好记性不如烂笔头,我居然会忘记方法哈哈哈 Linux安装nodejs sudo apt install node sudo apt install ...

  6. MVC 好记星不如烂笔头之 ---> 全局异常捕获以及ACTION捕获

    public class BaseController : Controller { /// <summary> /// Called after the action method is ...

  7. MVC 好记星不如烂笔头之 ---> 页面压缩GIP

    public class BaseController : Controller { /// <summary> /// Called before the action method i ...

  8. Unity烂笔头1-自定义INSPECTOR属性窗口节点项

    1.添加输入框和标签 LevelScript: using UnityEngine; using System.Collections; public class LevelScript : Mono ...

  9. MVC5 烂笔头

    HttpContent Controller:HttpContextBase View:HttpContext.Current View的搜寻顺序:本文件夹.本共享.根共享等 class=" ...

随机推荐

  1. 【IOS】Xcode7以上免证书真机调试

    Xcode7之前,想要真机调试,必须花99刀购买开发者账号,而且步骤繁琐,需要下载证书.随着Xcode7的推出,大幅度的简化了真机调试的步骤,对ios开发工作者和正在学习ios开发的众多码农们,可以说 ...

  2. navicat自动备份数据

    1.打开navicat客户端,连上mysql后,双击左边你想要备份的数据库.点击"计划",再点击"新建批处理作业". 2.双击上面的可用任务,它就会到下面的列表 ...

  3. css大小单位px em rem的转换和详解

    css大小单位px em rem的转换和详解 PX特点1. IE无法调整那些使用px作为单位的字体大小:2. 国外的大部分网站能够调整的原因在于其使用了em或rem作为字体单位:3. Firefox能 ...

  4. C/C++编译链接过程详解

    有些人写C/C++(以下假定为C++)程序,对unresolved external link或者duplicated external simbol的错误信息不知所措(因为这样的错误信息不能定位到某 ...

  5. JS-数组的方法

    var arr = [ 1,2,3 ];arr.push( 'abc' );//从后面加 arr.unshift( 0 );//从前面加 arr.pop()//从后面删除 arr.shift()//从 ...

  6. flask_用户信息和头像

    一.用户信息页 1.创建视图函数(microblog.py) @app.route('/user/<nickname>') @login_required def user(nicknam ...

  7. SqlServer自动化分区

    1.新增文件组 ALTER DATABASE [Test] ADD FILEGROUP FG2010 ALTER DATABASE [Test] ADD FILEGROUP FG2011 ALTER ...

  8. ANSI C 所有的转义字符

    \a 响铃符 \b 回退符 \f 换页符 \n 换行符 \r 回车符 \t 横向制表符 \v 纵向制表符 \\ 反斜杠 \? 问号 \' 单引号 \" 双引号 \000 八进制数 \xhh ...

  9. Object-C中动态类型对象相关操作汇总

    Object-C(以后简称OC)中有id类型,相对于明确定义类型的静态类型,称为动态类型. 使用动态类型,配合多态(不同类型拥有同名方法),动态绑定(运行时决定实际调用的方法)可以将很多判断延迟到运行 ...

  10. C++ 中 int 转string, 以及10进制转2进制

    感谢:http://blog.csdn.net/xiaofei2010/article/details/7434737 以及:http://www.cnblogs.com/nzbbody/p/3504 ...