spring cloud 集成 quartz (注解方式)

相比于scheduler,quartz支持分布式、持久化。当你部署多个实例时,定时任务会在多个实例上调度执行,提升了处理能力,同时多个实例也不会重复执行同一任务。而且也保证了服务的可靠性和高可用。

依赖

    implementation("org.springframework.boot:spring-boot-starter-quartz")

现在我都用gradle进行依赖管理,比maven简洁和智能。
gradle后台使用的也是maven源。

配置

application.yml

spring:
  quartz:
    job-store-type: jdbc
    jdbc:
      initialize-schema: always
    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,我们需要有一个数据源,比如mysql:

spring:
  datasource:
    url: jdbc:mysql://192.168.1.11:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: xxxxx
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      minimum-idle: 5
      idle-timeout: 180000
      maximum-pool-size: 10
      max-lifetime: 1800000
      connection-timeout: 30000
      connection-test-query: select 1

和平时的datasource是一致的。

定时任务代码

注解配置trigger

代码是用kotlin写的:

@Configuration
class QuartzSchedulerConfig {


    @Bean
    fun jobTrigger(jobDetail:JobDetail):CronTriggerFactoryBean{
        val trigger = CronTriggerFactoryBean()
        trigger.setJobDetail(jobDetail)
        trigger.setCronExpression("0/15 * * * * ?")
        return trigger
    }

    @Bean
    fun jobDetail():JobDetailFactoryBean{
        val jobDetailFactoryBean = JobDetailFactoryBean()
        jobDetailFactoryBean.setJobClass(WeatherJob::class.java)
        jobDetailFactoryBean.setDurability(true)
        return jobDetailFactoryBean
    }


}

任务代码

@Component
class WeatherJob : QuartzJobBean() {
    private val log:Logger = LoggerFactory.getLogger(WeatherJob::class.java)
    /**
     * 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.
     * @see .execute
     */
    override fun executeInternal(context: JobExecutionContext) {
        log.info("start job")
    }
}

每15秒会调用上面的executeInternal方法。

完成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiegwei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值