本文主要是介绍项目实战中的Quartz定时器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
这里是我真实项目中使用的,个人认为是比较全一点的,如果不足,还希望多多包涵或者补充
接2文章的基础上又简单更改了下,变得更加规范
1、依赖
<!--quartz--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId></dependency><dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz-jobs</artifactId><version>2.2.1</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.5</version></dependency>
2、添加quartz.properties配置
#默认或是自己改名字都行
org.quartz.scheduler.instanceName=HeaelrJeanQuartzScheduler
#如果使用集群,instanceId必须唯一,设置成AUTO
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.jobFactory.class=org.quartz.simpl.SimpleJobFactory
org.quartz.scheduler.autoStartup=true
org.quartz.scheduler.skipUpdateCheck=trueorg.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount=30
org.quartz.threadPool.threadPriority=5#是否使用集群(如果项目只部署到 一台服务器,就不用了)
rg.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval=20000
#存储方式使用JobStoreTX,也就是数据库
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate
#misfireThreshold是用来设置调度引擎对触发器超时的忍耐时间,简单来说 假设misfireThreshold=6000(单位毫秒)。
#那么它的意思说当一个触发器超时时间如果大于misfireThreshold的值 就认为这个触发器真正的超时(也叫Misfires)。
org.quartz.jobStore.misfireThreshold=60000
#存储的JobDataMaps是否都为String类型
org.quartz.jobStore.useProperties=true# 数据源 我们自己配置
org.quartz.jobStore.dataSource=healerjean# 数据源使用quartz配置
#org.quartz.jobStore.tablePrefix = QRTZ_
#org.quartz.jobStore.dataSource = myDS
#
#
##配置数据源
#数据库中quartz表的表名前缀
#
#org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver
#org.quartz.dataSource.myDS.URL = jdbc:mysql://localhost:3306/com_hlj_quartz?characterEncoding=utf-8
#org.quartz.dataSource.myDS.user = root
#org.quartz.dataSource.myDS.password = 123456
#org.quartz.dataSource.myDS.maxConnections = 5
4、sprinboot配置文件
4.1、主配置文件
spring.application.name=quartz
spring.profiles.active=demo#freemarker
spring.freemarker.charset=UTF-8
spring.freemarker.cache=true
spring.freemarker.enabled=true
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.request-context-attribute=request
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=#aop
spring.aop.auto=true
spring.aop.proxy-target-class=true#spring data jpa properties
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.database=MYSQL
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.data.jpa.repositories.enabled=true#log level
logging.level.root=info
logging.level.org.springframework=ERROR
logging.level.org.mybatis=ERROR
logging.level.org.mybatis.example.BlogMapper=debug#logging.level.com.duodian.youhui.dao.mybatis.coupon=debug#static
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
#
#
##出现错误时, 直接抛出异常 路径访问错误也抛出异常
#spring.mvc.throw-exception-if-no-handler-found=true
#spring.resources.add-mappings=false
4.2、启动配置文件
server.port=8080########################################################
###datasource mysql
########################################################
spring.datasource.url=jdbc:mysql://localhost:3306/com_hlj_demo?useUnicode=true&allowMultiQueries=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000#datasource healerjean 这个我们自己配置
healerjean.datasource.url=jdbc:mysql://localhost:3306/com_hlj_quartz?characterEncoding=utf-8
healerjean.datasource.username=root
healerjean.datasource.password=123456
healerjean.datasource.driver-class-name=com.mysql.jdbc.Driverlogging.config=classpath:logback.xml
3、数据表采用文章2中的数据库表,这里就不粘贴了
4、定时器配置,使用配置文件以及数据源
4.1、单独为定时任务创建了数据源(其实和主数据源是一起的)
4.2、添加了定时器监听入口
package com.hlj;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;import javax.sql.DataSource;/*** 类描述:* 创建人: HealerJean*/
@Configuration
public class QuartzConfig {@Value("${healerjean.datasource.url}")private String healerjeanUrl;@Value("${healerjean.datasource.username}")private String healerjeanname;@Value("${healerjean.datasource.password}")private String healerjeanPassword;@Beanpublic SpringBeanJobFactory jobFactory (){return new SpringBeanJobFactory();}@Beanpublic SchedulerFactoryBean schedulerFactoryBean (SpringBeanJobFactory jobFactory) {SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();schedulerFactoryBean.setDataSource(createAdmore());schedulerFactoryBean.setConfigLocation(new ClassPathResource("quartz.properties"));schedulerFactoryBean.setJobFactory(jobFactory);schedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(true);schedulerFactoryBean.setGlobalJobListeners(new com.duodian.admore.quartz.core.event.HealerJeanJobListener());schedulerFactoryBean.setOverwriteExistingJobs(true);return schedulerFactoryBean;}/*** 单独为定时任务创建一个单独的datasource*/private DataSource createAdmore(){DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setUrl
这篇关于项目实战中的Quartz定时器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!