【Quartz学习总结】——入门程序

2024-08-26 15:48

本文主要是介绍【Quartz学习总结】——入门程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

   

   Quartz是什么

    Quartz是OpenSystemphony开源组织在Job scheduling领域哟一个开源项目,他可以域J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单活为运行十个、百个,甚至好几万个Jobs这样复杂的日程序表。Jobs可以做成标准的Java组件或EJBs.

    Quartz是一个任务日程管理系统,一个预先确定(被纳入日程)的时间到达时,负责执行(或通知)其他软件组件的系统。

    Quartz是一个小java库发布文件(.jar文件),这个库文件包含了所有uartz核心功能。这些功能注解接口(API)是Scheduler接口。他提供了简单的操作,例如:将任务纳入日程或者从日程中取消,开始、停止、暂停日程进度。

   核心接口

    Scheduler——核心调度器

    Job——任务

    JobDetai——任务描述

    Trigger——触发器

   执行过程

   

    Trigger

    SimpleTrigger   简单触发

    SimpleTrigger用来触发只需执行一次或者在给定时间触发并且重复N次且每次执行延迟一定时间的任务。
如果你想让触发器在2014年1月11日,上午11:23:54秒执行,然后每个隔10秒钟重复执行一次,并且这样重复5次。那么SimpleTrigger 就可以满足你的要求。

    CronTrigger    表达式触发

    如果你需要像日历那样按日程来触发任务,而不是像SimpleTrigger 那样每隔特定的间隔时间触发,CronTriggers通常比SimpleTrigger更有用。
使用CronTrigger,你可以指定诸如“每个周五中午”,或者“每个工作日的9:30”或者“从每个周一、周三、周五的上午9:00到上午10:00之间每隔五分钟”这样日程安排来触发。甚至,象SimpleTrigger一样,CronTrigger也有一个startTime以指定日程从什么时候开始,也有一个(可选的)endTime以指定何时日程不再继续。

   关于时间表达式的编写:在下一篇博客中进行详细的介绍。

   可以这样说:凡是SimpleTrigger能完成的任务,CronTrigger表达式都可以完成

  官方Demo演示

   添加依赖:

        <dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>2.2.1</version></dependency>

   Job类代码:

    

/* * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * *   http://www.apache.org/licenses/LICENSE-2.0 *   * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License.* */package cn.itcast.quartz.example;import java.util.Date;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;/*** <p>* This is just a simple job that says "Hello" to the world.* </p>* * @author Bill Kratzer*/
public class HelloJob implements Job {private static Logger _log = LoggerFactory.getLogger(HelloJob.class);/*** <p>* Empty constructor for job initilization* </p>* <p>* Quartz requires a public empty constructor so that the* scheduler can instantiate the class whenever it needs.* </p>*/public HelloJob() {}/*** <p>* Called by the <code>{@link org.quartz.Scheduler}</code> when a* <code>{@link org.quartz.Trigger}</code> fires that is associated with* the <code>Job</code>.* </p>* * @throws JobExecutionException*             if there is an exception while executing the job.*/public void execute(JobExecutionContext context)throws JobExecutionException {// Say Hello to the World and display the date/time_log.info("Hello World! - " + new Date());}}

  表达式触发器代码:

   

/* * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * *   http://www.apache.org/licenses/LICENSE-2.0 *   * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License.* */package cn.itcast.quartz.example;import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.Date;/*** This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule* a job to run in Quartz.* * @author Bill Kratzer*/
public class SimpleCronExample {public void run() throws Exception {Logger log = LoggerFactory.getLogger(SimpleCronExample.class);log.info("------- Initializing ----------------------");// 定义调度器SchedulerFactory sf = new StdSchedulerFactory();Scheduler sched = sf.getScheduler();log.info("------- Initialization Complete -----------");// 获取当前时间的下一分钟Date runTime = evenMinuteDate(new Date());log.info("------- Scheduling Job  -------------------");// 定义jobJobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();// 定义触发器,每2秒执行一次Trigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0 0/1 * * * ?")).build();// 将job注册到调度器sched.scheduleJob(job, trigger);log.info(job.getKey() + " will run at: " + runTime);// 启动调度器sched.start();log.info("------- Started Scheduler -----------------");// 等待1分钟log.info("------- Waiting 60 seconds... -------------");try {Thread.sleep(60L * 1000L);} catch (Exception e) {//}// 关闭调度器log.info("------- Shutting Down ---------------------");sched.shutdown(true);log.info("------- Shutdown Complete -----------------");}public static void main(String[] args) throws Exception {SimpleCronExample example = new SimpleCronExample();example.run();}}

