Spring注解方式实现定时任务

29 篇文章 0 订阅
13 篇文章 0 订阅

目的:实现定时任务
实现方式:Spring定时器,使用注解方式(annotation)
jar包:Spring4.0.6 +Spring-context-support
链接:http://mvnrepository.com/artifact/org.springframework/spring-context-support/4.0.6.RELEASE

.....
@EnableScheduling 
public class certManagerService 
{ 
@Autowired 
private certManageDomain cmDomain; 
//每天下午4点执行
@Scheduled(cron = "0 0 16 * * ?") 
public void timeTask()
{ 
    /**
    * 此处写业务逻辑
    */

}


这地方要注意的是@EnableScheduling注解,在4.x版本是必须要加的,否则任务不会生效,spring 3.x版本是不需要这个注解。
参考:https://www.jianshu.com/p/fd21fd19bb8b
值得注意的是,定时任务方法不可以使用private进行修饰,否则会出现:

"ERROR: org.springframework.scheduling.support.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
java.lang.NullPointerException"

原因可能有几点,上网查询资料如下:

https://community.tableau.com/message/249323 原因是时区问题
http://www.lxway.com/201229422.htm 定时任务中使用 http request 和 response.
https://blog.csdn.net/qq_22585453/article/details/53692023

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值