码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot定时任务

时间:2018-12-02 10:27:53      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:col   void   com   conf   app   ram   int   pre   []   

代码做定时任务:1、开个线程,线程里面休眠去做

        2、使用一些定时任务的框架去做

1、创建TimerTest类

package com.cppdy.service;

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

@Component
public class TimerTest {
    
    @Scheduled(fixedRate=2000)
    public void showTime() {
        System.out.println("当前时间戳:"+System.currentTimeMillis());
    }

}

2、在Application类中开启定时任务(@EnableScheduling)

package com.cppdy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Application {
    
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

 

SpringBoot定时任务

标签:col   void   com   conf   app   ram   int   pre   []   

原文地址:https://www.cnblogs.com/cppdy/p/10051961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!