@@ -31,6 +31,7 @@ | |||||
<module>../spring-boot-demo-util</module> | <module>../spring-boot-demo-util</module> | ||||
<module>../spring-boot-demo-elasticsearch</module> | <module>../spring-boot-demo-elasticsearch</module> | ||||
<module>../spring-boot-demo-template-beetl</module> | <module>../spring-boot-demo-template-beetl</module> | ||||
<module>../spring-boot-demo-task-schedule</module> | |||||
</modules> | </modules> | ||||
<parent> | <parent> | ||||
@@ -0,0 +1,84 @@ | |||||
# spring-boot-demo-task-schedule | |||||
依赖 [spring-boot-demo-parent](../spring-boot-demo-parent) | |||||
## TaskConfig.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.springbootdemotaskschedule.config | |||||
* @description: 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/5/8 上午10:01 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Configuration | |||||
@EnableScheduling | |||||
@ComponentScan(basePackages = "com.xkcoding.springbootdemotaskschedule.task") | |||||
public class TaskConfig implements SchedulingConfigurer { | |||||
@Override | |||||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | |||||
taskRegistrar.setScheduler(taskExecutor()); | |||||
} | |||||
@Bean | |||||
public Executor taskExecutor() { | |||||
return Executors.newScheduledThreadPool(100); | |||||
} | |||||
} | |||||
``` | |||||
## TaskJob.java | |||||
```java | |||||
/** | |||||
* <p> | |||||
* 定时任务 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.springbootdemotaskschedule.task | |||||
* @description: 定时任务 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/5/8 上午10:05 | |||||
* @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,24 @@ | |||||
<?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-schedule</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<packaging>jar</packaging> | |||||
<name>spring-boot-demo-task-schedule</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo-parent</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<relativePath>../spring-boot-demo-parent/pom.xml</relativePath> | |||||
</parent> | |||||
<build> | |||||
<finalName>spring-boot-demo-task-schedule</finalName> | |||||
</build> | |||||
</project> |
@@ -0,0 +1,12 @@ | |||||
package com.xkcoding.springbootdemotaskschedule; | |||||
import org.springframework.boot.SpringApplication; | |||||
import org.springframework.boot.autoconfigure.SpringBootApplication; | |||||
@SpringBootApplication | |||||
public class SpringBootDemoTaskScheduleApplication { | |||||
public static void main(String[] args) { | |||||
SpringApplication.run(SpringBootDemoTaskScheduleApplication.class, args); | |||||
} | |||||
} |
@@ -0,0 +1,41 @@ | |||||
package com.xkcoding.springbootdemotaskschedule.config; | |||||
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.Executors; | |||||
/** | |||||
* <p> | |||||
* 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 | |||||
* </p> | |||||
* | |||||
* @package: com.xkcoding.springbootdemotaskschedule.config | |||||
* @description: 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/5/8 上午10:01 | |||||
* @copyright: Copyright (c) 2018 | |||||
* @version: V1.0 | |||||
* @modified: yangkai.shen | |||||
*/ | |||||
@Configuration | |||||
@EnableScheduling | |||||
@ComponentScan(basePackages = "com.xkcoding.springbootdemotaskschedule.task") | |||||
public class TaskConfig implements SchedulingConfigurer { | |||||
@Override | |||||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | |||||
taskRegistrar.setScheduler(taskExecutor()); | |||||
} | |||||
@Bean | |||||
public Executor taskExecutor() { | |||||
return Executors.newScheduledThreadPool(100); | |||||
} | |||||
} |
@@ -0,0 +1,50 @@ | |||||
package com.xkcoding.springbootdemotaskschedule.task; | |||||
import com.xiaoleilu.hutool.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.springbootdemotaskschedule.task | |||||
* @description: 定时任务 | |||||
* @author: yangkai.shen | |||||
* @date: Created in 2018/5/8 上午10:05 | |||||
* @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,16 @@ | |||||
package com.xkcoding.springbootdemotaskschedule; | |||||
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 SpringBootDemoTaskScheduleApplicationTests { | |||||
@Test | |||||
public void contextLoads() { | |||||
} | |||||
} |