Skip to content

Commit 7dbc34e

Browse files
committed
使用@async实现异步调用:使用Future
1 parent 4b0af6f commit 7dbc34e

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

Chapter4-1-5/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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-5</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter4-1-5</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.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<version>1.16.20</version>
42+
<scope>provided</scope>
43+
</dependency>
44+
45+
</dependencies>
46+
47+
<build>
48+
<plugins>
49+
<plugin>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-maven-plugin</artifactId>
52+
</plugin>
53+
</plugins>
54+
</build>
55+
56+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.ThreadPoolTaskExecutor;
9+
10+
import java.util.concurrent.Executor;
11+
import java.util.concurrent.ThreadPoolExecutor;
12+
13+
14+
@SpringBootApplication
15+
public class Application {
16+
17+
public static void main(String[] args) {
18+
SpringApplication.run(Application.class, args);
19+
}
20+
21+
@EnableAsync
22+
@Configuration
23+
class TaskPoolConfig {
24+
25+
@Bean("taskExecutor")
26+
public Executor taskExecutor() {
27+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
28+
executor.setCorePoolSize(10);
29+
executor.setMaxPoolSize(20);
30+
executor.setQueueCapacity(200);
31+
executor.setKeepAliveSeconds(60);
32+
executor.setThreadNamePrefix("taskExecutor-");
33+
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
34+
return executor;
35+
}
36+
}
37+
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.didispace.async;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.scheduling.annotation.Async;
5+
import org.springframework.scheduling.annotation.AsyncResult;
6+
import org.springframework.stereotype.Component;
7+
8+
import java.util.Random;
9+
import java.util.concurrent.Future;
10+
11+
/**
12+
* @author 程序猿DD
13+
* @version 1.0.0
14+
* @date 16/5/16 下午12:58.
15+
* @blog http://blog.didispace.com
16+
*/
17+
@Slf4j
18+
@Component
19+
public class Task {
20+
21+
public static Random random = new Random();
22+
23+
@Async("taskExecutor")
24+
public Future<String> run() throws Exception {
25+
long sleep = random.nextInt(10000);
26+
log.info("开始任务,需耗时:" + sleep + "毫秒");
27+
Thread.sleep(sleep);
28+
log.info("完成任务");
29+
return new AsyncResult<>("test");
30+
}
31+
32+
}

Chapter4-1-5/src/main/resources/application.properties

Whitespace-only changes.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.didispace;
2+
3+
import com.didispace.async.Task;
4+
import lombok.extern.slf4j.Slf4j;
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+
import java.util.concurrent.Future;
12+
import java.util.concurrent.TimeUnit;
13+
14+
@Slf4j
15+
@RunWith(SpringJUnit4ClassRunner.class)
16+
@SpringBootTest
17+
public class ApplicationTests {
18+
19+
@Autowired
20+
private Task task;
21+
22+
@Test
23+
public void test() throws Exception {
24+
Future<String> futureResult = task.run();
25+
String result = futureResult.get(5, TimeUnit.SECONDS);
26+
log.info(result);
27+
}
28+
29+
}

0 commit comments

Comments
 (0)