作者|olive丶
来源|
https://blog.csdn.net/asd1098626303/article/details/80831114
最近在做一个彩票相关的项目,主要涉及到不定时开奖,不定时封盘,原本打算使用springboot 自带的Schedule进行这一系列的工作,由于不能自动的添加定时任务,所以使用quartz,spring boot 2.0集成了quartz,所以决定尝试下quartz用于实现作业调度。
做的时候查看了很多资料,都写的花里胡哨的,要么就是做的东西太完整了,要么就是完全不能理解,要么就是很早以前的做法了,让人很头晕,所以说做个很简单明了的教程,说一下如何使用
https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#boot-features-quartz
build.gradle:
uildscript { ext { springBootVersion = '2.0.3.RELEASE' } repositories { maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'} } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group = 'com.zhu' version = '0.0.1-SNAPSHOT' sourceCompatibility = 1.8 repositories { maven{ url 'http://maven.aliyun.com/nexus/content/groups/public'} } dependencies { compile('org.springframework.boot:spring-boot-starter-web') compile('org.springframework.boot:spring-boot-starter-quartz') testCompile('org.springframework.boot:spring-boot-starter-test') } |
build.gradle中引入spring-boot-starter-quartz
之后如果没什么特殊需求的话,根本不需要任何的花里胡哨的配置,直接编码。
创建 ScheduleService.java,用于创建定时任务,这里动态的创建了两个定时任务,每隔1秒和每隔2秒运行。创建时需要新建JobDetail(任务)以及CronTrigger(定时任务触发器) ,并且scheduler.scheduleJob(jobDetail,cronTrigger);把任务添加到任务队列中。
package com.zhu.zqjc.schedule; import com.zhu.zqjc.bean.enums.FootballStatus; import com.zhu.zqjc.dao.FootBallMatchDao; import com.zhu.zqjc.entity.FootBallMatch; import org.quartz.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; @Service public class ScheduleService { @Autowired private Scheduler scheduler; public void testScheduleTask() throws SchedulerException { for (int i = 1; i < 3; i++) { JobDetail jobDetail = JobBuilder.newJob(TestSchedule.class) .withIdentity("updateMatch"+i, "updateMatch") .withDescription("定时比赛Id为"+i) .build(); //cron表达式 表示每隔i秒执行 CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(String.format("0/%d * * * * ? ",i)) .withMisfireHandlingInstructionDoNothing(); CronTrigger cronTrigger = TriggerBuilder.newTrigger() .withIdentity("updateMatch"+i, "updateMatch") .withDescription("定时比赛Id为"+i) .withSchedule(scheduleBuilder) .startNow().build(); scheduler.scheduleJob(jobDetail,cronTrigger); } } } |
TestSchedule.java 定时任务的执行类,在executeInternal函数中执行定时任务的逻辑
package com.zhu.zqjc.schedule; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class TestSchedule extends QuartzJobBean { /** * Execute the actual job. The job data map will already have been * applied as bean property values by execute. The contract is * exactly the same as for the standard Quartz execute method. * * @param context * @see #execute */ @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("定时任务执行 " + context.getJobDetail().getDescription()); } }
|
StartListen.java 执行定时任务
package com.zhu.zqjc.listener; import com.zhu.zqjc.bean.enums.FootballStatus; import com.zhu.zqjc.conf.SystemConf; import com.zhu.zqjc.dao.FootBallMatchDao; import com.zhu.zqjc.entity.FootBallMatch; import com.zhu.zqjc.schedule.ScheduleService; import com.zhu.zqjc.schedule.UpdateMatchSchedule; import org.quartz.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; import weixin.popular.support.TokenManager; import java.util.Arrays; import java.util.List; /** * Created by zhu yingzhi on 2018/6/6. * @author yingzhi zhu * */ @Component public class StartListen implements ApplicationListener<ApplicationReadyEvent> { @Autowired private ScheduleService scheduleService; @Override public void onApplicationEvent(final ApplicationReadyEvent event) { try { scheduleService.testScheduleTask(); } catch (Exception e) { e.printStackTrace(); } } } |
运行效果: