springboot定时器*(简单)

首先引入定时器所需依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

然后在启动类添加注解@EnableScheduling

最后使用定时器

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Auther: AmbroseLee
 * @Date: 2018/9/6 15:48
 * @Description:    定时器1
 */
@Component
public class SchedulerTask {
    private int count=0;
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(cron="*/6 * * * * ?")
    private void process(){
        System.out.println("this is scheduler task runing  "+(count++));
    }


    @Scheduled(fixedRate = 6000)
    public void reportCurrentTime() {
        System.out.println("现在时间:" + dateFormat.format(new Date()));
    }
}

效果

先这么简单用吧。然后更新详细用法


作者:AmbroseLe
链接:https://juejin.im/post/5b90dd46f265da0a8c6c02f7
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/qq_32786139/article/details/82462828