Quartz常规操作
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11899532.html
Project Directory
Maven Dependency
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.fool.quartz</groupId>
<artifactId>helloquartz</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.2</version>
</dependency> <dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.3.2</version>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency> <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.29</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
log4j.properties
log4j.rootLogger=info,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n
无状态Job
HelloJob.java
package org.fool.quartz.job; import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import java.util.Date; @Slf4j
public class HelloJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info("job name: {}", jobExecutionContext.getJobDetail().getKey().getName());
log.info("job group: {}", jobExecutionContext.getJobDetail().getKey().getGroup());
log.info("job class detail name: {}", jobExecutionContext.getJobInstance().getClass().getName());
log.info("job class simple name: {}", jobExecutionContext.getJobInstance().getClass().getSimpleName()); log.info("trigger name: {}", jobExecutionContext.getTrigger().getKey().getName());
log.info("trigger group: {}", jobExecutionContext.getTrigger().getKey().getGroup()); String jobDetailMessage = jobExecutionContext.getJobDetail().getJobDataMap().getString("message");
String triggerMessage = jobExecutionContext.getTrigger().getJobDataMap().getString("message"); log.info("current job detail message: {}", jobDetailMessage);
log.info("current trigger message: {}", triggerMessage); log.info("current datetime: {}", new Date());
log.info("current job fire time: {}", jobExecutionContext.getFireTime());
log.info("next job fire time: {}", jobExecutionContext.getNextFireTime());
}
}
QuartzJobTest.java
package org.fool.quartz.job; import lombok.extern.slf4j.Slf4j;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory; @Slf4j
public class QuartzJobTest {
public static void main(String[] args) throws Exception {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail jobDetail = JobBuilder.newJob(HelloJob.class)
.withIdentity("job1", "job group1")
.usingJobData("message", "HelloMyJob")
.build(); Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "trigger group1")
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5))
.usingJobData("message", "HelloMyTrigger")
.startNow()
.build(); scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
}
}
有状态Job
HelloPersistJob.java
package org.fool.quartz.job; import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution; @PersistJobDataAfterExecution
@Slf4j
public class HelloPersistJob implements Job { private String message;
private Integer count; public void setMessage(String message) {
this.message = message;
} public void setCount(Integer count) {
this.count = count;
} @Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
// if the key name is the same between trigger and jobDetail, the trigger name will override the jobDetail name
// if you want to fetch both trigger name and jobDetail name, please define different jobData key name
log.info("current name: {}", message); count++;
jobExecutionContext.getJobDetail().getJobDataMap().put("count", count); log.info("job execution count: {}", count);
}
}
Note: 有状态job需要在job类头上加上@PersistJobDataAfterExecution注解
QuartzPersistJobTest.java
package org.fool.quartz.job; import lombok.extern.slf4j.Slf4j;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory; @Slf4j
public class QuartzPersistJobTest {
public static void main(String[] args) throws Exception {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail jobDetail = JobBuilder.newJob(HelloPersistJob.class)
.withIdentity("job1", "job group1")
.usingJobData("message", "HelloMyJob")
.usingJobData("count", 0)
.build(); Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "trigger group1")
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(5))
.usingJobData("message", "HelloMyTrigger")
.startNow()
.build(); scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
}
}
SimpleSchedule
SimpleJob.java
package org.fool.quartz.job; import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import java.util.Date; @Slf4j
public class SimpleJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info("current datetime: {}", new Date());
}
}
QuartzSimpleJobTest.java
package org.fool.quartz.job; import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory; import java.util.Date; public class QuartzSimpleJobTest {
public static void main(String[] args) throws Exception {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail jobDetail = JobBuilder.newJob(SimpleJob.class)
.withIdentity("job1", "job group1")
.build(); Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "trigger group1")
.withSchedule(SimpleScheduleBuilder.repeatSecondlyForTotalCount(3, 8))
.startAt(new Date(System.currentTimeMillis() + 5000))
.endAt(new Date(System.currentTimeMillis() + 10000))
.build(); scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
}
}
CronSchedule
CronJob.java
package org.fool.quartz.job; import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException; import java.util.Date; @Slf4j
public class CronJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
log.info("current datetime: {}", new Date());
}
}
QuartzCronJobTest.java
package org.fool.quartz.job; import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory; public class QuartzCronJobTest {
public static void main(String[] args) throws Exception {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); JobDetail jobDetail = JobBuilder.newJob(CronJob.class)
.withIdentity("job1", "job group1")
.build(); Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "trigger group1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))
.startNow()
.build(); scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
}
}
Reference
http://www.quartz-scheduler.org/
Quartz常规操作的更多相关文章
- FTP服务器常规操作
导读 FTP协议是Internet文件传输的基础,它是由一系列规格说明文档组成,目标是提高文件的共享性,提供非直接使用远程计算机,使存储介质对用户透明和可靠高效地传送数据.下面就由我给大家简单介绍一下 ...
- Apache服务器常规操作
导读 Apache是世界上排名第一的Web服务器,50%以上的Web服务器都在使用Apache,它几乎可以在所有计算机平台上运行.下面就由我给大家说说Apache服务器的一些常规操作. Apache服 ...
- mysql服务器的常规操作
mysql服务器的常规操作 导读 MySQL是一个小型关系数据库管理系统,目前被广泛的应用在Internet上的中小型网站中,体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,使得许多中小型网站 ...
- 征服 Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)
有日子没写博客了,真的是忙得要疯掉. 完成项目基础架构搭建工作,解决了核心技术问题,接着需要快速的调研下基于Spring框架下的Redis操作. 相关链接: 征服 Redis 征服 Redis + J ...
- Mongodb常规操作【一】
Mongodb是一种比较常见的NOSQL数据库,数据库排名第四,今天介绍一下Net Core 下,常规操作. 首先下C# 版的驱动程序 "MongoDB.Driver",相关依赖包 ...
- C#通过Ado.net对连接数据库并进行添加删除等常规操作的代码
如下资料是关于C#通过Ado.net对连接数据库并进行添加删除等常规操作的内容. static string sqlcon = "server=.;database=;Integrated ...
- react-native 常规操作
1. 关闭xcode打开模拟器的快捷键 , 等常规操作 https://www.jianshu.com/p/f6723f3406b7
- Linux 下的 netfilter 认识与常规操作
Linux 下的 netfilter 认识与常规操作 前言 博客写到今天,1年7个月.可是包含所有写作经历,这个时间线可以达到三年. 上次更新了一篇 "镇站之宝" ,也是本站阅读量 ...
- 数据库mysql的常规操作
1. 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. 简单来说是本身可视为电子化的文件柜——存储电子文件的处所,用户可以对文件中的数据进 ...
随机推荐
- 阶段3 1.Mybatis_04.自定义Mybatis框架基于注解开发_2 回顾自定义mybatis的流程分析
- 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_05.mybatis环境搭建-前期准备
视频中右侧没有勾选 直接finish 用下面的sql里面的一些表来实现今天的功能 只需要用到里面的user表. 这是之前已经建好的数据库 把表都删除掉,用sql语句去创建表和表内的记录,最终的结果: ...
- 定制属于你自己的ViewEngine(一套逻辑多套UI)
ASP.NET MVC出来这么久了,心中却又很多的疑惑:为什么所有的View都要放在Views目录下? 为什么Shared文件夹下面的页面可以被共享? 为什么Page既可以是*.cshtml,也可以是 ...
- 如何保存不连着电脑跑monkey?如何跑多个APK的monkey?
哈喽,大家有没有遇到用真机跑monkey的时候,跑到一半结果小手一抖不小心把线碰掉了,结果报告就没获取到啦~ 今天就来解决这个问题 1.如何保存不连着电脑跑monkey? 1).输入命令进入shell ...
- 【MM系列】SAP MB5B中FI凭证摘要是激活的/结果可能不正确 的错误
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP MB5B中FI凭证摘要是激活 ...
- python每日一练:0014题
第 0014 题: 纯文本文件 student.txt为学生信息, 里面的内容(包括花括号)如下所示: { "1":["张三",150,120,100], &q ...
- 深入理解java:1.3.1 JVM内存区域的划分(运行时数据区)
学习Java GC机制,可以帮助我们在日常工作中 排查各种内存溢出或泄露问题,解决性能瓶颈,达到更高的并发量,写出更高效的程序. 我们将从4个方面学习Java GC机制, 1,内存是如何分配的: 2, ...
- MVC中的cshtml与ASPX的区别
在MVC3中,即可以使用cshtml,也可以使用aspx, 这两者到底有什么区别呢? 越详细越好,如果是用来正式开发,用哪种比较好. --------------------------------- ...
- docker--docker 网络管理
9 docker 网络管理 9.1 默认网络 1.查看docker网络: docker network ls Docker中默认的三种网络分别为bridge.host和none,其中名为bridge的 ...
- [Codeforces 1239D]Catowise City(2-SAT)
[Codeforces 1239D]Catowise City(2-SAT) 题面 有n个主人,每个主人都有一只猫.每个主人认识一些猫(包括自己的猫).现在要选出一些人和一些猫,个数均大于0且总共为n ...