第一步:添加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. ajax 传递数组类型参数后台接收不到的问题

    在做排序功能的时候需要将一个数组的数据传递到后台,(当时怎么没用json,如果用json就没有那么多的事情了),数据提交采用ajax! 先看代码 js: submitbtn: function () ...

  2. iOS xcode6.0使用7.1运行程序 iphone5上下有黑条

    转自:http://stackoverflow.com/questions/25817562/black-bars-appear-in-app-when-targeting-ios7-1-or-7-0 ...

  3. CodeForces 157A Game Outcome

    A. Game Outcome time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  4. 第00章—IDEA

    spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...

  5. ArcGIS runtime sdk for wpf 授权

    这两天由于runtime sdk for wpf的授权和runtime sdk 其他产品的授权的不一样导致自己混乱不堪. 总结下吧. sdk 简介 当前ArcGIS runtime sdk 包括一系列 ...

  6. 贪玩ML系列之一个BP玩一天

    手写串行BP算法,可调batch_size 既要:1.输入层f(x)=x  隐藏层sigmoid 输出层f(x)=x 2.run函数实现单条数据的一次前馈 3.train函数读入所有数据for循环处理 ...

  7. Windows Server 2016 下执行定时任务(英文系统)

    Step1. 找到“控制面板(Control Panel)” 打开 “管理工具(Administrative Tools)” Step2. 打开“任务计划(Task Schedule)” Step3. ...

  8. Learn How To Cross Over The Wall

    1.一个proxy的实现 http://blog.codingnow.com/2011/05/xtunnel.html 2.SOCK5 RFC http://www.faqs.org/rfcs/rfc ...

  9. android 布局属性详解

    Android功能强大,界面华丽,但是众多的布局属性就害苦了开发者,下面这篇文章结合了网上不少资料. 第一类:属性值为true或falseandroid:layout_centerHrizontal ...

  10. django 中的路由系统(url)

    路由系统 根据Django约定,一个WSGI应用里最核心的部件有两个:路由表和视图.Django框架 的核心功能就是路由:根据HTTP请求中的URL,查找路由表,将HTTP请求分发到 不同的视图去处理 ...