关于@Scheduled不执行的原因分析
作者:生活不只*眼前的苟且 时间:2021-06-27 17:13:20
@Scheduled不执行的原因
1. 今天用@Schedule做了一个定时任务
希望凌晨1点执行,代码如下
@Service
public class ParseJsonService {
@Scheduled(cron = "0 0 1 * * ?")
public void parseMongodbDataToJson() {
}
}
第二天来公司了,发现根本没有执行。然后开始查找问题
2. 首先遇到查到的一个解决方案
说是spring的版本的问题,我看了下我以前用的定时任务,的确spring用的是4. 于是我将spring的版本换成了4,发现还是不好使
3. 还有版本说要在spring
的配置文件中加上注解驱动
<task:annotation-driven />
并且还要加上default-lazy-init="false"属性,说是不让spring懒加载,但是发现还是不行
4. 又查到版本说要在Bean上加上@Lazy(false)
让spring对该Bean在启动的时候就加载,但是发现还是不行
终极解决方案(适合我,不一定适合你, 我用的Spring版本是3,不是4)
第一步:在spring的配置文件中加上
<task:annotation-driven />
第二步:在上述Bean上加上
@EnableScheduling
代码如下:
@Service
@EnableScheduling
public class ParseJsonService {
@Scheduled(cron = "0 0 1 * * ?")
public void parseMongodbDataToJson() {
}
}
@Scheduled不生效的排查记录
@Scheduled 不生效的排查记录
公司之前项目要加个定时处理,觉得so easy。
<!-- 开启定时任务 -->
<task:annotation-driven />
<!-- 开启注解 -->
<context:annotation-config />
<!-- 指定相关的包路径 -->
<context:component-scan base-package="com.ffrj.schedule"/>
@Component
public class CommonSchedule {
//@Scheduled(cron = "0 0 1 * * ?")
@Scheduled(cron = "*/5 * * * * ?")
public void avg() {
System.out.println("现在开始计算平均值 当前时间为");
}
}
本以为就搞定了 ,发现不生效!!!
开始网上查询资料。
@EnableScheduling
也加了 不生效!@Lazy(false)
也加了 不生效!!
开始有点慌了,看了下包路径 没问题啊 ,想着用xml 配一遍
<task:annotation-driven />
<bean id="myTaskXml" class="com.ffrj.schedule.CommonSchedule "></bean>
<task:scheduled-tasks>
<task:scheduled ref="myTaskXml" method="show" cron="*/5 * * * * ?" />
</task:scheduled-tasks>
<context:component-scan base-package="com.ffrj.schedule" />
发现还是不生效!!!!!!!
这时候前端访问了一下我的接口 ,看了看log , * system 打印了 !!!
冷静了一下
@Lazy(false) 不生效??没道理啊 然后开始翻配置文件看是不是有些bean 添加 default-lazy-init=“true” 属性了 ,也没有啊。
最后发现 :
不知道哪位奇葩 把这行给注掉了。
果断取消注释 解决问题。
来源:https://denglinjie.blog.csdn.net/article/details/52849868
标签:@Scheduled,不执行
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 数据结构与算法系列精讲之KMP算法
2023-05-06 14:55:55
![](https://img.aspxhome.com/file/2023/7/64907_0s.jpg)
JAVA实现单例模式的四种方法和一些特点
2023-11-02 05:38:20
利用Flutter制作经典贪吃蛇游戏
2023-07-21 07:04:13
![](https://img.aspxhome.com/file/2023/4/84724_0s.png)
java寻找迷宫路径的简单实现示例
2021-07-06 13:17:50
Springboot如何根据实体类生成数据库表
2023-11-20 13:54:39
![](https://img.aspxhome.com/file/2023/7/59677_0s.jpg)
dubbo将异常转换成RuntimeException的原因分析 ExceptionFilter
2023-11-24 14:25:13
![](https://img.aspxhome.com/file/2023/3/64143_0s.jpg)
SpringMvc+Mybatis+Pagehelper分页详解
2021-08-13 14:15:11
springboot项目打成war包部署到tomcat遇到的一些问题
2023-10-12 12:46:46
教你怎么用Java开发扫雷游戏
2023-07-22 09:49:26
![](https://img.aspxhome.com/file/2023/0/63480_0s.png)
java打印指定年月的日历
2023-11-11 19:21:19
![](https://img.aspxhome.com/file/2023/2/59062_0s.jpg)
Struts 2中实现Ajax的三种方式
2022-04-30 05:46:28
![](https://img.aspxhome.com/file/2023/7/65307_0s.jpg)
解决Mybatis的@Param()注解导致分页失效的问题
2022-05-01 13:24:53
![](https://img.aspxhome.com/file/2023/0/63400_0s.jpg)
MyBatis Plus 入门使用详细教程
2023-08-23 05:48:33
![](https://img.aspxhome.com/file/2023/3/58153_0s.png)
Flutter路由传递参数及解析实现
2023-06-22 11:48:45
![](https://img.aspxhome.com/file/2023/5/90155_0s.gif)
Mybatis-Plus的使用详解
2022-01-18 18:39:27
![](https://img.aspxhome.com/file/2023/1/61801_0s.jpg)
Idea中SpringBoot多模块项目的建立实现
2023-11-08 07:52:34
![](https://img.aspxhome.com/file/2023/3/58743_0s.png)
Java设计模式之备忘录模式实现对象状态的保存和恢复
2023-08-28 15:22:27
![](https://img.aspxhome.com/file/2023/9/58439_0s.png)
一文探寻Java装箱和拆箱的奥妙
2022-08-15 21:41:21
SpringBoot 整合 Shiro 密码登录的实现代码
2023-11-10 11:27:59
Java Web Fragment在项目中使用方法详解
2022-04-11 14:17:38
![](https://img.aspxhome.com/file/2023/5/64775_0s.png)