本文介绍下Quartz框架的使用
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。Quartz的最新版本为Quartz 2.3.0。
Quartz的使用
一、Quartz 的使用思路
二、Quartz 的基本使用方式
1.创建项目添加依赖
创建普通maven项目,添加相关依赖:
<!-- Quartz 坐标 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency>
2.创建job
/** * @program: spring-boot-schedueld-demo * @description: Quartz介绍Job * @author: 波波烤鸭 * @create: 2019-05-21 09:51 */ public class QuartzDemo implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("quartz任务执行了...."+new Date()); } }
3.测试
通过主方法测试。
public static void main(String[] args) throws Exception{ // 1、创建Job对象:你要做什么事? JobDetail job = JobBuilder.newJob(QuartzDemo.class).build(); /** * 2、创建trigger * 简单的 trigger 触发时间:通过 Quartz 提供一个方法来完成简单的重复调用 cron * Trigger:按照 Cron 的表达式来给定触发的时间 */ Trigger trigger = TriggerBuilder.newTrigger() .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) .build(); // 3.创建Scheduled对象,在什么时候做什么事? Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); scheduler.scheduleJob(job,trigger); // 启动 scheduler.start(); }
三、SpringBoot整合Quartz
1.创建项目,添加依赖
创建SpringBoot项目,添加如下依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Quartz 坐标 --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> <exclusions> <exclusion> <artifactId>slf4j-api</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <!-- 添加 Scheduled 坐标 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <!-- Sprng tx 坐标 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> </dependencies>
2.添加Quartz的配置类
/** * @program: spring-boot-schedueld-demo * @description: Quartz的配置类 * @author: 波波烤鸭 * @create: 2019-05-21 10:09 */ @Configuration public class QuartzConfig { /** * 1.创建 Job 对象 */ @Bean public JobDetailFactoryBean jobDetailFactoryBean() { JobDetailFactoryBean factory = new JobDetailFactoryBean(); //关联我们自己的 Job 类 factory.setJobClass(QuartzDemo.class); return factory; } /** * 2.创建 Trigger 对象 * 简单的 Trigger */ @Bean public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) { SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean(); //关联 JobDetail 对象 factory.setJobDetail(jobDetailFactoryBean.getObject()); //该参数表示一个执行的毫秒数 factory.setRepeatInterval(2000); //重复次数 factory.setRepeatCount(5); return factory; } /** * Cron Trigger */ @Bean public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean){ CronTriggerFactoryBean factory = new CronTriggerFactoryBean(); factory.setJobDetail(jobDetailFactoryBean.getObject()); //设置触发时间 factory.setCronExpression("0/2 * * * * ?"); return factory; } /** * 3.创建 Scheduler 对象 */ @Bean public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean) { SchedulerFactoryBean factory = new SchedulerFactoryBean(); //关联 trigger factory.setTriggers(cronTriggerFactoryBean.getObject()); return factory; } }
3.单元测试
@SpringBootApplication @EnableScheduling // 开启Scheduled定时任务 public class SpringBootSchedueldDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringBootSchedueldDemoApplication.class, args); } }
搞定~