问题1 由于业务需要,停止Quartz中正在执行的任务
Quartz:你的任务类只需要实现InterruptableJob类就可以了
只要实现一个方法:interrupt(),在这个方法中进行标记的改变,在执行中进行这个标记判断
就可实现中断任务了,另外在调度器上调用方法:sched.interrupt(job.getKey());
在查看Quartz文档中已经有说明了,如下:
- The interface to be implemented by Jobs 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.
- Interrupting a Job is very analogous in concept and challenge to normal interruption of a Thread in Java.
- The means of actually interrupting the Job must be implemented within the Job itself (the interrupt() method of this interface is simply a means for the scheduler to inform the Job 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 execute(..) 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 java source for the class org.quartz.examples.DumbInterruptableJob. It is legal to use some combination of wait() and notify() synchronization within interrupt() and execute(..) in order to have the interrupt() method block until the execute(..) 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 Job.execute(..) method store a reference to the calling Thread as a member variable. Then the Implementation of this interfaces interrupt() method can call interrupt() on that Thread. Before attempting this, make sure that you fully understand what java.lang.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.
- See Example 7 (org.quartz.examples.example7.DumbInterruptableJob) for a simple implementation demonstration
具体代码如下:
DumbInterruptableJob.java
- package org.quartz.examples.example7;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.quartz.InterruptableJob;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.quartz.JobKey;
- import org.quartz.UnableToInterruptJobException;
- /**
- * @author <a href="mailto:bonhamcm@thirdeyeconsulting.com">Chris Bonham</a>
- * @author Bill Kratzer
- */
- public class DumbInterruptableJob implements InterruptableJob {
- // logging services
- private static Logger _log = LoggerFactory.getLogger(DumbInterruptableJob.class);
- // has the job been interrupted?
- private boolean _interrupted = false;
- // job name
- private JobKey _jobKey = null;
- private static int counts = 0;
- public DumbInterruptableJob() {
- }
- public void execute(JobExecutionContext context)
- throws JobExecutionException {
- _jobKey = context.getJobDetail().getKey();
- _log.error("任务Key: " + _jobKey + " 执行时间: " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
- try {
- for (int i = 0; i < 4; i++) {
- try {
- Thread.sleep(1000L);
- } catch (Exception ignore) {
- ignore.printStackTrace();
- }
- if(_interrupted) {
- _log.error("被外界因素停止了这个任务key: " + _jobKey + ",当前执行次数: " + counts);
- return; // could also choose to throw a JobExecutionException
- // if that made for sense based on the particular
- // job's responsibilities/behaviors
- }
- //执行业务方法
- userManager();
- }
- } finally {
- _log.error("任务执行完成key: " + _jobKey + " 执行时间: " + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
- }
- }
- private void userManager() {
- _log.error("正在执行插入数据库的操作,次数: " + (++counts));
- }
- public void interrupt() throws UnableToInterruptJobException {
- _log.info("外界正在调用调度器停止这个任务key: " + _jobKey);
- _interrupted = true;
- }
- }
InterruptExample.java
- package org.quartz.examples.example7;
- import static org.quartz.JobBuilder.newJob;
- import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
- import static org.quartz.TriggerBuilder.newTrigger;
- import static org.quartz.DateBuilder.*;
- import java.util.Date;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.quartz.SchedulerFactory;
- import org.quartz.SchedulerMetaData;
- import org.quartz.SimpleTrigger;
- import org.quartz.impl.StdSchedulerFactory;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class InterruptExample {
- public void run() throws Exception {
- final Logger log = LoggerFactory.getLogger(InterruptExample.class);
- SchedulerFactory sf = new StdSchedulerFactory();
- Scheduler sched = sf.getScheduler();
- Date startTime = nextGivenSecondDate(null, 15);
- JobDetail job = newJob(DumbInterruptableJob.class)
- .withIdentity("interruptableJob1", "group1")
- .build();
- //当前时间15秒后,每间隔5秒执行一次任务
- SimpleTrigger trigger = newTrigger()
- .withIdentity("trigger1", "group1")
- .startAt(startTime)
- .withSchedule(simpleSchedule()
- .withIntervalInSeconds(5)
- .repeatForever())
- .build();
- Date ft = sched.scheduleJob(job, trigger);
- log.error(job.getKey() + " will run at: " + ft + " and repeat: "
- + trigger.getRepeatCount() + " times, every "
- + trigger.getRepeatInterval() / 1000 + " seconds");
- sched.start();
- for(int i=0; i < 10; i++) {
- try {
- Thread.sleep(7000L);
- sched.interrupt(job.getKey());
- } catch (Exception e) {
- }
- }
- sched.shutdown(true);
- SchedulerMetaData metaData = sched.getMetaData();
- log.error("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
- }
- public static void main(String[] args) throws Exception {
- InterruptExample example = new InterruptExample();
- example.run();
- }
- }
相关推荐
Quartz是Java领域的一款强大的开源任务调度框架,它允许开发者创建和管理定时任务,从而实现应用程序的自动执行功能。在给定的压缩包文件中,我们有两个版本为1.6.0的Quartz JAR包:`quartz-1.6.0.jar`和`quartz-all...
Quartz 是一个开源的作业调度框架,广泛应用于Java应用程序中,用于执行定时任务。它提供了丰富的API和灵活性,使得开发者可以方便地定义、安排和管理各种任务。版本1.8.6是Quartz的一个稳定版本,它包含了对数据库...
Quartz是一款广泛使用的开源任务调度框架,它允许开发者在Java应用程序中定义和执行定时任务。在Quartz 2.2.3版本中,初始化数据库是使用Quartz的关键步骤,因为Quartz依赖于一个持久化存储来保存作业和触发器的信息...
1. **Oracle数据库脚本** (`tables_oracle.sql`): Oracle是关系型数据库管理系统之一,以其高性能和企业级特性著名。Quartz的Oracle脚本会创建如`QRTZ_TRIGGERS`, `QRTZ_JOB_DETAILS`, `QRTZ_CALENDARS`等核心表,...
赠送jar包:quartz-2.3.2.jar; 赠送原API文档:quartz-2.3.2-javadoc.jar; 赠送源代码:quartz-2.3.2-sources.jar; 赠送Maven依赖信息文件:quartz-2.3.2.pom; 包含翻译后的API文档:quartz-2.3.2-javadoc-API...
Quartz 是一个开源的作业调度框架,广泛应用于Java企业级应用中,用于自动化任务执行,如定时触发工作流、发送邮件、数据同步等。在Quartz的部署和配置过程中,为了存储作业和触发器的信息,我们需要在关系型数据库...
在Spring框架中集成Quartz是一款常见的任务调度解决方案,它允许开发者在应用中安排定时任务的执行。Quartz是一个开源的作业调度框架,可以用来在Java应用程序中安排复杂的作业任务。以下将详细介绍如何在Spring中...
**Android Studio下的Quartz工程详解** Quartz是一个开源的作业调度框架,广泛应用于Java环境中的任务调度。在Android Studio中使用Quartz,可以为应用程序添加定时执行的任务功能,例如定期发送通知、更新数据或者...
这个框架的强大之处在于它的灵活性和可扩展性,使得开发者能够创建复杂的调度逻辑,以满足各种自动化需求。以下是对Quartz.NET及其官方源码和演示例子的详细解析。 **Quartz.NET核心概念** 1. **作业(Jobs)**:...
Quartz 批量下载源码,Quartz 批量下载源码Quartz 批量下载源码Quartz 批量下载源码Quartz 批量下载源码Quartz 批量下载源码Quartz 批量下载源码Quartz 批量下载源码Quartz 批量下载源码Quartz 批量下载源码
quartz scheduler 入门教程 Quartz Scheduler 是一种功能丰富、开源的任务调度程序库,可以在任何 Java 程序中使用。它可以用来创建简单或者复杂的执行次数可以达成千上万的任务。任务可以是任何 Java 可以做的事情...
Quartz是一款开源的作业调度框架,它允许开发者在Java应用程序中定义和执行复杂的定时任务。在Java应用开发中,Quartz常被用来自动化各种后台任务,如数据清理、报告生成等。"Quartz所需jar包"是使用Quartz库进行...
Quartz 是一个开源的作业调度框架,它允许开发者在 Java 应用程序中安排任务的执行。线程池是 Quartz 的核心组成部分,用于管理和执行触发的任务。本文将深入探讨 Quartz 线程池的工作原理、配置以及如何在实际项目...
Quartz是一款开源的作业调度框架,它允许开发者创建、组织和执行计划任务。这个实例是为初学者设计的,用于帮助理解Quartz的基本概念和使用方式。在MyEclipse 6.0.1环境下,你可以直接运行这个Spring整合Quartz的...
Quartz和Spring-Quartz是两个在Java世界中广泛使用的定时任务管理框架。Quartz是一个开源的作业调度框架,允许应用程序定义和调度任务在特定时间执行。而Spring-Quartz则是Spring框架对Quartz的集成,它使得在Spring...
赠送jar包:quartz-2.3.0.jar; 赠送原API文档:quartz-2.3.0-javadoc.jar; 赠送源代码:quartz-2.3.0-sources.jar; 赠送Maven依赖信息文件:quartz-2.3.0.pom; 包含翻译后的API文档:quartz-2.3.0-javadoc-API...
Quartz是一个功能丰富的开源作业调度库,几乎可以集成在任何Java应用程序中 - 从最小的独立应用程序到最大的电子商务系统。Quartz可用于创建简单或复杂的计划,以执行数十,数百甚至数万个作业; 将任务定义为标准...
Quartz是一个开源的作业调度框架,它允许开发者在应用程序中定义和执行定时任务。这个标题“Quartz 定时WebForm和WinForm使用的dll”暗示了我们将在WebForm和WinForm应用中使用Quartz来实现定时功能。在.NET环境中,...
Quartz是一款开源的作业调度框架,它允许开发者在应用程序中安排任务执行,比如定时执行某个方法或服务。在“quartz实例sqlserver数据库连接”这个主题中,我们主要讨论如何配置Quartz与SQL Server数据库进行交互,...
Quartz.NET是一款强大的开源作业调度框架,用于在.NET环境中创建和执行定时任务。这个"Quartz.net-定时任务 Demo"示例将展示如何利用Quartz.NET来安排代码在指定时间后执行,比如几十分钟后的场景。 Quartz.NET的...