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 实现可中断的任务(转)的更多相关文章

  1. 基于 Quartz.NET 实现可中断的任务

    基于 Quartz.NET 实现可中断的任务 Quartz.NET 是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET 允许开发 ...

  2. 基于Quartz实现简单的定时发送邮件

    一.什么是Quartz Quartz 是一个轻量级任务调度框架,只需要做些简单的配置就可以使用:它可以支持持久化的任务存储,即使是任务中断或服务重启后,仍可以继续运行.Quartz既可以做为独立的应用 ...

  3. Quartz.NET总结(五)基于Quartz.net 的开源任务管理平台

    前面总结了很多,关于Quartz.net 的文章,介绍了如何使用Quartz.net.不清楚的朋友,可以看我之前的系列文章,http://www.cnblogs.com/zhangweizhong/c ...

  4. 基于Quartz.NET构建自己的动态作业调度器

    在日常的开发中,运行定时任务基本上已经是很普遍的需求了,可以通过windows服务+timer组件来实现,也可以使用第三方框架来集成,Quartz.NET就是一款从JAVA的Quartz移植过来的一个 ...

  5. 任务调度之持久化(基于Quartz.net)

    上一篇我们了解了任务调度及他的远端管理方式,传送门:任务调度及远端管理(基于Quartz.net) 这篇我们要完成任务调度的持久化功能,即新增修改删除之类的功能,这必须得要有的,不然都不知道后台都有什 ...

  6. 任务调度之集群(基于Quartz.net)

    上一篇我们完成了任务调度的持久化,传送门:任务调度之持久化(基于Quartz.net) 这篇我们来完成Quartz.net的一个比较优秀的功能,即集群:集群可以提高任务调度服务的容灾性, 当一个节点宕 ...

  7. JobEngine 基于quartz.net 跨平台作业框架

    github:https://github.com/zzhi/JobEngine 基于quartz.net 的跨平台作业框架 quartz.net(https://github.com/quartzn ...

  8. Java 基于quartz实现定时 之二(XML方式配置)

    <!-- 在spring核心配置文件中进行如下配置 --> <!-- Spring基于quartz定时任务 --> <bean id="triggerByBea ...

  9. RDIFramework.NET框架基于Quartz.Net实现任务调度详解及效果展示

    在上一篇Quartz.Net实现作业定时调度详解,我们通过实例代码详细讲解与演示了基于Quartz.NET开发的详细方法.本篇我们主要讲述基于RDIFramework.NET框架整合Quartz.NE ...

随机推荐

  1. jsp导入数据库数据写法(模板)

    1.导入表格模板 <%@ page language="java" contentType="text/html; charset=utf-8" page ...

  2. 力扣(LeetCode)258. 各位相加

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...

  3. 基于虹软sdk,java实现人脸识别(demo)

    ## 开发环境准备:###开发使用到的软件和工具:* Jdk8.mysql5.7.libarcsoft_face.dll(so).libarcsoft_face_engine.dll(so).liba ...

  4. Lua报错:invalid key to 'next'

    1.问题产生的原因是,在一个循环里对table中的元素先进行置空操作,再进行增加新元素的操作,就会报这个错误. 2.比如下面的例子:(当中间的函数足够复杂并进行封装了的情况下,不会留意到存在这个问题) ...

  5. Codeforces 493C - Vasya and Basketball

    C. Vasya and Basketball 题目链接:http://codeforces.com/problemset/problem/493/C time limit per test 2 se ...

  6. Visual Studio 2015+InstallShield 2015

    下载Installshield http://learn.flexerasoftware.com/content/IS-EVAL-InstallShield-Limited-Edition-Visua ...

  7. Python __init__.py 文件使用

    __init__.py的主要作用是: 1. Python中package的标识,不能删除 2. 定义__all__用来模糊导入 3. 编写Python代码(不建议在__init__中写python模块 ...

  8. DirectX之顶点法线的计算

    首先要明白,顶点法线存在的原因:确定灯光照射到物体表面的角度.所以一提到顶点法线,肯定要进行与灯光相关的运算了. 下面是顶点法线的计算方式 假如 A.B.C三个顶点构成一个三角形,它们对应的顶点法线分 ...

  9. MyBatis-session-SqlSession

    The SqlSession instance is the most powerful in MyBatis. It is where you'll find all of the methods ...

  10. Hibernate相关的查询 --Hibernate框架基础

    接着上一篇博文:Hibernate第一个程序(最基础的增删改查) --Hibernate本例是对Hibernate查询的扩展,使用HQL语句查询 /** * HQL添加预先需要保存的测试数据 */ @ ...