## 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> <the artifactId> Boot-Spring-Maven-plugin </ the artifactId> </plugin> </ plugins> </ Build> </ Project> `` ` ## TaskConfig.java > here is equivalent to the configuration in the configuration file > >` `` Properties > spring.task.scheduling.pool.size=20 > spring.task.scheduling.thread-name-prefix=Job-Thread- > ``` ```java /** * <p> * Timing task configuration, thread configuration Pool, use different threads to perform tasks, improve efficiency * </p> * * @package: com.xkcoding.task.config * @description: Timing task configuration, configure thread pool, use different threads to execute tasks, improve efficiency * @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. */ @Bean public Executor taskExecutor() { return new ScheduledThreadPoolExecutor(20, new BasicThreadFactory.Builder().namingPattern("Job-Thread-%d").build()); } } ``` ## TaskJob.java ```java /** * <p > * Timed task * </p> * * @package: com.xkcoding.task.job * @description: Timed task * @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 { /** * Calculated according to standard time, executed once every 10s */ @Scheduled (cron = "0/10 * * * * ?") public void job1() { log.info("[job1] start execution: {}", DateUtil.formatDateTime(new Date())); } /** * Execute from the start time at 2s intervals * fixed interval time */ @Scheduled(fixedRate = 2000) public void job2() { log.info("[job2] start execution: {}", DateUtil.formatDateTime(new Date())); } /** * From the start time, it will be executed after 5s delay and 4s interval * Fixed waiting time */ @Scheduled(fixedDelay = 4000, initialDelay = 5000) public void job3() { log.info("[job3] start execution: {}", DateUtil.formatDateTime(new Date())); } } ``` ## application.yml port: 8080 servlet: ```yaml server: context-path: /demo # The following configuration is equivalent to TaskConfig #spring: # task: # scheduling: # pool: # size: 20 # thread-name-prefix: Job-Thread- ``` ## Reference -Spring Boot official Document: https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-task-execution-scheduling Source code address: github: https://github. com/juejuedog/SpringBootDemo.git
How Spring Boot implements timing tasks quickly
Guess you like
Origin blog.csdn.net/A___B___C/article/details/107357453
Recommended
Ranking