【Spring Boot】定时任务
2024-09-01 18:39:48
【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();
}
}
最新文章
- Python Day8
- NEFU 503 矩阵求解 (非01异或的高斯消元)
- 套接字I/O模型-select
- Extjs-4.2.1(二)——使用Ext.define自定义类
- Index Full Scan vs Index Fast Full Scan-1103
- sql - Invalid object name &#39;dbo.in$&#39;
- 通过 iTextSharp 实现PDF 审核盖章
- IT忍者神龟之使用 PowerDesigner
- 利用Matlab生成一个网格化的三维球面(生成直角坐标)
- C#读取word文件
- BZOJ2282: [Sdoi2011]消防
- 重温java基础
- [C# 设计模式] Adapter - 适配器模式(两种)
- svn提交出现错误 svn: Working copy &#39;D:\...&#39;locked.
- Python快速学习05:面向对象
- vb.net 日期時間
- cf C. Finite or not? 数论
- zabbix 自定义监控文本内容
- MySQL数据类型--与MySQL零距离接触2-8查看数据表
- Linux下启动tomcat报错,WARN org.apache.zookeeper.ClientCnxn - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect java.net.ConnectException:
热门文章
- springMvc 通过url传值,实现访问
- nyoj 169-素数 (打表)
- 【前端知识体系-JS相关】JS基础知识总结
- BIM到底是啥?
- ReentreantLock:重入锁
- ubuntu 16.04 上使用pybind11进行C++和Python代码相互调用 | Interfacing C++ and Python with pybind11 on ubuntu 16.04
- ";PSP助手”微信小程序宣传视频链接及内容介绍
- Win 使用终端创建mysql数据库及使用(5)
- windows系统两个网卡如何同时使用?内网和外网共用,配置教程
- 【Elasticsearch 7 探索之路】(四)Analyzer 分析