SpringTask实现定时任务方法讲解
作者:龙贝子 时间:2022-06-11 11:43:42
SpringTask是Spring自带的功能。实现起来比较简单。
使用SpringTask实现定时任务有两种方式:
1.注解方式
基于注解@Scheduled
@Scheduled(cron = "*/1 * * * * ?")
public void up(){
System.out.println("定时任务开启:"+System.currentTimeMillis());
}
cron表达式定义定时任务如何去执行。
2.配置文件xml方式
基于xml的方式【@Configuration + @ImportResource + xml】
需要重启应用才能生效
配置xml文件,定义xml文件的名称为task.xml,放置文件在resources文件夹下:
xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!--声明一个具有一个线程的池,如果定义多个,每个对象将获取同样的运行机会-->
<task:scheduler id="sch" pool-size="10"/>
<!--任务的调度类-->
<bean id="scheduleTask" class="com.cloudtop.base.task.ScheduleTask"/>
<!--引用线程池-->
<task:scheduled-tasks scheduler="sch">
<!--年报调度任务 5秒-->
<task:scheduled ref="scheduleTask" method="yearReportTask" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>
</beans>
配置类加载xml文件
package com.cloudtop.base.task;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
/**
* 加载调度的配置文件
*/
@Configuration
@ImportResource(locations={"classpath:task/task.xml"})//加载调度xml
public class SpringTaskConfig {
}
任务的调度类实现
package com.cloudtop.base.task;
import com.cloudtop.base.error.exception.BusinessException;
import com.cloudtop.core.service.EnvironmentUpService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 定时任务类
*/
public class ScheduleTask {
@Autowired
EnvironmentUpService environmentUpService;
public void yearReportTask() throws BusinessException {
System.out.println("*******定时任务执行的业务代码******");
}
}
最后,第一种使用注解@EnableSchedu ling开启定时任务,第二种使用xml的方式配置好上面的三个文件就开启了定时任务,不用使用注解@EnableSchedu ling来开启定时任务。
@SpringBootApplication
@ServletComponentScan
@EnableAutoConfiguration(exclude = {MultipartAutoConfiguration.class})
@EnableSchedu ling
public class CloudtopWebFrameApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(CloudtopWebFrameApplication.class);
}
/**
* 主程序入口
* 所有SpringBoot项目均采用main方法启动主程序,该部分为必须项
* @param args
*/
public static void main(String[] args) {
SpringApplication.run(CloudtopWebFrameApplication.class, args);
}
}
最后在控制台会输出结果:
来源:https://blog.csdn.net/qq_30624649/article/details/129020602
标签:SpringTask,SpringBoot,定时任务
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
javaweb Servlet开发总结(二)
2023-10-31 11:51:48
![](https://img.aspxhome.com/file/2023/7/58567_0s.png)
java生成图片验证码功能
2023-06-27 00:31:55
![](https://img.aspxhome.com/file/2023/4/60994_0s.png)
java POI解析Excel 之数据转换公用方法(推荐)
2023-06-10 11:04:53
解决Map集合使用get方法返回null抛出空指针异常问题
2023-11-25 00:14:00
![](https://img.aspxhome.com/file/2023/8/59848_0s.png)
Java设计模式的事件模型详解
2023-11-29 04:47:08
![](https://img.aspxhome.com/file/2023/2/59732_0s.png)
MyBatis-Plus 查询返回实体对象还是map
2023-11-28 03:20:19
JavaFX实现简单日历效果
2023-05-16 08:43:30
![](https://img.aspxhome.com/file/2023/5/61325_0s.jpg)
Java ThreadLocal的使用详解
2023-11-29 04:48:43
AQS加锁机制Synchronized相似点详解
2023-08-04 22:36:55
![](https://img.aspxhome.com/file/2023/8/57918_0s.png)
ShardingSphere数据分片算法及测试实战
2023-11-28 02:23:03
![](https://img.aspxhome.com/file/2023/8/59768_0s.png)
深度解析Java中ArrayList的使用
2023-06-16 23:26:01
![](https://img.aspxhome.com/file/2023/6/60696_0s.png)
Java操作Mongodb数据库实现数据的增删查改功能示例
2023-11-25 00:02:05
Java中HashMap里面key为null存放到哪
2023-11-10 02:46:47
springboot嵌套子类使用方式—前端与后台开发的注意事项
2023-09-16 12:37:22
如何用java程序(JSch)运行远程linux主机上的shell脚本
2023-11-24 12:35:58
Java最全文件操作实例汇总
2023-11-14 13:00:17
SpringBoot深入探究@Conditional条件装配的使用
2021-08-18 00:06:53
![](https://img.aspxhome.com/file/2023/2/61792_0s.png)
java 创建线程的几种方式
2023-10-29 19:40:16
![](https://img.aspxhome.com/file/2023/5/58695_0s.png)
解析spring cloud ouath2中的Eureka
2023-10-12 04:07:54
![](https://img.aspxhome.com/file/2023/6/61006_0s.png)
Java类中this关键字与static关键字的用法解析
2023-11-09 22:45:19
![](https://img.aspxhome.com/file/2023/1/59171_0s.png)