ScheduledTasks(定时任务)

来源:互联网 发布:linux数据库启动命令 编辑:程序博客网 时间:2024/06/02 05:20

spring框架的Scheduling Tasks学习demo
在上一篇ssm框架中添加一些内容
1.目录截图
这里写图片描述
2.在springmvc中添加task以及任务扫描注解以及扫描的test包
这里写图片描述

<context:component-scan base-package="com.fyl.smm.controller,com.fyl.smm.test"/>  <task:annotation-driven/> 

3.TestScheduledTasks类内容(每隔5秒在控制台输出一条语句)

package com.fyl.smm.test;import java.text.SimpleDateFormat;import java.util.Date;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Component //(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)public class TestScheduledTasks {      private static final Logger log = LoggerFactory.getLogger(TestScheduledTasks.class);        private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");        @Scheduled(fixedRate = 5000) //执行定时任务         public void reportCurrentTime() {            log.info("The time is now {}", dateFormat.format(new Date()));        }}

每天记录自己学习的点滴