首页 > 编程语言 > 详细

使用spring的@Scheduled注解执行定时任务,启动项目不输出警告

时间:2016-10-12 11:33:12      阅读:620      评论:0      收藏:0      [点我收藏+]

在applicationContext.xml中添加:

xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd">


<task:annotation-driven executor="myExecutor" scheduler="myScheduler" />
<task:executor id="myExecutor" pool-size="5" />
<task:scheduler id="myScheduler" pool-size="10" />

 

java代码:

@Component
public class CleanExpireTokenTask {

    private Logger logger = LoggerFactory.getLogger(LogTag.BUSINESS);
    
    @Scheduled(cron = "0 * * * * ?")
    public void startUpdateSaleThread(){
        try{
            System.out.println("check token expire");
        }catch(Exception e){
            logger.error("Make salesReport faild",e);
        }
    }
}

 

注意:

实现类上要加注解@Component

定时器的任务方法不能有返回值

配置及启动报错问题参考自
 

使用spring的@Scheduled注解执行定时任务,启动项目不输出警告

原文:http://www.cnblogs.com/feng-gamer/p/5951707.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!