![](https://img2018.cnblogs.com/i-beta/1833901/201912/1833901-20191220151243097-1938984799.png)
(2) Once you have the task, but also a possible trigger to perform tasks trigger, trigger Traigger most basic function is to specify the job execution time, the execution interval, run times, etc.
Two, Quartz Demo build
Let's take advantage of Quartz build a basic Demo.
1, introducing dependent jar package:
<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.0</version> </dependency>
2. Configure the information
# 名为:quartz.properties,放置在classpath下,如果没有此配置则按默认配置启动 # 指定调度器名称,非实现类 org.quartz.scheduler.instanceName = DefaultQuartzScheduler04 # 指定线程池实现类 org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool # 线程池线程数量 org.quartz.threadPool.threadCount = 10 # 优先级,默认5 org.quartz.threadPool.threadPriority = 5 # 非持久化job org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
3、新建一个能够打印任意内容的Job:
public class PrintWordsJob implements Job{ @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { String printTime = new SimpleDateFormat("yy-MM-dd HH-mm-ss").format(new Date()); System.out.println("PrintWordsJob start at:" + printTime + ", prints: Hello Job-" + new Random().nextInt(100)); } }
4、创建Schedule,执行任务:
public class MyScheduler { public static void main(String[] args) throws SchedulerException, InterruptedException { // 1、创建调度器Scheduler SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); // 2、创建JobDetail实例,并与PrintWordsJob类绑定(Job执行内容) JobDetail jobDetail = JobBuilder.newJob(PrintWordsJob.class) .withIdentity("job1", "group1").build(); // 3、构建Trigger实例,每隔1s执行一次 Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1") .startNow()//立即生效 .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInSeconds(1)//每隔1s执行一次 .repeatForever()).build();//一直执行 //4、执行 scheduler.scheduleJob(jobDetail, trigger); System.out.println("--------scheduler start ! ------------"); scheduler.start(); //睡眠 TimeUnit.MINUTES.sleep(1); scheduler.shutdown(); System.out.println("--------scheduler shutdown ! ------------"); } }
运行程序,可以看到程序每隔1s会打印出内容,且在一分钟后结束:
三、Quartz核心详解
下面就程序中出现的几个参数,看一下Quartz框架中的几个重要参数:
- Job和JobDetail
- JobExecutionContext
- JobDataMap
- Trigger、SimpleTrigger、CronTrigger
2.JobExecutionContext
JobExecutionContext中包含了Quartz运行时的环境以及Job本身的详细数据信息。
当Schedule调度执行一个Job的时候,就会将JobExecutionContext传递给该Job的execute()中,Job就可以通过JobExecutionContext对象获取信息。
3.JobDataMap
JobDataMap实现了JDK的Map接口,可以以Key-Value的形式存储数据。
JobDetail、Trigger都可以使用JobDataMap来设置一些参数或信息,
Job执行execute()方法的时候,JobExecutionContext可以获取到JobExecutionContext中的信息:
4.Trigger、SimpleTrigger、CronTrigger
[秒] [分] [小时] [日] [月] [周] [年]
下面的代码就实现了每周一到周五上午15:00执行定时任务
public class MyScheduler2 { public static void main(String[] args) throws SchedulerException { //创建调度器Scheduler SchedulerFactory schedulerFactory=new StdSchedulerFactory(); Scheduler scheduler=schedulerFactory.getScheduler(); //创建jobDetail实例,并与printWordsJob类绑定(job执行内容) JobDetail jobDetail= JobBuilder.newJob(PrintWordsJob.class).usingJobData("jobDetail1","这个job用来测试的") .withIdentity("job1","group1").build(); //构建Trigger实例,每隔一秒执行一次 //实现程序运行5s后开始执行Job,执行Job 5s后结束执行: Date startDate=new Date(); startDate.setTime(startDate.getTime()+5000); Date endDate=new Date(); endDate.setTime(startDate.getTime()+5000); CronTrigger cronTrigger=TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1") .usingJobData("trigger1", "这是jobDetail1的trigger") .startNow() .startAt(startDate) .endAt(endDate) .withSchedule(CronScheduleBuilder.cronSchedule("* 00 15 ? * 1/5 2019")).build(); scheduler.scheduleJob(jobDetail,cronTrigger); System.out.println("--------scheduler start ! ------------"); scheduler.start(); System.out.println("--------scheduler shutdown ! ------------"); } }
原文链接:https://blog.csdn.net/noaman_wgs/article/details/80984873