基于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 ...
随机推荐
- django使用表单
假设你想从表单接收用户名数据,一般情况下,你需要在HTML中手动编写一个如下的表单元素: <form action="/your-name/" method="po ...
- java类加载器和双亲委派模型
一. 类加载器 ClassLoader即常说的类加载器,其功能是用于从Class文件加载所需的类,主要场景用于热部署.代码热替换等场景. 系统提供3种的类加载器:Bootstrap ClassLoad ...
- java高并发解决方案
高并发的解决方法有两种: 1.使用缓存 2.使用生成静态页面: (代码质量,不要性能低下的sql和代码.有的一条sql搞定的事,有人用了多个循环才能搞定.取决于程序员的经验!(还有就是从最基础的地方优 ...
- Notepad++安装json插件
安装 : 1.下载插件压缩包并解压出dll:NPPJSONViewer.dll(64位) 下载地址:https://pan.baidu.com/s/1JeBzrovb-GHRo14vO-AnJA 提 ...
- BGP - 3,BGP重要概念(EBGP,IBGP,防环/黑洞/全互连/同步)
1,防环/黑洞/同步/全互连(为出现大于号,现在通常都是要下一跳可达+关同步) a)EBGP邻居传来的路由可以通过AS_PATH防环,所以收到的不会有问题,因此直接是优化的(>),也就是直接装表 ...
- Vue.js示例:GitHub提交(watch数据,created钩子,filters过滤); 网格组件(功能:1.检索,2排序);
GitHub提交 codePen: https://codepen.io/chentianwei411/pen/wEVPZo 注意:频繁看案例,可能会被限制. 重点: 表单输入绑定, 单选按钮的使 ...
- 并查集-解决区间和纠错问题 hdu-3038
题目:多次给出信息,告诉你[a,b]区间的和,求多少个错误信息(错误信息不考虑). 乍一看有点像线段树,但想想就发现这个并不能用线段树方便地解决.后来经提醒是并查集的一种经典题型. 把区间抽象为并查集 ...
- vue基础 (三) 自动化工具(Vue CIL)
一.自动化工具(Vue CIL) 安装过程 1. 先安装nvm 参考:https://www.jianshu.com/p/d0e0935b150a https://www.cnblogs.com/tj ...
- SPL之AccessArray
<?php /** * Class MyArrayAccess * 提供像访问数组一样访问对象的能力的接口 */ class MyArrayAccess implements ArrayAcce ...
- SP10707 COT2 - Count on a tree II (树上莫队)
大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...