Spring注解方式实现定时任务

目的:实现定时任务
实现方式: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

你可能感兴趣的:(exception,J2EE,SpringMVC,coding)