   简单才触发器代码:

   

/* * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * *   http://www.apache.org/licenses/LICENSE-2.0 *   * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License.* */package cn.itcast.quartz.example;import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.Date;/*** This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule* a job to run in Quartz.* * @author Bill Kratzer*/
public class SimpleExample {public void run() throws Exception {Logger log = LoggerFactory.getLogger(SimpleExample.class);log.info("------- Initializing ----------------------");// 定义调度器SchedulerFactory sf = new StdSchedulerFactory();Scheduler sched = sf.getScheduler();log.info("------- Initialization Complete -----------");// 获取当前时间的下一分钟Date runTime = evenMinuteDate(new Date());log.info("------- Scheduling Job  -------------------");// 定义job// 在quartz中,有组的概念,组+job名称 唯一的JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();// 定义触发器,在下一分钟启动Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();// 将job注册到调度器sched.scheduleJob(job, trigger);log.info(job.getKey() + " will run at: " + runTime);// 启动调度器sched.start();log.info("------- Started Scheduler -----------------");// 等待65秒log.info("------- Waiting 65 seconds... -------------");try {// wait 65 seconds to show jobThread.sleep(65L * 1000L);// executing...} catch (Exception e) {//}// 关闭调度器log.info("------- Shutting Down ---------------------");sched.shutdown(true);log.info("------- Shutdown Complete -----------------");}public static void main(String[] args) throws Exception {SimpleExample example = new SimpleExample();example.run();}}

   在下一篇博客中详细介绍,表达式触发器中表达式的编写。

这篇关于【Quartz学习总结】——入门程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1108994

相关文章

Windows Docker端口占用错误及解决方案总结

《WindowsDocker端口占用错误及解决方案总结》在Windows环境下使用Docker容器时,端口占用错误是开发和运维中常见且棘手的问题,本文将深入剖析该问题的成因,介绍如何通过查看端口分配... 目录引言Windows docker 端口占用错误及解决方案汇总端口冲突形成原因解析诊断当前端口情况解

Java程序进程起来了但是不打印日志的原因分析

《Java程序进程起来了但是不打印日志的原因分析》:本文主要介绍Java程序进程起来了但是不打印日志的原因分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java程序进程起来了但是不打印日志的原因1、日志配置问题2、日志文件权限问题3、日志文件路径问题4、程序

Spring Boot 集成 Quartz并使用Cron 表达式实现定时任务

《SpringBoot集成Quartz并使用Cron表达式实现定时任务》本篇文章介绍了如何在SpringBoot中集成Quartz进行定时任务调度,并通过Cron表达式控制任务... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启动 Sprin

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

java常见报错及解决方案总结

《java常见报错及解决方案总结》:本文主要介绍Java编程中常见错误类型及示例,包括语法错误、空指针异常、数组下标越界、类型转换异常、文件未找到异常、除以零异常、非法线程操作异常、方法未定义异常... 目录1. 语法错误 (Syntax Errors)示例 1:解决方案:2. 空指针异常 (NullPoi

Spring Boot 集成 Quartz 使用Cron 表达式实现定时任务

《SpringBoot集成Quartz使用Cron表达式实现定时任务》本文介绍了如何在SpringBoot项目中集成Quartz并使用Cron表达式进行任务调度,通过添加Quartz依赖、创... 目录前言1. 添加 Quartz 依赖2. 创建 Quartz 任务3. 配置 Quartz 任务调度4. 启

Java反转字符串的五种方法总结

《Java反转字符串的五种方法总结》:本文主要介绍五种在Java中反转字符串的方法,包括使用StringBuilder的reverse()方法、字符数组、自定义StringBuilder方法、直接... 目录前言方法一:使用StringBuilder的reverse()方法方法二:使用字符数组方法三:使用自

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

如何用java对接微信小程序下单后的发货接口

《如何用java对接微信小程序下单后的发货接口》:本文主要介绍在微信小程序后台实现发货通知的步骤,包括获取Access_token、使用RestTemplate调用发货接口、处理AccessTok... 目录配置参数 调用代码获取Access_token调用发货的接口类注意点总结配置参数 首先需要获取Ac