【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();
}
}

最新文章

  1. Python Day8
  2. NEFU 503 矩阵求解 (非01异或的高斯消元)
  3. 套接字I/O模型-select
  4. Extjs-4.2.1(二)——使用Ext.define自定义类
  5. Index Full Scan vs Index Fast Full Scan-1103
  6. sql - Invalid object name &#39;dbo.in$&#39;
  7. 通过 iTextSharp 实现PDF 审核盖章
  8. IT忍者神龟之使用 PowerDesigner
  9. 利用Matlab生成一个网格化的三维球面(生成直角坐标)
  10. C#读取word文件
  11. BZOJ2282: [Sdoi2011]消防
  12. 重温java基础
  13. [C# 设计模式] Adapter - 适配器模式(两种)
  14. svn提交出现错误 svn: Working copy &#39;D:\...&#39;locked.
  15. Python快速学习05:面向对象
  16. vb.net 日期時間
  17. cf C. Finite or not? 数论
  18. zabbix 自定义监控文本内容
  19. MySQL数据类型--与MySQL零距离接触2-8查看数据表
  20. Linux下启动tomcat报错,WARN org.apache.zookeeper.ClientCnxn - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect java.net.ConnectException:

热门文章

  1. springMvc 通过url传值,实现访问
  2. nyoj 169-素数 (打表)
  3. 【前端知识体系-JS相关】JS基础知识总结
  4. BIM到底是啥?
  5. ReentreantLock:重入锁
  6. ubuntu 16.04 上使用pybind11进行C++和Python代码相互调用 | Interfacing C++ and Python with pybind11 on ubuntu 16.04
  7. &quot;PSP助手”微信小程序宣传视频链接及内容介绍
  8. Win 使用终端创建mysql数据库及使用(5)
  9. windows系统两个网卡如何同时使用?内网和外网共用,配置教程
  10. 【Elasticsearch 7 探索之路】(四)Analyzer 分析