Spring boot with Scheduling

本文节选自《Netkiller Java 手札》

http://www.netkiller.cn/java/spring/boot/index.html

11.17. Spring boot with Scheduling

11.17.1. Application.java

package api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan({ "api.config", "api.web", "api.rest", "api.service","api.schedule" })
@EnableMongoRepositories
@EnableJpaRepositories
@EnableScheduling
public class Application {

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

}			

开启计划任务 @EnableScheduling

确保你的计划任务在 @ComponentScan 包中。

11.17.2. Component

package api.schedule;

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
public class ScheduledTasks {

	private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	public final static long ONE_DAY = 24 * 60 * 60 * 1000;
	public final static long ONE_HOUR = 60 * 60 * 1000;

	public ScheduledTasks() {
		// TODO Auto-generated constructor stub
	}

	@Scheduled(fixedRate = 5000) //5秒运行一次调度任务
	public void echoCurrentTime() {
		log.info("The time is now {}", dateFormat.format(new Date()));
	}

	@Scheduled(fixedRate = ONE_DAY)
	public void scheduledTask() {
		System.out.println("每隔一天执行一次调度任务");
	}

	@Scheduled(fixedDelay = ONE_HOUR)
	public void scheduleTask2() {
		System.out.println("运行完后隔一小时就执行任务");
	}

	@Scheduled(initialDelay = 1000, fixedRate = 5000)
	public void doSomething() {
		// something that should execute periodically
	}

	@Scheduled(cron = "0 0/1 * * * ? ")
	public void ScheduledTask3() {
		System.out.println(" 每隔一分钟执行一次任务");
	}

}			

11.17.3. 查看日志

tail -f spring.log	

原文发布于微信公众号 - Netkiller(netkiller-ebook)

原文发表时间:2016-09-17

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏一个会写诗的程序员的博客

$ gradle packageDebug --stacktrace error: top-left corner pixel must be either opaque white or trans

$ gradle packageDebug --stacktrace error: top-left corner pixel must be either ...

1062
来自专栏运维

linux文件树

以前有意找这方面的资料,今天突然发现在系统中就有 linux系统用man hier solaris用man  filesystem 其结果如下     ...

892
来自专栏Golang语言社区

Go调试简单的内存泄漏

Memory leaks are a class of bugs where memory is not released even after it is n...

3373
来自专栏杨建荣的学习笔记

hotspare的copyback(r7笔记第30天)

最近做硬件巡检,发现一部分硬盘出现了坏块,同事就帮忙去协调处理这个事情,晚些时候接到了现场工程师的电话,问我可以不可以换,简单确认是raid5的盘。所以只能一个...

3415
来自专栏技术小黑屋

Package Stopped State Since Android 3.1

Since Android 3.1, Android has introduced a LaunchControl mechanism. It’s call S...

911
来自专栏JAVA技术站

SpringBoot整合mybatis 原

1075
来自专栏everhad

转载:Package by feature, not layer

The first question in building an application is "How do I divide it up into pac...

1120
来自专栏潇涧技术专栏

Tips about MacBook's battery health

内容摘自Mac上的一个灰常好的免费软件Battery Health中关于保养MacBook的电池寿命的建议,与大家一起分享

561
来自专栏程序员笔记

Note_Motivation & Gamification

2126
来自专栏黑泽君的专栏

启动tomcat服务器报错:ERROR ContextLoader:307 - Context initialization failed

  项目环境是Eclipse企业版4.7.2 + jdk1.8.0_161 + apache-tomcat-8.5.30

2102

扫码关注云+社区