博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Quartz -第一篇-入门
阅读量:2386 次
发布时间:2019-05-10

本文共 6930 字,大约阅读时间需要 23 分钟。

学习地址:
官网:
特点:分布式+集群
设计模式:
  工厂模式
  builder模式
  组件模式
  链式写法
核心概念:调度器,任务,触发器
 
使用:
  依赖:
  
org.quartz-scheduler
quartz
2.2.3

创建一个实例实现Job接口,实现其中的execute方法,将你要做的任务放到此方法

package quartz.job;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import javax.xml.crypto.Data;import java.text.SimpleDateFormat;import java.util.Date;/** * ClassName: HelloJob
* Description:
* date: 2019/1/22 11:19 AM
* * @author chengluchao * @since JDK 1.8 */public class HelloJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("Current Exec Time Is:" + simpleDateFormat.format(date)); System.out.println("Hello word!"); }}

编写调用类

package quartz.job;import org.quartz.JobBuilder;import org.quartz.JobDetail;import org.quartz.Scheduler;import org.quartz.SchedulerFactory;import org.quartz.SimpleScheduleBuilder;import org.quartz.Trigger;import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;/** * ClassName: HelloScheduler
* Description:
* date: 2019/1/22 11:23 AM
* * @author chengluchao * @since JDK 1.8 */public class HelloScheduler { public static void main(String[] args) throws Exception { //创建一个JobDetail实例,将HelloJob.class绑定 JobDetail jobDetail = JobBuilder .newJob(HelloJob.class) .withIdentity("myJob", "group1") .build(); //创建一个Trigger实例,定义该job立即执行,并且每隔两秒钟重复执行一次,直到永远 Trigger trigger = TriggerBuilder .newTrigger() .withIdentity("myTrigger", "group1") .startNow()//立即执行 .withSchedule( SimpleScheduleBuilder .simpleSchedule() .withIntervalInSeconds(2)//两秒一次 .repeatForever())//直到永远 .build(); //创建Schduler实例 SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); scheduler.start(); scheduler.scheduleJob(jobDetail, trigger); }}

14:56:17.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.myJob', class="quartz".job.HelloJob

14:56:17.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
14:56:17.296 [DefaultQuartzScheduler_Worker-10] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.myJob
Current Exec Time Is:2019-01-22 14:56:17
Hello word!
14:56:19.295 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.myJob', class="quartz".job.HelloJob
14:56:19.295 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
14:56:19.295 [DefaultQuartzScheduler_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.myJob
Current Exec Time Is:2019-01-22 14:56:19
Hello word!
14:56:21.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.simpl.PropertySettingJobFactory - Producing instance of Job 'group1.myJob', class="quartz".job.HelloJob
14:56:21.296 [DefaultQuartzScheduler_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
14:56:21.296 [DefaultQuartzScheduler_Worker-2] DEBUG org.quartz.core.JobRunShell - Calling execute on job group1.myJob
Current Exec Time Is:2019-01-22 14:56:21
Hello word!

补充:

 

jobDetail的重要属性:
  name
  group 默认"DEFAULT"
  jobClass
  jobDataMap:
      在任务调度的时候,jobDataMap可以进行存储一些信息;
 
打印jobDetail的信息
System.out.println("jobDetail's name : " + jobDetail.getKey().getName());        System.out.println("jobDetail's gruop : " + jobDetail.getKey().getGroup());        System.out.println("jobDetail's jobClass : " + jobDetail.getKey().getClass());

jobDataMap的使用:

public class HelloScheduler {    public static void main(String[] args) throws Exception {        //创建一个JobDetail实例,将HelloJob.class绑定        JobDetail jobDetail = JobBuilder                .newJob(HelloJob.class)                .withIdentity("myJob")                .usingJobData("message","hello myJob1")                .usingJobData("floatJobvalue",3.14f)                .build();        //创建一个Trigger实例,定义该job立即执行,并且每隔两秒钟重复执行一次,直到永远        Trigger trigger = TriggerBuilder                .newTrigger()                .withIdentity("myTrigger", "group1")                .usingJobData("message","hello myTrigger1")                .usingJobData("doubleJobvalue",885.02)                .startNow()//立即执行                .withSchedule(                        SimpleScheduleBuilder                                .simpleSchedule()                                .withIntervalInSeconds(2)//两秒一次                                .repeatForever())//直到永远                .build();        //创建Schduler实例        SchedulerFactory schedulerFactory = new StdSchedulerFactory();        Scheduler scheduler = schedulerFactory.getScheduler();        scheduler.start();        scheduler.scheduleJob(jobDetail, trigger);    }}
public class HelloJob implements Job {    @Override    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {        Date date = new Date();        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        System.out.println("Current Exec Time Is:" + simpleDateFormat.format(date));        System.out.println("Hello word!");        JobKey key = jobExecutionContext.getJobDetail().getKey();        System.out.println("JobDetailKey : " + key.getName() + "JobDetailValue : " + key.getGroup());        TriggerKey triggerKey1 = jobExecutionContext.getTrigger().getKey();        System.out.println("triggerKey1 : " + triggerKey1.getName() + "triggervalue1 : " + triggerKey1.getGroup());        JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();        JobDataMap tdataMap = jobExecutionContext.getTrigger().getJobDataMap();        System.out.println(dataMap.get("message"));        System.out.println(tdataMap.get("message"));        JobDataMap dataMap2 = jobExecutionContext.getMergedJobDataMap();        //Trigger会覆盖JobDetail的信息        System.out.println(dataMap2.get("message"));        System.out.println(dataMap2.get("message"));    }

获取jobDataMap的值的方式2:

 在HelloJob类中定义属性,属性名和map中的key一样,加上get/set方法

在HelloJob的类中就可以直接使用

 

Trigger的方法:
.startAt(date)//首次执行的时间 .endAt(endDate)//最后一次执行的时间 .startNow()//立即执行

 

 

 

 

 

posted @
2019-01-22 15:06 阅读(
...) 评论(
...)

转载地址:http://xujab.baihongyu.com/

你可能感兴趣的文章
docker 图形化管理工具Kitematics
查看>>
unittest单元测试框架总结
查看>>
command 'x86_64-linux-gnu-gcc' failed with exit status 1
查看>>
浅谈前端SPA(单页面应用)
查看>>
PostgreSQL 服务启动后停止
查看>>
SSH V2的中间人攻击
查看>>
Socks代理反弹突破内网(freebuf)
查看>>
|=运算符
查看>>
Google网络+容器相关参考资料
查看>>
Tensorflow Privacy
查看>>
又一款linux提权辅助工具 – Linux_Exploit_Suggester
查看>>
linux 驱动开发视频
查看>>
The magic of LD_PRELOAD for Userland Rootkits
查看>>
__attribute__机制介绍
查看>>
十本 Linux 核心開發書籍介紹
查看>>
file_operations结构体
查看>>
try_module_get和module_put
查看>>
使用 /proc 文件系统来访问 Linux 内核的内容
查看>>
c#明年学习计划书
查看>>
linux驱动程序开发实例
查看>>