第一步:添加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. box-sizing与calc()与flex

    1,Syntax: /* Keyword values */ box-sizing: content-box; box-sizing: border-box; /* Global values */ ...

  2. 【BZOJ2090/2089】[Poi2010]Monotonicity 2 动态规划+线段树

    [BZOJ2090/2089][Poi2010]Monotonicity Description 给出N个正整数a[1..N],再给出K个关系符号(>.<或=)s[1..k].选出一个长度 ...

  3. angualar入门学习-- 自定义指令 认识属性

    个AngularJS指令在HTML代码中可以有四种表现形式: 1.作为一个新的HTML元素来使用 2.作为一个元素的属性来使用 3.作为一个元素的类来使用 4.作为注释来使用 一.创建指令 angul ...

  4. Alcor(安国)AU6387量产修复(u盘修复)

    2010年买的U盘,自从去年坏掉一直没有用. 今天试着把它修理的心态,看看能修好不能.不料真的被我搞好了. 下面是教程链接 如果你的芯片跟我的一样,我人品保证你可以成功. 如果你看教程之后量产 成功, ...

  5. 手动爬虫之京东笔记本栏(ptyhon3)

    import urllib.request as ur import urllib.error as ue import re # 目标网址 url = 'https://list.jd.com/li ...

  6. maven的核心概念

    1 简单的核心概念 1.1 坐标 groupId.artifactId.version,很简单,这三个坐标定位到了该依赖的位置,有了它们就可以下载该依赖了. 1.2 依赖 如果一个jar包使用了另外一 ...

  7. 程序运行时 0xC0000005: 读取位置 0x00000000 时发生访问冲突 ,可能是 com 组件引入各种问题

    在使用com组件事,可能引入很多不是问题的问题,比如CString 定义出运行时出错等等,这些问题解决的办法就是初始化组件 然后释放组件, 在使用组件时,如果仅仅用在按钮事件或者别的mfc 对话框类里 ...

  8. java基础10 吃货联盟点餐系统

    public class OrderMsg { public static void main(String[] args) throws Exception { /** * 订餐人姓名.选择菜品.送 ...

  9. 踩坑之jinja2注释问题(Flask中)

    报错信息  jinja2.exceptions.TemplateSyntaxError  jinja2.exceptions.TemplateSyntaxError: Expected an expr ...

  10. WTForms In Flask(WTForms在Flask中的应用)

    WTForms WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 安装wtforms : pip3/pip install wtforms 用户登录/注册示例 项 ...