浅谈在springboot中使用定时任务的方式
作者:拍打醒 发布时间:2023-03-02 19:14:09
标签:springboot,定时任务
springboot定时任务
在springboot环境下有多种方法,这里记录下使用过的其中两种;1、使用注解,2、通过实现接口的方式。
使用注解的方式虽然比较简单,但是如果项目需要用户对定时周期进行修改操作,只使用注解就比较难实现。所以可以使用实现接口的方式。通过对接口的实现,可以在项目运行时根据需要修改任务执行周期,只需要关闭原任务再开启新任务即可。
1、使用注解方式
首先需要在启动类下添加 @EnableScheduling 注解(@EnableAsync是开启异步的注解)
package com.fongtech.cli;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@MapperScan("com.fongtech.cli.mbg.*.**")
@EnableAsync
@EnableScheduling
public class SpringbootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminApplication.class, args);
}
}
接着在需要用到定时任务的类和方法下加 @Component 和 @Scheduled(cron = "0 0/1 * * * ? ")注解,其中@Scheduled()中的 ‘cron' 有固定的格式。(@Async注解表示开启异步)
@Slf4j
@Component
public class AsyncTaskConfiguration {
/**
* 每分钟检查任务列表,判断任务类型执行相应的任务
* 根据实际任务执行情况,限定执行任务数量
*/
@Scheduled(cron = "0 0/1 * * * ? ")
@Async
public void startCommonTask() throws Exception {
log.info("startCommonTask start........." + Thread.currentThread().getName());
commonTaskService.startCommonTask();
log.info("startCommonTask end........." + Thread.currentThread().getName());
}}
2、使用实现接口的方式
通过实现 SchedulingConfigurer 接口,可对定时任务进行操作。实现接口的方式相比使用注解更加灵活,但需要编写代码,相对繁琐。
实现工具类如下:
package com.fongtech.cli.admin.tasktime;
import com.fongtech.cli.common.util.BeanUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.SchedulingException;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.config.TriggerTask;
import org.springframework.scheduling.support.CronTrigger;
import javax.annotation.PostConstruct;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
/**
* @author linb
* @date 2020/6/15 11:16
*/
@Configuration
//@EnableScheduling
public class DefaultSchedulingConfigurer implements SchedulingConfigurer {
private ScheduledTaskRegistrar taskRegistrar;
private Set<ScheduledFuture<?>> scheduledFutures = null;
private Map<String, ScheduledFuture<?>> taskFutures = new ConcurrentHashMap<>();
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
this.taskRegistrar = taskRegistrar;
}
@SuppressWarnings("unchecked")
private Set<ScheduledFuture<?>> getScheduledFutures() {
if (scheduledFutures == null) {
try {
// spring版本不同选用不同字段scheduledFutures
scheduledFutures = (Set<ScheduledFuture<?>>) BeanUtils.getProperty(taskRegistrar, "scheduledTasks");
} catch (NoSuchFieldException e) {
throw new SchedulingException("not found scheduledFutures field.");
}
}
return scheduledFutures;
}
/**
* 添加任务
*/
public void addTriggerTask(String taskId, TriggerTask triggerTask) {
if (taskFutures.containsKey(taskId)) {
throw new SchedulingException("the taskId[" + taskId + "] was added.");
}
TaskScheduler scheduler = taskRegistrar.getScheduler();
ScheduledFuture<?> future = scheduler.schedule(triggerTask.getRunnable(), triggerTask.getTrigger());
getScheduledFutures().add(future);
taskFutures.put(taskId, future);
}
/**
* 取消任务
*/
public void cancelTriggerTask(String taskId) {
ScheduledFuture<?> future = taskFutures.get(taskId);
if (future != null) {
future.cancel(true);
}
taskFutures.remove(taskId);
getScheduledFutures().remove(future);
}
/**
* 重置任务
*/
public void resetTriggerTask(String taskId, TriggerTask triggerTask) {
cancelTriggerTask(taskId);
addTriggerTask(taskId, triggerTask);
}
/**
* 任务编号
*/
public Set<String> taskIds() {
return taskFutures.keySet();
}
/**
* 是否存在任务
*/
public boolean hasTask(String taskId) {
return this.taskFutures.containsKey(taskId);
}
/**
* 任务调度是否已经初始化完成
*/
public boolean inited() {
return this.taskRegistrar != null && this.taskRegistrar.getScheduler() != null;
}
}
在项目启动后就自动开启任务的操作类如下:
package com.fongtech.cli.admin.tasktime;
import com.fongtech.cli.admin.service.IAuthLoginService;
import com.fongtech.cli.admin.service.IBackupsService;
import com.fongtech.cli.admin.service.IDictionnaryEntryService;
import com.fongtech.cli.mbg.model.entity.AuthLogin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.config.TriggerTask;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
/**
* 项目启动后执行,
*/
@Slf4j
@Component
@Order(value = 1)
public class CmdRunner implements CommandLineRunner {
@Autowired
private DefaultSchedulingConfigurer defaultSchedulingConfigurer;
@Autowired
private IDictionnaryEntryService dictionnaryEntryService;
@Autowired
private IBackupsService backupsService;
@Autowired
private IAuthLoginService authLoginService;
@Override
public void run(String... args) throws Exception {
log.info("------按照预设备份周期启动数据库备份定时任务");
while (!defaultSchedulingConfigurer.inited())
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
}
}
String cron = dictionnaryEntryService.getEntryValueByName("CRON_VALUE");
//默认按照管理员用户权限执行备份任务
AuthLogin authLogin = authLoginService.query().eq(AuthLogin::getLogin_user, "admin").getOne();
//启动线程,按照原表内的时间执行备份任务
defaultSchedulingConfigurer.addTriggerTask("task",
new TriggerTask(
() -> System.out.println("=====----------启动定时任务=-----------");,
new CronTrigger(cron)));
}
}
暂停定时任务:
defaultSchedulingConfigurer.cancelTriggerTask("task");
来源:https://blog.csdn.net/iampatrick_star/article/details/117930163


