任务类

 package org.quartz.examples.example2;

 import java.util.Date;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey; /**
* 任务job
*/
public class SimpleJob implements Job { private static Logger LOG = LoggerFactory.getLogger(SimpleJob.class); // 必须要有public修饰的无参构造函数
public SimpleJob() {
} // 任务执行方法
public void execute(JobExecutionContext context) throws JobExecutionException {
JobKey jobKey = context.getJobDetail().getKey();  //jobKey长这样:group1.job6
LOG.info("执行任务。SimpleJob says: " + jobKey + " executing at " + new Date());
} }

调度管理类

package org.quartz.examples.example2;

import static org.quartz.DateBuilder.futureDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.JobKey.jobKey;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger; import org.quartz.DateBuilder;
import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
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; import java.util.Date; /**
* 这个例子表明,只要触发器运用的好,job任务可以被花式执行
*/
public class SimpleTriggerExample {
Logger LOG = LoggerFactory.getLogger(SimpleTriggerExample.class); public void run() throws Exception { // 初始化一个调度工厂,并实例化一个调度类
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
// 第一个参数:null就是默认当前时间,也可以指定时间
// 第二个参数:把一分钟按15进行划分,也就是60/15等份。
// 举例:当前时间是10:26:04,那么startTime就是10:26:15。当前时间是10:26:31,那么startTime就是10:26:45。
Date startTime = DateBuilder.nextGivenSecondDate(null, 15); // ========================================================
// ============ job1 任务绑定到group1 组中 ==================
// ========================================================
JobDetail job = newJob(SimpleJob.class).withIdentity("job1", "group1").build();
// 实例化一个触发器,命名为trigger1,并绑定到一个名为group1的组中,Job类运行开始时间为runTime(下一分钟)
SimpleTrigger trigger = (SimpleTrigger) newTrigger().withIdentity("trigger1", "group1").startAt(startTime)
.build();
Date date = scheduler.scheduleJob(job, trigger);
LOG.info(job.getKey() + " will run at: " + date + " and repeat: " + trigger.getRepeatCount() + " times, every "
+ trigger.getRepeatInterval() / 1000 + " seconds"); // ========================================================
// ============ job2 任务绑定到group1 组中 ==================
// ========================================================
job = newJob(SimpleJob.class).withIdentity("job2", "group1").build();
trigger = (SimpleTrigger) newTrigger().withIdentity("trigger2", "group1").startAt(startTime).build();
date = scheduler.scheduleJob(job, trigger);
LOG.info(job.getKey() + " will run at: " + date + " and repeat: " + trigger.getRepeatCount() + " times, every "
+ trigger.getRepeatInterval() / 1000 + " seconds"); // ====================================================================
// ============ job3 任务绑定到group1 组中,运行一次每间隔10秒重复1次,总共重复10次
// ====================================================================
job = newJob(SimpleJob.class).withIdentity("job3", "group1").build();
trigger = newTrigger().withIdentity("trigger3", "group1").startAt(startTime)
.withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(10)).build();
date = scheduler.scheduleJob(job, trigger);
LOG.info(job.getKey() + " will run at: " + date + " and repeat: " + trigger.getRepeatCount() + " times, every "
+ trigger.getRepeatInterval() / 1000 + " seconds");
// ====================================================================
// ============ job3 任务绑定到group2 组中,将被另一个触发器调度,这一次将以70秒的间隔重复两次
// ====================================================================
trigger = newTrigger().withIdentity("trigger3", "group2").startAt(startTime)
.withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(2)).forJob(job).build(); date = scheduler.scheduleJob(trigger);
LOG.info(job.getKey() + " will [also] run at: " + date + " and repeat: " + trigger.getRepeatCount()
+ " times, every " + trigger.getRepeatInterval() / 1000 + " seconds"); // ====================================================================
// ============ job4 将被执行6次,执行一次重复5次,每次间隔10秒
// ====================================================================
job = newJob(SimpleJob.class).withIdentity("job4", "group1").build();
trigger = newTrigger().withIdentity("trigger4", "group1").startAt(startTime)
.withSchedule(simpleSchedule().withIntervalInSeconds(10).withRepeatCount(5)).build();
date = scheduler.scheduleJob(job, trigger);
LOG.info(job.getKey() + " will run at: " + date + " and repeat: " + trigger.getRepeatCount() + " times, every "
+ trigger.getRepeatInterval() / 1000 + " seconds"); // ====================================================================
// ============ job5 将在5分钟后执行一次
// ====================================================================
job = newJob(SimpleJob.class).withIdentity("job5", "group1").build();
trigger = (SimpleTrigger) newTrigger().withIdentity("trigger5", "group1")
.startAt(futureDate(5, IntervalUnit.MINUTE)).build();
date = scheduler.scheduleJob(job, trigger);
LOG.info(job.getKey() + " will run at: " + date + " and repeat: " + trigger.getRepeatCount() + " times, every "
+ trigger.getRepeatInterval() / 1000 + " seconds"); // ====================================================================
// ============ job6 将被无限期地执行,每隔40秒执行一次
// ====================================================================
job = newJob(SimpleJob.class).withIdentity("job6", "group1").build();
trigger = newTrigger().withIdentity("trigger6", "group1").startAt(startTime)
.withSchedule(simpleSchedule().withIntervalInSeconds(40).repeatForever()).build();
date = scheduler.scheduleJob(job, trigger);
LOG.info(job.getKey() + " will run at: " + date + " and repeat: " + trigger.getRepeatCount() + " times, every "
+ trigger.getRepeatInterval() / 1000 + " seconds"); // ====================================================================
// ============ 启动调度器
// ====================================================================
scheduler.start(); // ====================================================================
// ============ 在调度器启动之后,新增加的job7仍将可以被调度,job7将5分钟执行一次,总共重复20次
// ====================================================================
job = newJob(SimpleJob.class).withIdentity("job7", "group1").build();
trigger = newTrigger().withIdentity("trigger7", "group1").startAt(startTime)
.withSchedule(simpleSchedule().withIntervalInMinutes(5).withRepeatCount(20)).build();
date = scheduler.scheduleJob(job, trigger);
LOG.info(job.getKey() + " will run at: " + date + " and repeat: " + trigger.getRepeatCount() + " times, every "
+ trigger.getRepeatInterval() / 1000 + " seconds"); // ====================================================================
// ============ job8绑定到group1,只有定义好触发器,才会被执行
// ====================================================================
job = newJob(SimpleJob.class).withIdentity("job8", "group1").storeDurably().build();
scheduler.addJob(job, true); // job添加到任务调度器
scheduler.triggerJob(jobKey("job8", "group1")); // 手动触发job8,绑定到group1 try {
Thread.sleep(30L * 1000L); // 等待30秒,让任务都执行完毕
} catch (Exception e) {
} /**
* 终止调度。
* 但是如果当前仍然有任务正在运行中,则会等待该任务执行完毕再终止,期间debug信息会打印:
* 21:48:54.965 [main] DEBUG org.quartz.simpl.SimpleThreadPool - Waiting for thread TestScheduler_Worker-1 to shut down
*/
scheduler.shutdown(true); // 显示调度器的一些统计信息
SchedulerMetaData metaData = scheduler.getMetaData();
LOG.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs."); } // 演示触发器被重新调度案例
private void rescheduleJobDemo() throws SchedulerException {
// 初始化一个调度工厂,并实例化一个调度类
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler scheduler = sf.getScheduler();
// 第一个参数:null就是默认当前时间,也可以指定时间
// 第二个参数:把一分钟按30进行划分,也就是60/30等份。
// 举例:当前时间是21:01:27,那么startTime就是21:01:30。当前时间是21:01:32,那么startTime就是21:02:00。
Date startTime = DateBuilder.nextGivenSecondDate(null, 15); // ====================================================================
// ============ job600 将被无限期地执行,每隔5秒执行一次,由trigger2019触发
// ====================================================================
JobDetail job = newJob(SimpleJob.class).withIdentity("job600", "group1").build();
SimpleTrigger trigger = newTrigger().withIdentity("trigger2019", "group1").startAt(startTime)
.withSchedule(simpleSchedule().withIntervalInSeconds(5).repeatForever()).build();
Date date = scheduler.scheduleJob(job, trigger);
LOG.info(job.getKey() + " will run at: " + date + " and repeat: " + trigger.getRepeatCount() + " times, every "
+ trigger.getRepeatInterval() / 1000 + " seconds");
// ====================================================================
// ============ 启动调度器
// ====================================================================
scheduler.start(); try {
System.out.println("线程进入睡眠");
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程被唤醒");
// ====================================================================
// ============ job600 被重新定义的trigger2019触发器重新调度,改为每1秒钟执行一次
// ====================================================================
SimpleTrigger trigger2 = newTrigger().withIdentity("trigger2019", "group1").startAt(startTime)
.withSchedule(simpleSchedule().withIntervalInSeconds(1).repeatForever()).build();
Date date2 = scheduler.rescheduleJob(trigger2.getKey(), trigger2);
LOG.info("job600 rescheduled to run at: " + date2);
} public static void main(String[] args) throws Exception {
SimpleTriggerExample example = new SimpleTriggerExample();
example.run();
example.rescheduleJobDemo(); // 重新调度案例
} }

