springboot 定时任务

原创
08/04 16:17
阅读数 52

一、在 DemoApplycation.java 写入如下代码

package com.taven.demo;

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


@EnableAsync
@EnableScheduling
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

二、在 SpringTaskService.java 写入如下代码

package com.taven.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class SpringTaskService {

    private static final Logger log = LoggerFactory.getLogger(SpringTaskService.class);

    @Async
    @Scheduled(cron = "0/1 * * * * *")
    public void scheduled() throws InterruptedException {
        log.info("每1秒执行一次:{}", LocalDateTime.now());
    }

}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
在线直播报名
返回顶部
顶部