第一步:添加jar,maven配置


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

第二步:job代码


CycleJob

@DisallowConcurrentExecution
public class CycleJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap data = context.getJobDetail().getJobDataMap();
System.out.println("这是一个周期性执行的job。参数:" + data.getString("key"));
}
}

StartFixedTimeJob

public class StartFixedTimeJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap data = context.getJobDetail().getJobDataMap();
System.out.println("这是定时执行的job。参数:" + data.getString("key"));
}
}

StartNowJob

public class StartNowJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException { JobDataMap data = context.getJobDetail().getJobDataMap();
System.out.println("这是一个立刻执行的job。参数:" + data.getString("key"));
}
}

SchedulerManager

public class SchedulerManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulerManager.class);

    private static SchedulerFactory sf = new StdSchedulerFactory();
private static Scheduler scheduler;
static{
try{
LOGGER.info("------------------------- SchedulerManager.start----------------");
// 通过SchedulerFactory来获取一个调度器scheduler
scheduler = sf.getScheduler();
scheduler.start();
}
catch(SchedulerException e){
LOGGER.error("SchedulerManager.start.error", e);
}
} public static Scheduler getScheduler() {
return scheduler;
} public static String getSchedulerName() throws SchedulerException {
return scheduler.getSchedulerName();
} /**
* 创建周期执行的job
*
* @param jobId
* @throws SchedulerException
*/
public static void createCycleJob(Integer jobId) throws Exception {
JobKey jobKey = JobKey.jobKey("cycleJob_" + jobId, getSchedulerName());
if(scheduler.checkExists(jobKey)){
throw new IllegalStateException("[周期任务JobKey: " + jobKey + "] 已经存在");
} // 作业
JobDetail job = newJob(CycleJob.class).withIdentity(jobKey).requestRecovery(true).build(); // 设置job参数
job.getJobDataMap().put("key", "jobId=" + jobId); // 计划表达式 cron 一秒执行一次
String corn = "* */1 * * * ?"; // 触发器
CronTrigger trigger = newTrigger().withIdentity("cycleTrigger_" + jobId, getSchedulerName())
.withSchedule(cronSchedule(corn)).build(); // 作业和触发器设置到调度器中
SchedulerManager.getScheduler().scheduleJob(job, trigger);
} /**
* 创建立刻执行的job
*
* @param jobId
* @throws SchedulerException
*/
public static void createStartNowJob(Integer jobId) throws Exception {
JobKey jobKey = JobKey.jobKey("startNowJob_" + jobId, getSchedulerName());
if(scheduler.checkExists(jobKey)){
throw new IllegalStateException("[立刻执行的JobKey: " + jobKey + "] 已经存在");
} JobDetail job = newJob(StartNowJob.class).withIdentity(jobKey).requestRecovery(true).build();
job.getJobDataMap().put("key", "jobId=" + jobId); Trigger trigger = newTrigger().withIdentity("startNowTrigger" + jobId, getSchedulerName()).startNow().build(); SchedulerManager.getScheduler().scheduleJob(job, trigger);
} /**
* 创建定时执行的job
*
* @param jobId
* @throws SchedulerException
*/
public static void createStartFixedTimeJob(Integer jobId) throws Exception {
JobKey jobKey = JobKey.jobKey("startFixedTimeJob_" + jobId, getSchedulerName());
if(scheduler.checkExists(jobKey)){
throw new IllegalStateException("[定时执行的JobKey: " + jobKey + "] 已经存在");
} JobDetail job = newJob(StartFixedTimeJob.class).withIdentity(jobKey).requestRecovery(true).build();
job.getJobDataMap().put("key", "jobId=" + jobId); // 定时执行。2017-04-25 09:58:00分执行
Date date = DateUtils.parseDate("2017-04-25 09:59:00", new String[]{"yyyy-MM-dd hh:mm:ss"});
Trigger trigger = newTrigger().withIdentity("srartNowTrigger" + jobId, getSchedulerName()).startAt(date)
.build();
SchedulerManager.getScheduler().scheduleJob(job, trigger);
} /**
* 删除job
*
* @param jobId
* @throws SchedulerException
*/
public static void stopCycleTaskJob(Integer jobId) throws SchedulerException {
JobKey jobKey = JobKey.jobKey("cycleJob_" + jobId, getSchedulerName());
if(scheduler.checkExists(jobKey)){
scheduler.deleteJob(jobKey);
LOGGER.info("------SchedulerManager.stopCycleTaskJob: delete the job jobKey [" + jobKey + "]");
}
}
}

