要点
- pom.xml引入Quartz依赖
- 配置Quartz Configuration
- application.properties配置Quartz
- 下载SQL脚步,导入到数据库
- Job中实现业务
1. pom.xml引入Quartz依赖
org.springframework.boot spring-boot-starter-quartz
2. 配置Quartz Configuration
package com.ly.job;@Configurationpublic class QuartzConfig { //cron表达式,如cron=0 0/5 * * * ? @Value("${push.info.cron}") private String pushInfoCron; //设置Job @Bean public JobDetail pushOrderInfoJobDetail() { return JobBuilder.newJob(PushOrderInfoJob.class).withIdentity("pushOrderInfoJob").storeDurably().build(); } //设置Trigger @Bean public Trigger pushOrderInfoJobTrigger() { CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(pushInfoCron); return TriggerBuilder.newTrigger().forJob(pushOrderInfoJobDetail()) .withIdentity("pushOrderInfoJob") .withSchedule(scheduleBuilder) .build(); }}
3. application.properties配置Quartz
spring.quartz.properties.org.quartz.scheduler.instanceName=clusteredSchedulerspring.quartz.properties.org.quartz.scheduler.instanceId=AUTOspring.quartz.properties.org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTXspring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegatespring.quartz.properties.org.quartz.jobStore.tablePrefix=QRTZ_spring.quartz.properties.org.quartz.jobStore.isClustered=truespring.quartz.properties.org.quartz.jobStore.clusterCheckinInterval=10000spring.quartz.properties.org.quartz.jobStore.useProperties=falsespring.quartz.properties.org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPoolspring.quartz.properties.org.quartz.threadPool.threadCount=10spring.quartz.properties.org.quartz.threadPool.threadPriority=5spring.quartz.properties.org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread=truespring.quartz.job-store-type=jdbc4. 下载SQL脚步,导入到数据库
去官网下载:http://www.quartz-scheduler.org/downloads/
根据版本不同,脚步一般在这2个地方
/docs/dbTablesorg/quartz/impl/jdbcjobstore
5. Job中实现业务
@Configuration@DisallowConcurrentExecutionpublic class PushOrderInfoJob extends QuartzJobBean { private static final Logger logger = LoggerFactory.getLogger(PushOrderInfoJob.class); /** * 这儿写业务 **/ @Override protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { logger.info("-----[PushOrderInfoJob]begin----"); logger.info("-----[PushOrderInfoJob]end----"); }}