From cd054025d837ba7d5a58abbd6c0ced376f421ca7 Mon Sep 17 00:00:00 2001 From: ityouknow Date: Thu, 1 Dec 2016 18:08:39 +0800 Subject: [PATCH] add scheduler --- README.md | 18 +++--- spring-boot-scheduler/pom.xml | 56 +++++++++++++++++++ .../src/main/java/com/neo/Application.java | 14 +++++ .../java/com/neo/task/Scheduler2Task.java | 23 ++++++++ .../main/java/com/neo/task/SchedulerTask.java | 22 ++++++++ .../src/main/resources/application.properties | 2 + .../test/java/com/neo/ApplicationTests.java | 17 ++++++ 7 files changed, 144 insertions(+), 8 deletions(-) create mode 100644 spring-boot-scheduler/pom.xml create mode 100644 spring-boot-scheduler/src/main/java/com/neo/Application.java create mode 100644 spring-boot-scheduler/src/main/java/com/neo/task/Scheduler2Task.java create mode 100644 spring-boot-scheduler/src/main/java/com/neo/task/SchedulerTask.java create mode 100644 spring-boot-scheduler/src/main/resources/application.properties create mode 100644 spring-boot-scheduler/src/test/java/com/neo/ApplicationTests.java diff --git a/README.md b/README.md index 68f283b..74b92f9 100644 --- a/README.md +++ b/README.md @@ -12,18 +12,20 @@ Spring boot使用的各种示例,以最简单、最实用为标准 - [spring-boot-mybaits-xml](https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-mybatis-xml):xml配置版本 -- [spring-boot-rabbitmq](https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-rabbitmq):spring boot和rabbitmq简单应用 - +- [spring-boot-rabbitmq](https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-rabbitmq):spring boot和rabbitmq各种消息应用案例 +- [spring-boot-rabbitmq](https://github.com/ityouknow/spring-boot-starter/tree/master/spring-boot-scheduler):spring boot和定时任务案例 文章可以参考这里 -- [构建微服务:Spring boot 入门篇](http://www.cnblogs.com/ityouknow/p/5662753.html) -- [构建微服务:Spring boot 提高篇](http://www.ityouknow.com/spring%20boot/2016/02/03/%E6%9E%84%E5%BB%BA%E5%BE%AE%E6%9C%8D%E5%8A%A1-Spring-boot-%E6%8F%90%E9%AB%98%E7%AF%87.html) -- [构建微服务:Spring Boot中Redis的使用](http://www.ityouknow.com/spring%20boot/2016/03/06/%E6%9E%84%E5%BB%BA%E5%BE%AE%E6%9C%8D%E5%8A%A1-Spring-Boot%E4%B8%ADRedis%E7%9A%84%E4%BD%BF%E7%94%A8.html) -- [构建微服务:thymeleaf使用详解](http://www.ityouknow.com/thymeleaf/2016/05/01/%E6%9E%84%E5%BB%BA%E5%BE%AE%E6%9C%8D%E5%8A%A1-thymeleaf%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3.html) -- [构建微服务:spring data jpa的使用](http://www.ityouknow.com/spring%20data%20jpa/2016/08/20/%E6%9E%84%E5%BB%BA%E5%BE%AE%E6%9C%8D%E5%8A%A1-spring-data-jpa%E7%9A%84%E4%BD%BF%E7%94%A8.html) -- [构建微服务:如何优雅的使用mybatis](http://www.ityouknow.com/mybatis/2016/11/06/%E6%9E%84%E5%BB%BA%E5%BE%AE%E6%9C%8D%E5%8A%A1-%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E4%BD%BF%E7%94%A8mybatis.html) +- [spring boot(一):入门篇](http://www.cnblogs.com/ityouknow/p/5662753.html) +- [springboot(二):web综合开发](http://www.ityouknow.com/springboot/2016/02/03/springboot(%E4%BA%8C)-web%E7%BB%BC%E5%90%88%E5%BC%80%E5%8F%91.html) +- [springboot(三):Spring boot中Redis的使用](http://www.ityouknow.com/springboot/2016/03/06/springboot(%E4%B8%89)-Spring-Boot%E4%B8%ADRedis%E7%9A%84%E4%BD%BF%E7%94%A8.html) +- [springboot(四):thymeleaf使用详解](http://www.ityouknow.com/thymeleaf/2016/05/01/springboot(%E5%9B%9B)-thymeleaf%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3.html) +- [springboot(五):spring data jpa的使用](http://www.ityouknow.com/springboot/2016/08/20/springboot(%E4%BA%94)-spring-data-jpa%E7%9A%84%E4%BD%BF%E7%94%A8.html) +- [springboot(六):如何优雅的使用mybatis](http://www.ityouknow.com/springboot/2016/11/06/springboot(%E5%85%AD)-%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E4%BD%BF%E7%94%A8mybatis.html) +- [springboot(七):springboot+mybatis多数据源最简解决方案](http://www.ityouknow.com/springboot/2016/11/25/springboot(%E4%B8%83)-springboot+mybatis%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E6%9C%80%E7%AE%80%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88.html) +- [springboot(八):RabbitMQ详解](http://www.ityouknow.com/springboot/2016/11/30/springboot(%E5%85%AB)-RabbitMQ%E8%AF%A6%E8%A7%A3.html) diff --git a/spring-boot-scheduler/pom.xml b/spring-boot-scheduler/pom.xml new file mode 100644 index 0000000..b3d1836 --- /dev/null +++ b/spring-boot-scheduler/pom.xml @@ -0,0 +1,56 @@ + + + 4.0.0 + + com.neo + spring-boot-scheduler + 1.0.0 + jar + + spring-boot-scheduler + Demo project for Spring Boot and scheduler + + + org.springframework.boot + spring-boot-starter-parent + 1.4.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-devtools + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + + + diff --git a/spring-boot-scheduler/src/main/java/com/neo/Application.java b/spring-boot-scheduler/src/main/java/com/neo/Application.java new file mode 100644 index 0000000..f260f3d --- /dev/null +++ b/spring-boot-scheduler/src/main/java/com/neo/Application.java @@ -0,0 +1,14 @@ +package com.neo; + +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); + } +} diff --git a/spring-boot-scheduler/src/main/java/com/neo/task/Scheduler2Task.java b/spring-boot-scheduler/src/main/java/com/neo/task/Scheduler2Task.java new file mode 100644 index 0000000..7b59c9f --- /dev/null +++ b/spring-boot-scheduler/src/main/java/com/neo/task/Scheduler2Task.java @@ -0,0 +1,23 @@ +package com.neo.task; + +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * Created by summer on 2016/12/1. + */ + +@Component +public class Scheduler2Task { + + private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); + + @Scheduled(fixedRate = 6000) + public void reportCurrentTime() { + System.out.println("现在时间:" + dateFormat.format(new Date())); + } + +} diff --git a/spring-boot-scheduler/src/main/java/com/neo/task/SchedulerTask.java b/spring-boot-scheduler/src/main/java/com/neo/task/SchedulerTask.java new file mode 100644 index 0000000..69885e3 --- /dev/null +++ b/spring-boot-scheduler/src/main/java/com/neo/task/SchedulerTask.java @@ -0,0 +1,22 @@ +package com.neo.task; + +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.Date; + +/** + * Created by summer on 2016/12/1. + */ + +@Component +public class SchedulerTask { + + private int count=0; + + @Scheduled(cron="*/6 * * * * ?") + private void process(){ + System.out.println("this is scheduler task runing "+(count++)); + } + +} diff --git a/spring-boot-scheduler/src/main/resources/application.properties b/spring-boot-scheduler/src/main/resources/application.properties new file mode 100644 index 0000000..8978a46 --- /dev/null +++ b/spring-boot-scheduler/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.application.name=spirng-boot-scheduler + diff --git a/spring-boot-scheduler/src/test/java/com/neo/ApplicationTests.java b/spring-boot-scheduler/src/test/java/com/neo/ApplicationTests.java new file mode 100644 index 0000000..569db81 --- /dev/null +++ b/spring-boot-scheduler/src/test/java/com/neo/ApplicationTests.java @@ -0,0 +1,17 @@ +package com.neo; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ApplicationTests { + + @Test + public void contextLoads() { + System.out.println("hello world"); + } + +}