猜你喜欢
- 这篇文章主要介绍了Java TreeSet类的简单理解和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 目录前言应用定义基本Enum特性Enum的静态导入Enum中添加新方法Switch语句中的EnumEnum的继承EnumSet的使用Enum
- 三元运算符:?很多语言中都有这个运算符,原理都是一样的,区别可能就在于书写的格式上。习惯了excel的方式,总想把多个表达式用 , 号隔开,
- 最近由于编程的需要,对 C# 的类型转换做了一些研究,其内容涉及 C# 的装箱/拆箱/别名、数值类型间相互转换、字符的 ASCII 码和 U
- 已知两个链表list1和list,2,各自非降序排列,将它们合并成另外一个链表list3,并且依然有序,要求保留所有节点。实现过程中,lis
- 关于 LoadLibrary 的疑问Win32 API 中 LoadLibrary 函数的功能是加载某个库文件(通常是 dll 文件),然后
- JSON字符串和java对象的互转【json-lib】在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML、JSON等,JSON
- 最近这段时间一直在看Android,利用Listview去实现点赞功能,下面给大家介绍下基本思路。基本思路:进入界面–》获取数据–》 在Li
- 本章概要文件上传@ControllerAdvice文件上传Java 中的文件上传一共涉及两个组件,一个是 CommonsMultipartR
- 一、简介SpringBoot 中给普通变量注入值只需在变量上添加 @Value 注解即可。application.properties 配置
- 前言:Android的服务是开发Android应用程序的重要组成部分。不同于活动Activity,服务是在后台运行,服务没有接口,生命周期也
- Java画图 给图片底部添加文字标题需求给图片底部添加文字编号import java.awt.Color;import java.awt.F
- 由于最近的工作需要用到文本转语音的功能,在网上找到的资料有些不完整,特此记录下整个完整功能。这种方式的优点在于不会被浏览器限制,在js的文本
- 分页实现的基本过程是这样的:1. 设置自己的分页器的基本参数(可以从配置文件中读取)■每页显示的记录条数■每次最多显示多少页2. 编写设置分
- SpringBoot集成Mybatis+xml格式的sql配置文件最近一直在研究SpringBoot技术,由于项目需要,必须使用Mybati
- 演示 - 初始化销毁顺序/* 初始化和销毁的执行顺序 */@SpringBootApplicationpublic
- 一、概要1.Java虚拟机(Jvm)是什么?2.Java虚拟机是用来干什么的?3.Java虚拟机它的体系结构是什么样子的?4.Java虚拟机
- Java是垃圾回收语言的一种,其优点是开发者无需特意管理内存分配,降低了应用由于局部故障(segmentation fault)导致崩溃,同
- import java.util.Calendar;import java.util.Date;public class Matrix {&
- logback filter过滤某个类 屏蔽某个类使用logback配置日志文件,有的时候需要我们过滤或者屏蔽掉某个类的日志,便可以通过以下