基于Quartz.NET 实现可中断的任务(转)
Quartz.NET 是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等。 Quartz.NET 允许开发人员根据时间间隔(或天)来调度作业。它实现了作业和触发器的多对多关系,还能把多个作业与不同的触发器关联。整合了 Quartz.NET 的应用程序可以重用来自不同事件的作业,还可以为一个事件组合多个作业。 在 Quartz.NET 的默认实现中 Worker 并非后台线程( IsBackground= false ),所以当我们终止调度器(调用 Scheduler.Shutdown() 方法)时,假如有一个比较耗时的 Job 正在执行,那么进程将不会立即结束,而是等待这个 Job 执行完毕再结束。
为了可以立即退出进程,我们需要了解一个 Quartz.NET 中内置的接口 : IInterruptableJob 。该接口表示一个可中断的任务,实现该接口的 Job 被认为是可以被中断执行的,下面是官方对 IInterruptableJob 接口的定义和解释:
The interface to be implemented by Quartz.IJobs that provide a mechanism for having their execution interrupted. It is NOT a requirement for jobs to implement this interface - in fact, for most people, none of their jobs will. The means of actually interrupting the Job must be implemented within the Quartz.IJob itself (the Quartz.IInterruptableJob.Interrupt method of this interface is simply a means for the scheduler to inform the Quartz.IJob that a request has been made for it to be interrupted). The mechanism that your jobs use to interrupt themselves might vary between implementations. However the principle idea in any implementation should be to have the body of the job's Quartz.IJob.Execute(Quartz.IJobExecutionContext) periodically check some flag to see if an interruption has been requested, and if the flag is set, somehow abort the performance of the rest of the job's work. An example of interrupting a job can be found in the source for the class Example7's DumbInterruptableJob It is legal to use some combination of System.Threading.Monitor.Wait(System.Object) and System.Threading.Monitor.Pulse(System.Object) synchronization within System.Threading.Thread.Interrupt and Quartz.IJob.Execute(Quartz.IJobExecutionContext) in order to have the System.Threading.Thread.Interrupt method block until the Quartz.IJob.Execute(Quartz.IJobExecutionContext) signals that it has noticed the set flag. If the Job performs some form of blocking I/O or similar functions, you may want to consider having the Quartz.IJob.Execute(Quartz.IJobExecutionContext) method store a reference to the calling System.Threading.Thread as a member variable. Then the implementation of this interfaces System.Threading.Thread.Interrupt method can call System.Threading.Thread.Interrupt on that Thread. Before attempting this, make sure that you fully understand what System.Threading.Thread.Interrupt does and doesn't do. Also make sure that you clear the Job's member reference to the Thread when the Execute(..) method exits (preferably in a finally block).
该接口定义了 Interrupt 方法,当调用 Scheduler.Shutdown() 方法时,Quartz.IScheduler 将会调用该方法来中断正在运行的任务。这就意味着,我们需要自己实现中断方法来停止当前的 Job 。 通常我们可以通过在任务执行时拿到当前工作的线程,并在中断时调用线程 Abort 方法的方式来终止当前任务。
public class CommonInterruptableJob : IInterruptableJob
{
private Thread _currentThread; public void Execute(IJobExecutionContext context)
{
_currentThread = Thread.CurrentThread;
try
{
//TODO:编写你的任务代码
}
finally
{
_currentThread = null;
}
} public void Interrupt()
{
if (_currentThread != null)
{
_currentThread.Abort();
_currentThread = null;
}
}
}
这种方法简单粗暴,在一些要求不太严格的情况下表现令人满意。更为优雅的方式是定义布尔型字段 _stop 默认为 false ,在 Interrupt 方法被调用时将其设置为 true 。在 Execute 时不断检测该字段的值,并在合适的时机退出处理。
public class NiceInterruptableJob : IInterruptableJob
{
private bool _stop; public void Execute(IJobExecutionContext context)
{
//假设我的任务是循环 1000 次处理某数据
for (var i = 0; !_stop && i < 1000; i++)
{
//TODO:处理代码
}
} public void Interrupt()
{
_stop = true;
}
}
本片文章对 Quartz.NET 进行了一个简单的介绍,且展示了两种不同的实现任务终止的方式。方式一简单粗暴,编写简单,适合大多数要求不太严格的场合。方式二更加优雅,对退出时机的掌控更加精确,不容易出现危险,但编写更加复杂。
原文:http://www.cnblogs.com/Soar1991/p/7228397.html
基于Quartz.NET 实现可中断的任务(转)的更多相关文章
- 基于 Quartz.NET 实现可中断的任务
基于 Quartz.NET 实现可中断的任务 Quartz.NET 是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET 允许开发 ...
- 基于Quartz实现简单的定时发送邮件
一.什么是Quartz Quartz 是一个轻量级任务调度框架,只需要做些简单的配置就可以使用:它可以支持持久化的任务存储,即使是任务中断或服务重启后,仍可以继续运行.Quartz既可以做为独立的应用 ...
- Quartz.NET总结(五)基于Quartz.net 的开源任务管理平台
前面总结了很多,关于Quartz.net 的文章,介绍了如何使用Quartz.net.不清楚的朋友,可以看我之前的系列文章,http://www.cnblogs.com/zhangweizhong/c ...
- 基于Quartz.NET构建自己的动态作业调度器
在日常的开发中,运行定时任务基本上已经是很普遍的需求了,可以通过windows服务+timer组件来实现,也可以使用第三方框架来集成,Quartz.NET就是一款从JAVA的Quartz移植过来的一个 ...
- 任务调度之持久化(基于Quartz.net)
上一篇我们了解了任务调度及他的远端管理方式,传送门:任务调度及远端管理(基于Quartz.net) 这篇我们要完成任务调度的持久化功能,即新增修改删除之类的功能,这必须得要有的,不然都不知道后台都有什 ...
- 任务调度之集群(基于Quartz.net)
上一篇我们完成了任务调度的持久化,传送门:任务调度之持久化(基于Quartz.net) 这篇我们来完成Quartz.net的一个比较优秀的功能,即集群:集群可以提高任务调度服务的容灾性, 当一个节点宕 ...
- JobEngine 基于quartz.net 跨平台作业框架
github:https://github.com/zzhi/JobEngine 基于quartz.net 的跨平台作业框架 quartz.net(https://github.com/quartzn ...
- Java 基于quartz实现定时 之二(XML方式配置)
<!-- 在spring核心配置文件中进行如下配置 --> <!-- Spring基于quartz定时任务 --> <bean id="triggerByBea ...
- RDIFramework.NET框架基于Quartz.Net实现任务调度详解及效果展示
在上一篇Quartz.Net实现作业定时调度详解,我们通过实例代码详细讲解与演示了基于Quartz.NET开发的详细方法.本篇我们主要讲述基于RDIFramework.NET框架整合Quartz.NE ...
随机推荐
- 如何对接oracle 建立pdb
Oracle数据库的结构是一个数据库实例下有许多用户,每一个用户有自己的表空间,即每一个用户相当于MySQL中的一个数据库.不久前下了oracle 12c的数据库,安装之后建user时才知道oracl ...
- Codeforces 1053 B - Vasya and Good Sequences
B - Vasya and Good Sequences 思路: 满足异或值为0的区间,必须满足一下条件: 1.区间中二进制1的个数和为偶数个; 2.区间二进制1的个数最大值的两倍不超过区间和. 如果 ...
- 手机网页唤醒app,
1.在系统系统自带的浏览器中 首先做成HTML的页面,页面内容格式如下: <a href="[scheme]://[host]/[path]?[query]">启动应用 ...
- vue 导出excel表格
对于涉及到数据比较多的管理后台导出excel 表格这个需求就非常的常见了 所以? vue 怎么到处excel表格的? 有两种办法 1:请求接口后台直接给你的是excel文件,你需要做的就是怎么接收ex ...
- Python生成pyd文件
Python的脚本文件是开源的,量化策略的安全性没有保障.因此需要保护源码.那么要对Python代码进行混淆.加密保护. 混淆代码,我准备使用pyminifier.而加密处理,就比较麻烦. Pytho ...
- 全栈性能测试修炼宝典--Jmeter实战(三)
JMeter体系结构 1.JMeter体系结构 (1)名词解释 元件:JMeter工具菜单中的一个子菜单,比如HTTP请求.事务控制器.响应断言就是一个元件. 组件:一组元件的集合,比如逻辑控制器中有 ...
- 最大似然估计实例 | Fitting a Model by Maximum Likelihood (MLE)
参考:Fitting a Model by Maximum Likelihood 最大似然估计是用于估计模型参数的,首先我们必须选定一个模型,然后比对有给定的数据集,然后构建一个联合概率函数,因为给定 ...
- H.264开源解码器评测
转自:http://wmnmtm.blog.163.com/blog/static/38245714201142883032575/ 要播放HDTV,就首先要正确地解开封装,然后进行视频音频解码.所以 ...
- 自己动手编写IPv4地址包含关系测试的小脚本
工作中需要对地址包含关系进行测试,现有ipaddress标准库和IPy无法满足,于是自己动手编写小脚本,主要实现== , in, <等专用功能,其他功能可以后续用到再补充,例如迭代打印网段内所有 ...
- P3911 最小公倍数之和
终于找到了一个只会用[gcd(i,j)==1] = sigema d|gcd(i,j) mu(d) 做不了的题. 考虑枚举gcd后. 此时,ans可以表示为一个 sigema x f(x)的形式. 考 ...