Skip to content

Commit 4bf616e

Browse files
committed
使用@async实现异步调用:资源优雅关闭
1 parent fd635d9 commit 4bf616e

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed

Chapter4-1-4/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>Chapter4-1-4</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter4-1-4</name>
12+
<description>Spring Boot project</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.10.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-data-redis</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.projectlombok</groupId>
45+
<artifactId>lombok</artifactId>
46+
<version>1.16.20</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
</dependencies>
50+
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-maven-plugin</artifactId>
56+
</plugin>
57+
</plugins>
58+
</build>
59+
60+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.scheduling.annotation.EnableAsync;
8+
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
9+
10+
import java.util.concurrent.Executor;
11+
12+
@SpringBootApplication
13+
public class Application {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(Application.class, args);
17+
}
18+
19+
@EnableAsync
20+
@Configuration
21+
class TaskPoolConfig {
22+
23+
@Bean("taskExecutor")
24+
public Executor taskExecutor() {
25+
ThreadPoolTaskScheduler executor = new ThreadPoolTaskScheduler();
26+
executor.setPoolSize(20);
27+
executor.setThreadNamePrefix("taskExecutor-");
28+
29+
executor.setWaitForTasksToCompleteOnShutdown(true);
30+
executor.setAwaitTerminationSeconds(60);
31+
return executor;
32+
}
33+
34+
}
35+
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.didispace.async;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.data.redis.core.StringRedisTemplate;
6+
import org.springframework.scheduling.annotation.Async;
7+
import org.springframework.stereotype.Component;
8+
9+
/**
10+
* @author 程序猿DD
11+
* @version 1.0.0
12+
* @date 16/5/16 下午12:58.
13+
* @blog http://blog.didispace.com
14+
*/
15+
@Slf4j
16+
@Component
17+
public class Task {
18+
19+
@Autowired
20+
private StringRedisTemplate stringRedisTemplate;
21+
22+
@Async("taskExecutor")
23+
public void doTaskOne() throws Exception {
24+
log.info("开始做任务一");
25+
long start = System.currentTimeMillis();
26+
log.info(stringRedisTemplate.randomKey());
27+
long end = System.currentTimeMillis();
28+
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
29+
}
30+
31+
@Async("taskExecutor")
32+
public void doTaskTwo() throws Exception {
33+
log.info("开始做任务二");
34+
long start = System.currentTimeMillis();
35+
log.info(stringRedisTemplate.randomKey());
36+
long end = System.currentTimeMillis();
37+
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
38+
}
39+
40+
@Async("taskExecutor")
41+
public void doTaskThree() throws Exception {
42+
log.info("开始做任务三");
43+
long start = System.currentTimeMillis();
44+
log.info(stringRedisTemplate.randomKey());
45+
long end = System.currentTimeMillis();
46+
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
47+
}
48+
49+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
spring.redis.pool.max-wait=5000
2+
spring.redis.pool.max-active=10
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.didispace;
2+
3+
import com.didispace.async.Task;
4+
import lombok.SneakyThrows;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
11+
12+
@RunWith(SpringJUnit4ClassRunner.class)
13+
@SpringBootTest
14+
public class ApplicationTests {
15+
16+
@Autowired
17+
private Task task;
18+
19+
@Test
20+
@SneakyThrows
21+
public void test() {
22+
23+
for (int i = 0; i < 10000; i++) {
24+
task.doTaskOne();
25+
task.doTaskTwo();
26+
task.doTaskThree();
27+
28+
if (i == 9999) {
29+
System.exit(0);
30+
}
31+
}
32+
}
33+
34+
}

0 commit comments

Comments
 (0)