第三步:测试类


QuartzTest

public class QuartzTest {
public static void main(String[] args) throws Exception {
// 测试周期性job执行
// SchedulerManager.createCycleJob(1); // 测试立刻执行的job
// SchedulerManager.createStartNowJob(2); // 测试定时执行的job
SchedulerManager.createStartFixedTimeJob(3);
}
}

测试周期性job

测试立刻执行的job

测试定时执行的job

quartz(2) -- 入门案例的更多相关文章

  1. C# -- Quartz.Net入门案例

    1. 入门案例 using Quartz;using Quartz.Impl; public class PrintTime : IJob { public Task Execute(IJobExec ...

  2. Quartz应用实践入门案例二(基于java工程)

    在web应用程序中添加定时任务,Quartz的简单介绍可以参看博文<Quartz应用实践入门案例一(基于Web应用)> .其实一旦学会了如何应用开源框架就应该很容易将这中框架应用与自己的任 ...

  3. Quartz应用实践入门案例一(基于Web环境)

    Quartz是一个完全由java编写的开源作业调度框架,正是因为这个框架整合了许多额外的功能,所以在使用上就显得相当容易.只是需要简单的配置一下就能轻松的使用任务调度了.在Quartz中,真正执行的j ...

  4. JAVAEE——BOS物流项目13:Quartz入门案例、核心概念、cron 表达式的格式(了解)

    1.quartz入门案例 本入门案例基于spring和quartz整合完成. 第一步:创建maven工程,导入spring和quartz相关依赖 第二步:创建任务类 第三步:在spring配置文件中配 ...

  5. Spring学习笔记(一)—— Spring介绍及入门案例

    一.Spring概述 1.1 Spring是什么 Spring是一个开源框架,是于2003年兴起的一个轻量级的Java开发框架, 由Rod Johnson 在其著作<Expert one on ...

  6. SpringMVC入门案例及请求流程图(关于处理器或视图解析器或处理器映射器等的初步配置)

    SpringMVC简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 Spring结构图 Spr ...

  7. SpringMvc核心流程以及入门案例的搭建

    1.什么是SpringMvc Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 M ...

  8. Struts2第一个入门案例

      一.如何获取Struts2,以及Struts2资源包的目录结构的了解    Struts的官方地址为http://struts.apache.org 在他的主页当中,我们可以通过左侧的Apache ...

  9. MyBatis入门案例、增删改查

    一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...

随机推荐

  1. centos7的nfs配置

    author : headsen chen date : 2018-04-12  09:40:14  一,服务端安装和配置: 环境准备: systemctl stop firewalld system ...

  2. linux的%用法

    转自:http://blog.csdn.net/wu020708/article/details/52387473 linux (%和%%)(#和##)贪婪匹配规则 先看一个案例,提取文件名: fil ...

  3. vmware key

    VMware vRealize Suite 2017 Enterprise   N04CL-09H9H-J89DJ-0KCH6-90N0J VMware vRealize Operations Man ...

  4. python if x:

    # !usr/bin/env python # -*- coding:utf-8 _*- """ @author:happy_code @email: happy_cod ...

  5. bootstrap-table固定表头固定列

    1.引入 bootstrap依赖于jquery bootstrap-table依赖于bootstrap,所以都需要引入 2. bootstrap-table有两种方式,html.js <tabl ...

  6. cpp中文乱码

    中文乱码 [root@test mediaStudio]# g++ testCgi.cpp [root@test mediaStudio]# ./a.out Content-type:text/htm ...

  7. <2014 05 16> 线性表、栈与队列——一个环形队列的C语言实现

    栈与队列都是具有特殊存取方式的线性表,栈属于先进后出(FILO),而队列则是先进先出(FIFO).栈能够将递归问题转化为非递归问题,这是它的一个重要特性.除了FILO.FIFO这样的最普遍存取方式外, ...

  8. logging/re - 总结

    logging 模块 很多程序都有记录日志的需求 logging的日志可以分为 debug(), info(), warning(), error() and critical()5个级别 1.输出到 ...

  9. DKLang Translation Editor

    https://yktoo.com/en/software/dklang-traned Features Translation using a dictionary (so-called Trans ...

  10. sp_who 查看数据库连接数

    create table #TempTable(spid int,ecid int,statusvarchar(32),loginname varchar(32),hostname varchar(3 ...