1.项目基础

项目是基于Spring Boot2.x版本的

2.添加依赖

        <!-- quartz依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

3.yml配置

application-quartz.yml的配置内容如下

spring:
quartz:
#相关属性配置
properties:
org:
quartz:
scheduler:
instanceName: clusteredScheduler
instanceId: AUTO
jobStore:
class: org.quartz.impl.jdbcjobstore.JobStoreTX
driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
tablePrefix: QRTZ_
isClustered: true
clusterCheckinInterval: 10000
useProperties: false
threadPool:
class: org.quartz.simpl.SimpleThreadPool
threadCount: 10
threadPriority: 5
threadsInheritContextClassLoaderOfInitializingThread: true
#数据库方式
job-store-type: jdbc
#初始化表结构
#jdbc:
#initialize-schema: never

4.创建任务测试类

  • 简单任务

代码如下:

public class MyJob extends QuartzJobBean {

    @Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("start My Job:" + LocalDateTime.now());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("end My Job:" + LocalDateTime.now()); }
}

CRON任务

public class MyCronJob extends QuartzJobBean {

    @Autowired
IndexController indexController; @Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("任务执行了" + new Date());
// indexController.testMail();
}
}

5.Java配置(QuartzConfiguration)

@Configuration
public class QuartzConfiguration { // 使用jobDetail包装job
@Bean
public JobDetail myJobDetail() {
return JobBuilder.newJob(MyJob.class).withIdentity("myJob").storeDurably().build();
} // 把jobDetail注册到trigger上去
@Bean
public Trigger myJobTrigger() {
SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(15).repeatForever(); return TriggerBuilder.newTrigger()
.forJob(myJobDetail())
.withIdentity("myJobTrigger")
.withSchedule(scheduleBuilder)
.build();
} // 使用jobDetail包装job
@Bean
public JobDetail myCronJobDetail() {
return JobBuilder.newJob(MyCronJob.class).withIdentity("myCronJob").storeDurably().build();
} // 把jobDetail注册到Cron表达式的trigger上去
@Bean
public Trigger CronJobTrigger() {
CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/10 * * * * ?"); return TriggerBuilder.newTrigger()
.forJob(myCronJobDetail())
.withIdentity("myCronJobTrigger")
.withSchedule(cronScheduleBuilder)
.build();
}
}

其实上面的配置就等价于在传统的xml中配置bean是一样的。

6.启动测试

至此,SpringBoot集成Quartz的完毕。

四、总结

  • Spring Boot集成quartz还是比较简单的。
  • 其实还有更高级的用法,就是前台动态创建和控制定时任务,后面有时间再完善。大家先把这种最简单的基本用法熟练掌握。

SpringBoot之整合Quartz调度框架-基于Spring Boot2.0.2版本的更多相关文章

  1. SpringBoot之整合Redis分析和实现-基于Spring Boot2.0.2版本

    背景介绍 公司最近的新项目在进行技术框架升级,基于的Spring Boot的版本是2.0.2,整合Redis数据库.网上基于2.X版本的整个Redis少之又少,中间踩了不少坑,特此把整合过程记录,以供 ...

  2. 基于spring boot2.0+spring security +oauth2.0+ jwt微服务架构

    github地址:https://github.com/hankuikuide/microservice-spring-security-oauth2 项目介绍 该项目是一个演示项目,主要演示了,基于 ...

  3. Spring Boot2.0以上版本EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer替代

    在Spring Boot2.0以上配置嵌入式Servlet容器时EmbeddedServletContainerCustomizer类不存在,经网络查询发现被WebServerFactoryCusto ...

  4. Spring Boot2.0 静态资源被拦截问题

    在Spring Boot2.0+的版本中,只要用户自定义了拦截器,则静态资源会被拦截.但是在spring1.0+的版本中,是不会拦截静态资源的. 因此,在使用Spring Boot2.0+时,配置拦截 ...

  5. Spring Boot2.0之整合事物管理

    首先Spring 事务分类 1.声明事务  原理:基于编程事务的 2.编程事务  指定范围 扫包去解决 3.事务原理:AOP技术   通过环绕通知进行了拦截 使用Spring 事务注意事项: 不要tr ...

  6. Spring Boot2.0 整合 Kafka

    Kafka 概述 Apache Kafka 是一个分布式流处理平台,用于构建实时的数据管道和流式的应用.它可以让你发布和订阅流式的记录,可以储存流式的记录,并且有较好的容错性,可以在流式记录产生时就进 ...

  7. 【spring boot】整合LCN,启动spring boot2.0.3 启动报错:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

    spring boot 2.0.3启动报错: Error starting ApplicationContext. To display the conditions report re-run yo ...

  8. spring boot 2.0(一)权威发布spring boot2.0

    Spring Boot2.0.0.RELEASE正式发布,在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误,然后Spring ...

  9. Spring Boot2.0使用Spring Security

     一.Spring Secutity简介     Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性 ...

随机推荐

  1. JavaScript基础函数的配置对象Configuration Objects(020)

    配置对象通常用在API库的实现中,当程序中需要编写要多次的模块,也可以采用这种模式.这种模式的好处是接口明确,扩展方便.比如,一个 addPerson在设计的最初需要两个参数作为初始化时人的姓名: f ...

  2. 第一步:安装centos_8

    关于centos的安装其实大部分时候都是在虚拟机环境下安装. 好处无疑有这几个:方便,快速,主要就是整出事情了我可以直接删了重装 我这边是在vmware下进行一个安装 vmware我这边给出下载链接: ...

  3. 注解式HTTP请求Feign (F版)

    Spring Cloud 为开发者提供了在分布式系统中的一些常用的组件(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁定,决策竞选,分布式会话集群状态).使用Sprin ...

  4. 服务消费者(Ribbon)

    上一篇文章,简单概述了服务注册与发现,在微服务架构中,业务都会被拆分成一个独立的服务,服务之间的通讯是基于http restful的,Ribbon可以很好地控制HTTP和TCP客户端的行为,Sprin ...

  5. Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available

    连接elasticsearch已经成功,但是会报以下错误,字面意思是节点不可用这样 Exception in thread "main" NoNodeAvailableExcept ...

  6. redis基础02-redis的5种对象数据类型

    表格引用地址:http://www.cnblogs.com/xrq730/p/8944539.html 参考书籍:<Redis设计与实现>,<Redis运维与开发> 1.对象 ...

  7. day68 form组件

    目录 一.自定义分页器的拷贝和使用 二.Forms组件 1 前戏 2 form组件的基本功能 3 基本使用 4 基本方法 5 渲染标签 6 展示提示信息 7 钩子函数(HOOK) 8 forms组件其 ...

  8. MYSQL 之 JDBC(八):增删改查(六)ReflectionUtils

    这里在网上找了一份ReflectionUtils package com.litian.jdbc; /** * @author: Li Tian * @contact: litian_cup@163. ...

  9. Flask 基础组件(三):路由系统

    1. 常见路由 @app.route('/user/<username>') @app.route('/post/<int:post_id>') @app.route('/po ...

  10. 使用Vue做出跑马灯效果

     <div id="pmd">         <h4> {{msg}}</h4>         <input type="b ...