SpringBoot集成Quartz定时器框架

SpringBoot集成Quartz框架


前言

案例github地址(如果有用点个star呗) https://github.com/chenxiban/BlogCaseSet.git


Quartz任务调度框架

Quartz是一个任务调度框架。比如你遇到这样的问题

  • 想每月10号,信用卡自动还款
  • 想每年4月1日自己给当年暗恋女神发一封匿名贺卡
  • 想每隔1小时,备份一下自己的学习笔记到云盘
  • 想每隔半小时,扫描订单信息等


这些问题总结起来就是:在某一个有规律的时间点干某件事。并且时间的触发的条件可以非常复杂(比如每
月最后一个工作日的17:50),复杂到需要一个专门的框架来干这个事。 Quartz就是来干这样的事,你给它
一个触发条件的定义,它负责到了时间点,触发相应的Job起来干活。


SpringBoot框架提供了对Quartz框架的整合,并且整合起来超级简单,下面介绍集成的实现具体步骤:

  1. 引入Quartz依赖;
<!-- Spring Quartz依赖 -->
<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>2.2.1</version>
	<exclusions>
		<exclusion>
			<artifactId>slf4j-api</artifactId>
			<groupId>org.slf4j</groupId>
		</exclusion>
	</exclusions>
</dependency>
  1. 我们需要使用注解@Configuration来定义一个配置类,代码如下:
package com.cyj.springboot.config;

import java.text.SimpleDateFormat;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConfigMyTools {

	/**
	 * 格式化当前日期
	 * 
	 * @return
	 */
	@Bean("dateFormat")
	public SimpleDateFormat dateFormat() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	}

}
  1. 为了方便我们看清楚任务调度的次数,我们声明一个辅助类,代码如下:
package com.cyj.springboot.config;

import org.springframework.stereotype.Component;

@Component("myTask")
public class MyTask {

	private int count = 0;

	public void say() {
		System.out.println("大家好,我是springboot任务调度=>" + count++);
	}

}
  1. 接下创建一个任务调度类,代码如下:
package com.cyj.springboot.quartz;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.cyj.springboot.config.MyTask;

@Component
@EnableScheduling
public class SimpleTask {

	@Autowired
	private MyTask myTask;
	@Resource
	private SimpleDateFormat dateFormat;

	@Scheduled(fixedRate = 1000 * 3) // 每隔三秒
	public void reportCurrentTime() {
		myTask.say();// 执行任务方法
		System.out.println("每隔3秒任务调度一次  现在时间 " + dateFormat.format(new Date()));
	}

	@Scheduled(cron = "*/5 * * * * ? ") // 每隔5秒
	public void reportCurrentByCron() {
		System.out.println(
				"每隔5秒任务调度一次 Scheduling Tasks Examples By Cron: The time is now " + dateFormat.format(new Date()));
	}

}

备注 cron = "*/5 * * * * ? "

表达式表示秒分时日月年。*/5表示每隔5秒。
在任务类中使用两个注解@EnableScheduling和@Scheduled(Cron表达式)。具体作用如下:@EnableScheduling:放在类前,标注启动定时任务; @Scheduled(表达式): 放在方法前,定义某个
定时任务;

  1. 最后定义主模块启动类,启动测试,如下所示:
大家好,我是springboot任务调度=>2
每隔3秒任务调度一次  现在时间 2019-11-18 14:00:54
每隔5秒任务调度一次 Scheduling Tasks Examples By Cron: The time is now 2019-11-18 14:00:55
大家好,我是springboot任务调度=>3
每隔3秒任务调度一次  现在时间 2019-11-18 14:00:57
每隔5秒任务调度一次 Scheduling Tasks Examples By Cron: The time is now 2019-11-18 14:01:00
大家好,我是springboot任务调度=>4
每隔3秒任务调度一次  现在时间 2019-11-18 14:01:00

最后

  • 更多参考精彩博文请看这里:《陈永佳的博客》

  • 喜欢博主的小伙伴可以加个关注、点个赞哦,持续更新嘿嘿!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

陈永佳

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

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

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

打赏作者

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

抵扣说明:

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

余额充值