quartz
概述
- 特点
- 用到的设计模式
- Builder 模式
- factory模式
- 组件模式
- 链式写法
- 体系结构
- 架构图
-
spring boot quartz
- pom配置
org.quartz-scheduler quartz 2.2.3 org.springframework spring-context-support org.springframework spring-tx 5.1.3.RELEASE
package com.ltf.shop.quartzdemo;import org.quartz.Job;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;public class HelloJob implements Job { @Override public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { System.out.println("hello word quartz"); }}
package com.ltf.shop.quartzconfig;import com.ltf.shop.quartzdemo.HelloJob;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.quartz.CronTriggerFactoryBean;import org.springframework.scheduling.quartz.JobDetailFactoryBean;import org.springframework.scheduling.quartz.SchedulerFactoryBean;@Configurationpublic class QuartzConfig { /** * 创建Job对象 */ @Bean public JobDetailFactoryBean jobDetailFactoryBean() { JobDetailFactoryBean factory=new JobDetailFactoryBean(); factory.setJobClass(HelloJob.class); return factory; } /** * 创建Job对象 */// @Bean// public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean)// {// SimpleTriggerFactoryBean simpleTriggerFactoryBean=new SimpleTriggerFactoryBean();// //关联JobDetail对象// simpleTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());// //设置执行的毫秒数// simpleTriggerFactoryBean.setRepeatInterval(3000);// simpleTriggerFactoryBean.setRepeatCount(15);// return simpleTriggerFactoryBean;// }// /**// * 创建scheduler对象// */// @Bean// public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean)// {// SchedulerFactoryBean schedulerFactoryBean=new SchedulerFactoryBean();// schedulerFactoryBean.setTriggers(simpleTriggerFactoryBean.getObject());// return schedulerFactoryBean;// } @Bean public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) { CronTriggerFactoryBean cronTriggerFactoryBean=new CronTriggerFactoryBean(); cronTriggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject()); cronTriggerFactoryBean.setCronExpression("0/2 * * * * ?"); return cronTriggerFactoryBean; } @Bean public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean) { SchedulerFactoryBean schedulerFactoryBean=new SchedulerFactoryBean(); schedulerFactoryBean.setTriggers(cronTriggerFactoryBean.getObject()); return schedulerFactoryBean; }}