Skip to content

Commit 1b70def

Browse files
committed
add async examples
1 parent 6913c82 commit 1b70def

File tree

24 files changed

+779
-83
lines changed

24 files changed

+779
-83
lines changed

api-gateway/pom.xml

Lines changed: 0 additions & 27 deletions
This file was deleted.

api-gateway/src/main/resources/application.yml

Lines changed: 0 additions & 32 deletions
This file was deleted.

demo-jersey-starter/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<dependencies>
1515
<dependency>
16-
<groupId>cn.keets</groupId>
16+
<groupId>com.blueskykong</groupId>
1717
<artifactId>jersey-starter-swagger</artifactId>
1818
<version>1.0.0.RELEASE</version>
1919
</dependency>

demo-jersey-starter/src/main/java/com/blueskykong/starter/jersey/DemoApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.blueskykong.starter.jersey;
22

3-
import cn.keets.swagger.EnableSwagger2Doc;
3+
import com.blueskykong.swagger.EnableSwagger2Doc;
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66
import org.springframework.boot.SpringApplication;

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
<module>demo-jersey-starter</module>
2121
<module>zipkin-server-stream</module>
2222
<module>config-client</module>
23-
<module>api-gateway</module>
23+
<module>zuul-gateway</module>
2424
<module>config-server</module>
2525
<module>auth-backend</module>
26+
<module>spring-boot-async</module>
2627
</modules>
2728

2829
<properties>

spring-boot-async/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>1.5.2.RELEASE</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<groupId>com.blueskykong</groupId>
14+
15+
<artifactId>spring-boot-async</artifactId>
16+
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter</artifactId>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-test</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.projectlombok</groupId>
31+
<artifactId>lombok</artifactId>
32+
<version>1.16.20</version>
33+
<scope>provided</scope>
34+
</dependency>
35+
36+
<!-- <dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-data-redis</artifactId>
39+
</dependency>-->
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.blueskykong.samples.async;
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+
@SpringBootApplication
14+
@EnableAsync
15+
public class AsyncApplication {
16+
public static void main(String[] args) {
17+
SpringApplication.run(AsyncApplication.class, args);
18+
}
19+
20+
@Configuration
21+
class TaskPoolConfig {
22+
23+
@Bean("taskExecutor")
24+
public Executor taskExecutor() {
25+
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
26+
executor.setCorePoolSize(10);
27+
executor.setMaxPoolSize(20);
28+
executor.setQueueCapacity(200);
29+
executor.setKeepAliveSeconds(60);
30+
executor.setThreadNamePrefix("taskExecutor-");
31+
executor.setWaitForTasksToCompleteOnShutdown(true);
32+
executor.setAwaitTerminationSeconds(60);
33+
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
34+
return executor;
35+
}
36+
}
37+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.blueskykong.samples.async.service;
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 keets
13+
* @data 2018/4/4.
14+
*/
15+
@Component
16+
@Slf4j
17+
public class TaskService {
18+
19+
@Async("taskExecutor")
20+
public Future<String> doTaskOne() throws Exception {
21+
log.info("开始做任务一");
22+
long start = System.currentTimeMillis();
23+
Thread.sleep((long) (Math.random() * 1000));
24+
long end = System.currentTimeMillis();
25+
log.info("完成任务一,耗时:" + (end - start) + "毫秒");
26+
return new AsyncResult<>("任务一完成");
27+
}
28+
29+
@Async("taskExecutor")
30+
public Future<String> doTaskTwo() throws Exception {
31+
log.info("开始做任务二");
32+
long start = System.currentTimeMillis();
33+
Thread.sleep((long) (Math.random() * 10000));
34+
long end = System.currentTimeMillis();
35+
log.info("完成任务二,耗时:" + (end - start) + "毫秒");
36+
return new AsyncResult<>("任务二完成");
37+
}
38+
39+
@Async("taskExecutor")
40+
public Future<String> doTaskThree() throws Exception {
41+
log.info("开始做任务三");
42+
long start = System.currentTimeMillis();
43+
Thread.sleep((long) (Math.random() * 10000));
44+
long end = System.currentTimeMillis();
45+
log.info("完成任务三,耗时:" + (end - start) + "毫秒");
46+
return new AsyncResult<>("任务三完成");
47+
}
48+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.redis.pool:
2+
max-wait: 5000
3+
max-active: 10
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.blueskykong.samples.async;
2+
3+
import com.blueskykong.samples.async.service.TaskService;
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.SpringApplication;
9+
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
import java.util.concurrent.Future;
14+
import java.util.concurrent.TimeUnit;
15+
16+
@RunWith(SpringRunner.class)
17+
@SpringBootTest
18+
@Slf4j
19+
public class AsyncApplicationTest {
20+
21+
@Autowired
22+
private TaskService taskService;
23+
24+
@Test
25+
public void test() throws Exception {
26+
27+
long start = System.currentTimeMillis();
28+
29+
Thread t1 = new Thread(() ->callThread());
30+
t1.start();
31+
32+
long end = System.currentTimeMillis();
33+
log.info(Thread.currentThread().getName());
34+
t1.join();
35+
36+
log.info("任务全部完成,总耗时:" + (end - start) + "毫秒");
37+
38+
}
39+
40+
private void callThread() {
41+
try {
42+
long start = System.currentTimeMillis();
43+
44+
Future<String> task1 = taskService.doTaskOne();
45+
Future<String> task2 = taskService.doTaskTwo();
46+
Future<String> task3 = taskService.doTaskThree();
47+
48+
49+
while (true) {
50+
51+
if (task1.isDone() && task2.isDone() && task3.isDone()) {
52+
String a = task1.get(100000, TimeUnit.MILLISECONDS);
53+
log.info(a);
54+
log.info("任务一完成,总耗时:" + (System.currentTimeMillis() - start) + "毫秒");
55+
Thread.sleep(1000);
56+
57+
break;
58+
}
59+
// Thread.sleep(1000);
60+
}
61+
} catch (Exception e) {
62+
63+
}
64+
65+
}
66+
67+
}

0 commit comments

Comments
 (0)