quartz(2) -- 入门案例
第一步:添加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) -- 入门案例的更多相关文章
- C# -- Quartz.Net入门案例
1. 入门案例 using Quartz;using Quartz.Impl; public class PrintTime : IJob { public Task Execute(IJobExec ...
- Quartz应用实践入门案例二(基于java工程)
在web应用程序中添加定时任务,Quartz的简单介绍可以参看博文<Quartz应用实践入门案例一(基于Web应用)> .其实一旦学会了如何应用开源框架就应该很容易将这中框架应用与自己的任 ...
- Quartz应用实践入门案例一(基于Web环境)
Quartz是一个完全由java编写的开源作业调度框架,正是因为这个框架整合了许多额外的功能,所以在使用上就显得相当容易.只是需要简单的配置一下就能轻松的使用任务调度了.在Quartz中,真正执行的j ...
- JAVAEE——BOS物流项目13:Quartz入门案例、核心概念、cron 表达式的格式(了解)
1.quartz入门案例 本入门案例基于spring和quartz整合完成. 第一步:创建maven工程,导入spring和quartz相关依赖 第二步:创建任务类 第三步:在spring配置文件中配 ...
- Spring学习笔记(一)—— Spring介绍及入门案例
一.Spring概述 1.1 Spring是什么 Spring是一个开源框架,是于2003年兴起的一个轻量级的Java开发框架, 由Rod Johnson 在其著作<Expert one on ...
- SpringMVC入门案例及请求流程图(关于处理器或视图解析器或处理器映射器等的初步配置)
SpringMVC简介:SpringMVC也叫Spring Web mvc,属于表现层的框架.Spring MVC是Spring框架的一部分,是在Spring3.0后发布的 Spring结构图 Spr ...
- SpringMvc核心流程以及入门案例的搭建
1.什么是SpringMvc Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 M ...
- Struts2第一个入门案例
一.如何获取Struts2,以及Struts2资源包的目录结构的了解 Struts的官方地址为http://struts.apache.org 在他的主页当中,我们可以通过左侧的Apache ...
- MyBatis入门案例、增删改查
一.MyBatis入门案例: ①:引入jar包 ②:创建实体类 Dept,并进行封装 ③ 在Src下创建大配置mybatis-config.xml <?xml version="1.0 ...
随机推荐
- windows下caffe如何单独编译proto文件
利用protoc.exe即可编译. 在protoc.exe当前文件夹下打开cmd,输入命令如下: pushd %~dp0 echo "copying .proto and generated ...
- 数据结构 + 算法 -> 收集
董的博客:数据机构与算法合集 背包问题应用(2011-08-26) 数据结构之红黑树(2011-08-20) 素数判定算法(2011-06-26) 算法之图搜索算法(一)(2011-06-22) 算法 ...
- sql查询用nolock
大家在写查询时,为了性能,往往会在表后面加一个nolock,或者是with(nolock),其目的就是查询是不锁定表,从而达到提高查询速度的目的. 什么是并发访问:同一时间有多个用户访问同一资源,并发 ...
- [HAOI2008]排名系统& [Zjoi2006]GameZ游戏排名系统
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2487 Solved: 711[Submit][Statu ...
- iOS侧面加shadow
UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:_bgView.bounds]; _bgView.layer.masksToBo ...
- [hihoCoder] 后序遍历
The key of this problem is that we need not build the tree from scratch. In fact, we can direct obta ...
- mongoDB之find()
一.find方法 db.collection_name.find();查询 查询所有结果 1) db.users.find();类似于select * from users; 指定返回那些列(键) 2 ...
- Log4j 2
Log4j – Apache Log4j 2 - Apache Log4j 2 http://logging.apache.org/log4j/2.x/ Apache Log4j 2 Apache L ...
- gerrit添加appkey以及简单添加分支
最近团队开放用上gerrit版本项目管理工具,简单说一下appkey配置过程 首先是拿到gerrit分配的账户密码.然后进入到首页,假如是新搭建的应该是没有信息,我这里有一些提交的信息,然后找到右上角 ...
- delphi中String 和 动态静态数组
默认string类型为ansiString:有编译开关控制 shortString: strShort : shortString; strShort 大小256字节,可根据sizeof()计算出,s ...