第一步:添加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. Hadoop格式化HDFS报错java.net.UnknownHostException: centos64

    异常描述 在对HDFS格式化,执行hadoop namenode -format命令时,出现未知的主机名的问题,异常信息如下所示: [shirdrn@localhost bin]$ hadoop na ...

  2. linux 项目自动部署脚本

    1.使用maven获取源码部署,并可替换配置文件(金融数据分析平台) #!/bin/bash#设置变量cd /home#停止tomcatecho "开始停止tomcat..." p ...

  3. My97DatePicker日历控件在iframe提示没有权限的问

    修改 WdatePicker.js 文件 $crossFrame:false, 值设成 false,原来的值是 true

  4. 【BZOJ3931】[CQOI2015]网络吞吐量 最大流

    [BZOJ3931][CQOI2015]网络吞吐量 Description 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为 ...

  5. iOS section 随tableview一起滚动

    @interface YGSectionHeaderView : UIView @property NSUInteger section; @property (nonatomic, weak) UI ...

  6. Eclipse出现ContextLoaderListener not find

    严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...

  7. SharePoint BI

    本篇博客主要针对SharePoint BI整体结构进行整理,为读者分析几种Sharepoint BI场景 先附一张自己做的结构图:

  8. JDK源码分析之concurrent包(二) -- 线程池ThreadPoolExecutor

    上一篇我们简单描述了Executor框架的结构,本篇正式开始并发包中部分源码的解读. 我们知道,目前主流的商用虚拟机在线程的实现上可能会有所差别.但不管如何实现,在开启和关闭线程时一定会耗费很多CPU ...

  9. Linux中搭建HTTP服务器

    1.配置IP [root@localhost~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static ...

  10. 区块链 block chain 去信任

    去中心化:不以参与交易的任何一方为中心 去信任:假定参与交易的任何一方都是不可信任的 区块链受到关注的原因 去中心化.去信任化.智能合约等,正好满足未来互联网持续发展所要求的信息的盖度自动化和高度程序 ...