【Spring Boot】定时任务
【Spring Boot】定时任务
测试用业务Service
package com.example.schedule.service;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void syncUserData(){
System.out.println("syncUserData");
}
} package com.example.schedule.service;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
public void syncStudentData(){
System.out.println("syncStudentData");
}
}
1、使用Spring的@Scheduled注解
使用@EnableScheduling注解启用Spring定时任务
package com.example.schedule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class ScheduleApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduleApplication.class, args);
}
}
定时方法
package com.example.schedule.scheduled_bean;
import com.example.schedule.service.StudentService;
import com.example.schedule.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class YcxScheduledBean {
@Autowired
UserService userService;
@Autowired
StudentService studentService;
@Scheduled(cron="*/2 * * * * ?")
public void syncData() {
System.out.println(">>> YcxScheduledBean");
userService.syncUserData();
studentService.syncStudentData();
}
}
2、使用quartz
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
继承QuartzJobBean
package com.example.schedule.job; import com.example.schedule.service.StudentService;
import com.example.schedule.service.UserService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean; public class YcxSyncJob extends QuartzJobBean {
@Autowired
UserService userService; @Autowired
StudentService studentService;
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println(">>> QuartzJobBean 同步任务");
userService.syncUserData();
studentService.syncStudentData();
}
}
【Spring Boot】定时任务的更多相关文章
- Spring Boot定时任务应用实践
在Spring Boot中实现定时任务功能,可以通过Spring自带的定时任务调度,也可以通过集成经典开源组件Quartz实现任务调度. 一.Spring定时器 1.cron表达式方式 使用自带的定时 ...
- spring boot.定时任务问题记录(TaskScheduler/ScheduledExecutorService异常)
一.背景 spring boot的定时任务非常简单,只需要在启动类中加上@EnableScheduling注解,然后在对应的方法上配置@Scheduled就可以了,系统会自动处理并按照Schedule ...
- (14)Spring Boot定时任务的使用【从零开始学Spring Boot】
本文介绍在 Spring Boot 中如何使用定时任务,使用非常简单,就不做过多说明了. com.kfit.base.scheduling.SchedulingConfig: package com. ...
- Spring Boot 定时任务单线程和多线程
Spring Boot 的定时任务: 第一种:把参数配置到.properties文件中: 代码: package com.accord.task; import java.text.SimpleDat ...
- Spring Boot (十一): Spring Boot 定时任务
在实际的项目开发工作中,我们经常会遇到需要做一些定时任务的工作,那么,在 Spring Boot 中是如何实现的呢? 1. 添加依赖 在 pom.xml 文件中只需引入 spring-boot-sta ...
- Spring Boot 定时任务 @Scheduled
项目开发中经常需要执行一些定时任务,比如在每天凌晨,需要从 implala 数据库拉取产品功能活跃数据,分析处理后存入到 MySQL 数据库中.类似这样的需求还有许多,那么怎么去实现定时任务呢,有以下 ...
- Spring Boot定时任务运行一段时间后自动关闭的解决办法
用Spring Boot默认支持的 Scheduler来运行定时任务,有时在服务器运行一段时间后会自动关闭.原因:Schedule默认是单线程运行定时任务的,即使是多个不同的定时任务,默认也是单线程运 ...
- Spring Boot 定时任务 Quartz 使用教程
Quartz是一个完全由java编写的开源作业调度框架,他使用非常简单.本章主要讲解 Quartz在Spring Boot 中的使用. 快速集成 Quartz 介绍 Quartz 几个主要技术点 Qu ...
- spring -boot定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现
spring 定时任务 quartz 基于 MethodInvokingJobDetailFactoryBean 实现 依赖包 如下 <dependencies> <depende ...
- spring boot 定时任务
定时任务实现方式 三种: 1) Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务. 最早的时候就是这样写定时任务的. 2) 开源的第三方框 ...
随机推荐
- shell——数组
默认从0开始索引:也可以单独(像字典一样)pid[35420]=httpd -k ssl, 只能是一维的 bash4.0增加了关联数组 数组赋值: declare -a myarray声明数组 一次一 ...
- 2-SAT习题讲解
2-SAT习题讲解 讲在前面:下述例题不是按照难度顺序的,而且基本就只会讲解建图的过程.下面讲解中$A'$为$A$的反向状态. 一.bzoj习题 例一:$bzoj2199 奶牛议会$ 首先我们考虑本题 ...
- keypress 和 blur 事件冲突的问题
需求:点击需求:点击添加标签,出来input框,内容输入完成后点击enter键和blur时都可以执行提交标签的效果,提交时对内容进行判断,执行完成后清除input内的内容.如下图 问题:内容输入完成后 ...
- 给公司写的composer包开发的规范
版本格式 主版本号.次版本号.修订号 版本号递增规则 主版本号:当你做了不兼容的 API 修改 次版本号:当你做了向下兼容的功能性新增 修订号:当你做了向下兼容的问题修正 先行版本号及版本编译元数据可 ...
- PHP中的服务容器与依赖注入的思想
依赖注入 当A类需要依赖于B类,也就是说需要在A类中实例化B类的对象来使用时候,如果B类中的功能发生改变,也会导致A类中使用B类的地方也要跟着修改,导致A类与B类高耦合.这个时候解决方式是,A类应该去 ...
- vue 做一个简单的TodoList
目录结构 index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...
- pynlp报错:pynlpir.LicenseError: Your license appears to have expired. Try running "pynlpir update"解决办法。
使用pyltp做文本挖掘时报错: pynlpir.LicenseError: Your license appears to have expired. Try running "pynlp ...
- httpclient常规封装的方法
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-compr ...
- insertBefore()
insertBefore()方法将把一个给定的节点插入到一个给定元素节点的给定子节点前面,他返回一个指向新增子节点的引用指针: reference = element.insertBefore(new ...
- SQL Server2017 安装完成后找不到启动项解决方案
很多用于当SQL Server2017 安装完成后开始菜单找不到启动项无法启动SQL Server2017 其实你只需要安装一下SSMS-Setup-CHS就可以了 安装完成之后就有了 SSMS-Se ...