diff --git a/README.md b/README.md index 0abf99114..7c2a4b399 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,8 @@ Spring Boot 使用的各种示例,以最简单、最实用为标准,此开 - [spring-boot-thymeleaf](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-thymeleaf):Spring Boot 3.0 Thymeleaf 语法、布局使用示例 - [spring-boot-jpa](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-jpa):Spring Boot 3.0 Jpa 操作、多数据源使用示例 - [spring-boot-mybatis](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis):Spring Boot 3.0 Mybatis 注解、xml 使用、多数据源使用示例 -- [spring-boot-rabbitmq](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-rabbitmq):Spring Boot 3.0 RabbitMQ 各种常见场景使用示例 - +- [spring-boot-rabbitmq](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-rabbitmq):Spring Boot 3.0 RabbitMQ 各种常见场景使用示例 +- [spring-boot-scheduler](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-scheduler):Spring Boot 3.0 定时任务 scheduler 使用示例 --- > 如果大家想了解关于 Spring Boot 的其它方面应用,也可以以[issues](https://github.com/ityouknow/spring-boot-examples/issues)的形式反馈给我,我后续来完善。 diff --git a/spring-boot-rabbitmq/src/main/resources/application.properties b/spring-boot-rabbitmq/src/main/resources/application.properties index a4c9b46d0..dcaf54273 100644 --- a/spring-boot-rabbitmq/src/main/resources/application.properties +++ b/spring-boot-rabbitmq/src/main/resources/application.properties @@ -1,6 +1,6 @@ spring.application.name=spring-boot-rabbitmq -spring.rabbitmq.host=119.91.142.6 +spring.rabbitmq.host=192.0.0.6 spring.rabbitmq.port=5672 spring.rabbitmq.username=admin spring.rabbitmq.password=admin diff --git a/spring-boot-scheduler/pom.xml b/spring-boot-scheduler/pom.xml new file mode 100644 index 000000000..e956d21ab --- /dev/null +++ b/spring-boot-scheduler/pom.xml @@ -0,0 +1,58 @@ + + + 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 + 3.0.0 + + + + + UTF-8 + 17 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-test + + + org.junit.vintage + junit-vintage-engine + test + + + org.hamcrest + hamcrest-core + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-boot-scheduler/src/main/java/com/neo/SchedulerApplication.java b/spring-boot-scheduler/src/main/java/com/neo/SchedulerApplication.java new file mode 100644 index 000000000..1fd012d24 --- /dev/null +++ b/spring-boot-scheduler/src/main/java/com/neo/SchedulerApplication.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 SchedulerApplication { + + public static void main(String[] args) { + SpringApplication.run(SchedulerApplication.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 000000000..7b59c9f2b --- /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 000000000..69885e3cc --- /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 000000000..8978a46f1 --- /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/SchedulerApplicationTests.java b/spring-boot-scheduler/src/test/java/com/neo/SchedulerApplicationTests.java new file mode 100644 index 000000000..9a627ba57 --- /dev/null +++ b/spring-boot-scheduler/src/test/java/com/neo/SchedulerApplicationTests.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 SchedulerApplicationTests { + + @Test + public void contextLoads() { + System.out.println("hello world"); + } + +}