SpringBoot整合简单的定时任务~
定时任务框架很多种Quartz,SpringTask,xxljob,PowerJob...
1、JDK提供的timer
// JDK提供的
Timer timer = new Timer();
//timer.schedule(new TimerTask() {
// @Override
// public void run() {
// System.out.println("JDK提供的 timer task 执行了~");
// }
//},0);
// 指定时间执行,并且两秒执行一次
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("JDK提供的 timer task 执行了~");
}
}, new Date(), 2000);
2、Quartz
先导入依赖
implementation 'org.springframework.boot:spring-boot-starter-quartz:3.0.2'
定义一个你需要执行的任务
package com.qbb.quartz;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* @author startqbb (个人博客:https://www.cnblogs.com/qbbit)
* @date 2023-02-08 20:56
* @tags 喜欢就去努力的争取
*/
@Slf4j
public class QuartzBean extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
log.info("quartz task run ......");
}
}
定义一个Quartz的配置类,绑定Trigger和JobDetail
package com.qbb.quartz;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author startqbb (个人博客:https://www.cnblogs.com/qbbit)
* @date 2023-02-08 20:57
* @tags 喜欢就去努力的争取
*/
@Configuration
public class QuartzConfig {
@Bean
public JobDetail jobDetail() {
// 绑定Job
return JobBuilder
.newJob(QuartzBean.class)
.storeDurably()
.build();
}
@Bean
public Trigger trigger() {
// 绑定JobDetail
ScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?");
return TriggerBuilder
.newTrigger()
.forJob(jobDetail())
.withSchedule(scheduleBuilder)
.build();
}
}
启动主程序

3、SpringTask
开启SpringTask定时任务
package com.qbb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling // 开启定时任务
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
在需要执行的任务上加@Scheduled(注意需要把当前任务类交给Spring管理)
package com.qbb.springtask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author startqbb (个人博客:https://www.cnblogs.com/qbbit)
* @date 2023-02-08 21:10
* @tags 喜欢就去努力的争取
*/
@Component
@Slf4j
public class SpringTaskDemo {
@Scheduled(cron = "0/1 * * * * ?")
public void print(){
log.info("SpringTask Run ......");
}
}

TODO 整合PowerJob...
SpringBoot整合简单的定时任务~的更多相关文章
- springboot整合xxl-job分布式定时任务【图文完整版】
一.前言 定时任务有很多种,有一些大的框架也有一些简单的实现. 比如常见的: JDK的Timer和TimerTask Quartz异步任务调度框架 分布式定时任务XXL-JOB Spring Task ...
- 【spring-boot】 springboot整合quartz实现定时任务
在做项目时有时候会有定时器任务的功能,比如某某时间应该做什么,多少秒应该怎么样之类的. spring支持多种定时任务的实现.我们来介绍下使用spring的定时器和使用quartz定时器 1.我们使用s ...
- springboot整合Quartz实现定时任务
1.maven依赖: <!--quartz--> <dependency> <groupId>org.quartz-scheduler</groupId> ...
- SpringBoot整合全局异常处理&SpringBoot整合定时任务Task&SpringBoot整合异步任务
============整合全局异常=========== 1.整合web访问的全局异常 如果不做全局异常处理直接访问如果报错,页面会报错500错误,对于界面的显示非常不友好,因此需要做处理. 全局异 ...
- springboot和quartz整合实现动态定时任务(持久化单节点)
Quartz是一个完全由java编写的开源作业调度框架,为在Java应用程序中进行作业调度提供了简单却强大的机制,它支持定时任务持久化到数据库,从而避免了重启服务器时任务丢失,支持分布式多节点,大大的 ...
- SpringBoot 整合Quartz 定时任务框架
更多内容,前往 IT-BLOG 一.Scheduled 定时任务 [1]添加 Scheduled相关依赖,它是 Spring自带的一个 jar包因此引入 Spring的依赖: 1 <depend ...
- springboot整合redis(简单整理)
Redis安装与开启 我这里是在windows上练习,所以这里的安装是指在windows上的安装,操作非常简单,点击https://github.com/MicrosoftArchive/redis/ ...
- SpringBoot整合Quartz定时任务 系统job Spring Boot教程 调度任务
原文地址:https://www.cnblogs.com/allalongx/p/8477368.html 构建工程 创建一个Springboot工程,在它的程序入口加上@EnableScheduli ...
- springBoot整合MyBatise及简单应用
springBoot整合MyBatise及简单应用 我采用的是 工具IDEA 框架是springBoot+maven+Mybatise 第一步: pom.xml 引入相关jar包 <?xml v ...
- SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建
SpringBoot整合Shiro实现基于角色的权限访问控制(RBAC)系统简单设计从零搭建 技术栈 : SpringBoot + shiro + jpa + freemark ,因为篇幅原因,这里只 ...
随机推荐
- ELK环境部署-基础环境安装(一)
ELK简介 ElasticSearch工作原理以及专用名词 ELK是Elasticsearch(ES) , Logstash, Kibana的结合,是一个开源日志收集软件. Elasticsearch ...
- Python colorama 设置控制台、命令行输出彩色文字
为了方便调试代码,经常会向stdout中输出一些日志,但是大量日志,有时不好定位问题. 使用终端打印特定颜色字符串,可以突出显示关键性的信息,帮助用户更好地识别和理解输出内容. https://pyp ...
- TCP vs UDP:揭秘可靠性与效率之争
概述 今天我们开始主要讲解TCP的相关知识点.在之前讲解分层章节的时候,我们提到过一个重要观点.在网络层及以下几层,更多的是让主机与主机建立连接,也就是说你的电脑需要知道另一台电脑在哪里才能连接上它. ...
- 简单的Oracle增删改查笔记
- 深入理解 python 虚拟机:生成器停止背后的魔法
深入理解 python 虚拟机:生成器停止背后的魔法 在本篇文章当中主要给大家介绍 Python 当中生成器的实现原理,尤其是生成器是如何能够被停止执行,而且还能够被恢复的,这是一个非常让人疑惑的地方 ...
- C#学习笔记--变量类型的转换
变量类型的转化: 转换原则 同类型的大的可以装小的,小类型的装大的就需要强制转换. 隐式转换: 同种类型的转换: //有符号 long-->int-->short-->sbyte l ...
- 单元测验3:亲密关系mooc
单元测验3:亲密关系 查看帮助 返回 1 单选(2分) 在亲密关系中,有关权力的表述,以下说法不太准确的的是? A. 对关系付出越多,权力越大. B. 大部分人会倾向认为,在恋爱关系中,男女应该拥 ...
- JDK21的虚拟线程是什么?和平台线程什么关系?
虚拟线程(Virtual Thread)是 JDK 而不是 OS 实现的轻量级线程(Lightweight Process,LWP),由 JVM 调度.许多虚拟线程共享同一个操作系统线程,虚拟线程的数 ...
- 企业FTP搭建教程
安装Vsftpd 提前关闭selinux 和firewalld防火墙 安装vsftp软件包: yum install -y vsftpd* 启动vsftp服务器: systemctl start vs ...
- codeforces #864 div2 B
GCD Partition 这道题首先要解决一个问题,要把区间分成几块,可以证明分成两块是更优 首先我们假设把区间分成了m(>= 2)块 b1, b2, b3, ...,bm,则答案是gcd(b ...