quartz2.3.0(二)触发器Trigger花式Scheduler调度job的更多相关文章

  1. 04 Zabbix4.0系统配置触发器trigger

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 04 Zabbix4.0系统配置触发器trigger 请点击查看Zabbix3.0.8版本trig ...

  2. quartz2.3.0(三)cron定义调度周期

    cron总结 cron详解参见:<quartz CronExpression表达式> CronTrigger配置完整格式为7个: [秒]   [分]   [小时]   [日]   [月]  ...

  3. quartz2.3.0系列目录——带您由浅入深全面掌握quartz2.3.0

    quartz2.3.0系列目录 官网下载地址:http://www.quartz-scheduler.org/downloads/ 本系列demo全部来源于官网,仅仅是简化和汉化了注释!一部分代码de ...

  4. quartz2.3.0(十三)数据库持久化定时器job任务和trigger触发器,在多个调度器实例情况下,由其它调度器实例恢复执行调度器宕机的job任务

    一.初始化数据库11张quartz表:qrtz_*   先从官网下载好quartz2.3.0包:http://www.quartz-scheduler.org/downloads/ 解压后进入目录:q ...

  5. quartz2.3.0(十二)通过RMI协议向Scheduler调度器远程添加job任务

    此代码示例通过RMI协议向Scheduler调度器远程添加job任务. 代码文件包括:job任务类(SimpleJob.java).RMI服务端server类(RemoteServerExample. ...

  6. quartz2.3.0(十)xml配置方式定义quartz定时任务

    1.新增pom依赖 除了按照<quartz2.3.0系列目录——带您由浅入深全面掌握quartz2.3.0>添加依赖之外,pom.xml里新增加依赖: <dependency> ...

  7. spring3.2.8+quartz2.2.0(比较全,对比quartz1.x的配置)

    spring3.2.8 + quartz2.2.0报错: java.lang.IncompatibleClassChangeError: class org.springframework.sched ...

  8. 如何使用MySQL触发器trigger

    阅读目录:触发器trigger的使用 创建触发器 单一执行语句.多执行语句 new.old详解 查看触发器 删除触发器:慎用触发器,不用就删除 Q:什么是触发器? A: 触发器是与表有关的数据库对象, ...

  9. pl/sql学习(5): 触发器trigger/事务和锁

    (一)触发器简单介绍 触发器是由数据库的特定时间来触发的, 特定事件主要包括以下几种类型: (1)DML: insert, update,delete 增删改 (2)DDL: create, alte ...

随机推荐

  1. HTML5之contenteditable可编辑属性

    运用contenteditable实现输入框高度自动增加,输入框标题name相对高度自动居中,代码如下: <!DOCTYPE html> <html> <head> ...

  2. go与python的不同

    go 开发中需要注意的与python的不同点 列出golang开发过程中与python的不同点,主要是在语法方面,golang的一些语法真是要人命啊. 1.golang可读性很强,与或对应&& ...

  3. 阿里云部署自己的web服务器

    阿里云部署自己的web服务器 [外链图片转存失败(img-GIKNTPPx-1564287221547)(https://upload-images.jianshu.io/upload_images/ ...

  4. element ui 里面的table怎么弹出一个框让表中数据点击出现弹框

    <el-table-column label="团队" prop="name" min-width="120px" align=&qu ...

  5. Hadoop(五)—— HDFS NameNode、DataNode工作机制

    一.NN与2NN工作机制 NameNode(NN) 1.当HDFS启动时,会加载日志(edits)和镜像文件(fsImage)到内存中. 2-4.当元数据的增删改查请求进来时,NameNode会先将操 ...

  6. docker_概念

    为什么有docker? 1. 宿主机可以虚拟一个硬件平台:其上会有内核(在虚拟机上的操作系统),内核负责资源调度和通信:内核之上会有用户态,运行在用户态(用户空间)之上多是应用程序,也就是进程.硬件( ...

  7. 第07组 Alpha冲刺(4/6)

    队名:摇光 队长:杨明哲 组长博客:求戳 作业博客:求再戳 队长:杨明哲 过去两天完成了哪些任务 文字/口头描述:摇光测评的相关功能. 展示GitHub当日代码/文档签入记录:(组内共用,已询问过助教 ...

  8. 检测是否安装或者开启flash

    function flashChecker() { var hasFlash = 0; //是否安装了flash var flashVersion = 0; //flash版本 if(document ...

  9. Hbuilder提交项目到GitHub出现cannot open git-upload-pack

    问题描述 Hbuilder上传本地项目到GitHub时是通过下载的Egit插件,然而提交代码时出现下图问题 网上有说添加http的sslVerify=false,然并卵. 解决方案 不用hbuilde ...

  10. Nginx配置SSL证书部署HTTPS网站(颁发证书)

    一.Http与Https的区别HTTP:是互联网上应用最为广泛的一种网络协议,是一个客户端和服务器端请求和应答的标准(TCP),用于从WWW服务器传输超文本到本地浏览器的传输协议,它可以使浏览器更加高 ...