@@ -0,0 +1,185 @@ | |||||
# spring-boot-demo-task | |||||
> 此 demo 主要演示了 Spring Boot 如何快速实现定时任务。 | |||||
## pom.xml | |||||
```xml | |||||
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |||||
<modelVersion>4.0.0</modelVersion> | |||||
<artifactId>spring-boot-demo-task</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>spring-boot-demo-task</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo</artifactId> | |||||
<version>1.0.0-SNAPSHOT</version> | |||||
</parent> | |||||
<properties> | |||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |||||
<java.version>1.8</java.version> | |||||
</properties> | |||||
<dependencies> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-web</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.apache.commons</groupId> | |||||
<artifactId>commons-lang3</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-starter-test</artifactId> | |||||
<scope>test</scope> | |||||
</dependency> | |||||
</dependencies> | |||||
<build> | |||||
<finalName>spring-boot-demo-task</finalName> | |||||
<plugins> | |||||
<plugin> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-maven-plugin</artifactId> | |||||
</plugin> | |||||
</plugins> | |||||
</build> | |||||
</project> | |||||
``` | |||||
## TaskConfig.java | |||||
> 此处等同于在配置文件配置 | |||||
> | |||||
> ```properties | |||||
> spring.task.scheduling.pool.size=20 | |||||
> spring.task.scheduling.thread-name-prefix=Job-Thread- | |||||
> ``` | |||||
```java | |||||
/** | |||||
* <p> | |||||
* 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.task.config | |||||
* @description: 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/22 19:02 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Configuration | |||||
@EnableScheduling | |||||
@ComponentScan(basePackages = {"com.xkcoding.task.job"}) | |||||
public class TaskConfig implements SchedulingConfigurer { | |||||
@Override | |||||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | |||||
taskRegistrar.setScheduler(taskExecutor()); | |||||
} | |||||
/** | |||||
* 这里等同于配置文件配置 | |||||
* {@code spring.task.scheduling.pool.size=20} - Maximum allowed number of threads. | |||||
* {@code spring.task.scheduling.thread-name-prefix=Job-Thread- } - Prefix to use for the names of newly created threads. | |||||
* {@link org.springframework.boot.autoconfigure.task.TaskSchedulingProperties} | |||||
*/ | |||||
@Bean | |||||
public Executor taskExecutor() { | |||||
return new ScheduledThreadPoolExecutor(20, new BasicThreadFactory.Builder().namingPattern("Job-Thread-%d").build()); | |||||
} | |||||
} | |||||
``` | |||||
## TaskJob.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* 定时任务 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.task.job | |||||
* @description: 定时任务 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/22 19:09 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Component | |||||
@Slf4j | |||||
public class TaskJob { | |||||
/** | |||||
* 按照标准时间来算,每隔 10s 执行一次 | |||||
*/ | |||||
@Scheduled(cron = "0/10 * * * * ?") | |||||
public void job1() { | |||||
log.info("【job1】开始执行:{}", DateUtil.formatDateTime(new Date())); | |||||
} | |||||
/** | |||||
* 从启动时间开始,间隔 2s 执行 | |||||
* 固定间隔时间 | |||||
*/ | |||||
@Scheduled(fixedRate = 2000) | |||||
public void job2() { | |||||
log.info("【job2】开始执行:{}", DateUtil.formatDateTime(new Date())); | |||||
} | |||||
/** | |||||
* 从启动时间开始,延迟 5s 后间隔 4s 执行 | |||||
* 固定等待时间 | |||||
*/ | |||||
@Scheduled(fixedDelay = 4000, initialDelay = 5000) | |||||
public void job3() { | |||||
log.info("【job3】开始执行:{}", DateUtil.formatDateTime(new Date())); | |||||
} | |||||
} | |||||
``` | |||||
## application.yml | |||||
```yaml | |||||
server: | |||||
port: 8080 | |||||
servlet: | |||||
context-path: /demo | |||||
# 下面的配置等同于 TaskConfig | |||||
#spring: | |||||
# task: | |||||
# scheduling: | |||||
# pool: | |||||
# size: 20 | |||||
# thread-name-prefix: Job-Thread- | |||||
``` | |||||
## 参考 | |||||
- Spring Boot官方文档:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-task-execution-scheduling |
@@ -25,7 +25,23 @@ | |||||
<dependencies> | <dependencies> | ||||
<dependency> | <dependency> | ||||
<groupId>org.springframework.boot</groupId> | <groupId>org.springframework.boot</groupId> | ||||
<artifactId>spring-boot-starter</artifactId> | |||||
<artifactId>spring-boot-starter-web</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.apache.commons</groupId> | |||||
<artifactId>commons-lang3</artifactId> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>cn.hutool</groupId> | |||||
<artifactId>hutool-all</artifactId> | |||||
</dependency> | </dependency> | ||||
<dependency> | <dependency> | ||||
@@ -3,6 +3,19 @@ package com.xkcoding.task; | |||||
import org.springframework.boot.SpringApplication; | import org.springframework.boot.SpringApplication; | ||||
import org.springframework.boot.autoconfigure.SpringBootApplication; | import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
/** | |||||
* <p> | |||||
* 启动器 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.task | |||||
* @description: 启动器 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/22 19:00 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@SpringBootApplication | @SpringBootApplication | ||||
public class SpringBootDemoTaskApplication { | public class SpringBootDemoTaskApplication { | ||||
@@ -0,0 +1,46 @@ | |||||
package com.xkcoding.task.config; | |||||
import org.apache.commons.lang3.concurrent.BasicThreadFactory; | |||||
import org.springframework.context.annotation.Bean; | |||||
import org.springframework.context.annotation.ComponentScan; | |||||
import org.springframework.context.annotation.Configuration; | |||||
import org.springframework.scheduling.annotation.EnableScheduling; | |||||
import org.springframework.scheduling.annotation.SchedulingConfigurer; | |||||
import org.springframework.scheduling.config.ScheduledTaskRegistrar; | |||||
import java.util.concurrent.Executor; | |||||
import java.util.concurrent.ScheduledThreadPoolExecutor; | |||||
/** | |||||
* <p> | |||||
* 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.task.config | |||||
* @description: 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/22 19:02 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Configuration | |||||
@EnableScheduling | |||||
@ComponentScan(basePackages = {"com.xkcoding.task.job"}) | |||||
public class TaskConfig implements SchedulingConfigurer { | |||||
@Override | |||||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | |||||
taskRegistrar.setScheduler(taskExecutor()); | |||||
} | |||||
/** | |||||
* 这里等同于配置文件配置 | |||||
* {@code spring.task.scheduling.pool.size=20} - Maximum allowed number of threads. | |||||
* {@code spring.task.scheduling.thread-name-prefix=Job-Thread- } - Prefix to use for the names of newly created threads. | |||||
* {@link org.springframework.boot.autoconfigure.task.TaskSchedulingProperties} | |||||
*/ | |||||
@Bean | |||||
public Executor taskExecutor() { | |||||
return new ScheduledThreadPoolExecutor(20, new BasicThreadFactory.Builder().namingPattern("Job-Thread-%d").build()); | |||||
} | |||||
} |
@@ -0,0 +1,52 @@ | |||||
package com.xkcoding.task.job; | |||||
import cn.hutool.core.date.DateUtil; | |||||
import lombok.extern.slf4j.Slf4j; | |||||
import org.springframework.scheduling.annotation.Scheduled; | |||||
import org.springframework.stereotype.Component; | |||||
import java.util.Date; | |||||
/** | |||||
* <p> | |||||
* 定时任务 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.task.job | |||||
* @description: 定时任务 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/11/22 19:09 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Component | |||||
@Slf4j | |||||
public class TaskJob { | |||||
/** | |||||
* 按照标准时间来算,每隔 10s 执行一次 | |||||
*/ | |||||
@Scheduled(cron = "0/10 * * * * ?") | |||||
public void job1() { | |||||
log.info("【job1】开始执行:{}", DateUtil.formatDateTime(new Date())); | |||||
} | |||||
/** | |||||
* 从启动时间开始,间隔 2s 执行 | |||||
* 固定间隔时间 | |||||
*/ | |||||
@Scheduled(fixedRate = 2000) | |||||
public void job2() { | |||||
log.info("【job2】开始执行:{}", DateUtil.formatDateTime(new Date())); | |||||
} | |||||
/** | |||||
* 从启动时间开始,延迟 5s 后间隔 4s 执行 | |||||
* 固定等待时间 | |||||
*/ | |||||
@Scheduled(fixedDelay = 4000, initialDelay = 5000) | |||||
public void job3() { | |||||
log.info("【job3】开始执行:{}", DateUtil.formatDateTime(new Date())); | |||||
} | |||||
} |
@@ -0,0 +1,11 @@ | |||||
server: | |||||
port: 8080 | |||||
servlet: | |||||
context-path: /demo | |||||
# 下面的配置等同于 TaskConfig | |||||
#spring: | |||||
# task: | |||||
# scheduling: | |||||
# pool: | |||||
# size: 20 | |||||
# thread-name-prefix: Job-Thread- |