Skip to content

Commit 490ea42

Browse files
committed
add:完成基于 @scheduled 注解的定时任务模块
1 parent 921cb9b commit 490ea42

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

spring-boot-task/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>JavaStudy</artifactId>
7+
<groupId>com.chachae</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>spring-boot-task</artifactId>
13+
14+
<dependencies>
15+
16+
<!-- joda-time -->
17+
<dependency>
18+
<groupId>joda-time</groupId>
19+
<artifactId>joda-time</artifactId>
20+
<version>${joda.time.version}</version>
21+
</dependency>
22+
23+
</dependencies>
24+
25+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.chachae.task;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
6+
7+
/**
8+
* @author chachae
9+
* @since 2020/1/11 17:26
10+
*/
11+
@SpringBootApplication
12+
@EnableScheduling
13+
public class TaskApplication {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(TaskApplication.class);
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.chachae.task.job;
2+
3+
import com.chachae.task.util.DateUtil;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.scheduling.annotation.Scheduled;
6+
import org.springframework.stereotype.Component;
7+
8+
/**
9+
* @author chachae
10+
* @since 2020/1/11 20:19
11+
*/
12+
@Slf4j
13+
@Component
14+
public class TaskJob {
15+
16+
/** 每隔20s 执行一次 */
17+
@Scheduled(cron = "0/20 * * * * ?")
18+
public void taskOne() {
19+
log.info("【task 1】 : {}", DateUtil.now());
20+
}
21+
22+
/** 从启动时间开始,间隔 5s 执行 固定间隔时间 */
23+
@Scheduled(fixedRate = 5000)
24+
public void taskTwo() {
25+
log.info("【task 2】 : {}", DateUtil.now());
26+
}
27+
28+
/** 从启动时间开始,延迟 5s 后间隔 10s 执行 固定等待时间 */
29+
@Scheduled(fixedDelay = 10000, initialDelay = 5000)
30+
public void taskThree() {
31+
log.info("【task 3】 : {}", DateUtil.now());
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.chachae.task.util;
2+
3+
import org.joda.time.LocalDateTime;
4+
import org.joda.time.format.DateTimeFormat;
5+
import org.joda.time.format.DateTimeFormatter;
6+
7+
import java.util.Date;
8+
9+
/**
10+
* 日期工具类
11+
*
12+
* @author chachae
13+
* @date 2019/12/17 10:46
14+
*/
15+
public class DateUtil {
16+
17+
private static final DateTimeFormatter FORMATTER =
18+
DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
19+
20+
public static String now() {
21+
return FORMATTER.print(LocalDateTime.now());
22+
}
23+
24+
public static Date nowDate() {
25+
return LocalDateTime.now().toDate();
26+
}
27+
28+
public static Date parse(String date) {
29+
return FORMATTER.parseDateTime(date).toDate();
30+
}
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spring:
2+
task:
3+
execution:
4+
thread-name-prefix: chachae-Thread-
5+
scheduling:
6+
pool:
7+
size: 20

0 commit comments

Comments
 (0)