前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring boot 2.0 quartz 轻松实现定时任务和作业调度

spring boot 2.0 quartz 轻松实现定时任务和作业调度

作者头像
算法与编程之美
发布2019-07-17 17:24:49
1.4K0
发布2019-07-17 17:24:49
举报

欢迎点击「算法与编程之美」↑关注我们!

本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列文章。

作者|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(); } } }

运行效果:

拓展阅读:

深入理解遗传算法(一)

深入理解遗传算法(二)

从1到100求和学算法思维(一)

从1到100求和学算法思维(二)

从1到100求和学算法思维(三)

从1到100求和学算法思维(四)

从1到100求和学算法思维(五)

从1到100求和学算法思维(六)

where2go 团队


微信号:算法与编程之美

温馨提示:点击页面右下角“写留言”发表评论,期待您的参与!期待您的转发!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-05-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 算法与编程之美 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档