-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Spring Boot Async | ||
|
||
## 概述 | ||
|
||
Spring Boot Async 概述... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.6.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<groupId>name.guolanren</groupId> | ||
<artifactId>async</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<properties> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package name.guolanren; | ||
|
||
import name.guolanren.service.AsyncService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@SpringBootApplication | ||
public class AsyncApplication implements CommandLineRunner { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(AsyncApplication.class); | ||
|
||
@Autowired | ||
private AsyncService asyncService; | ||
|
||
private CompletableFuture<String>[] futures = new CompletableFuture[3]; | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(AsyncApplication.class, args); | ||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
long start = System.currentTimeMillis(); | ||
|
||
futures[0] = asyncService.nameToUpperCase("guolanren"); | ||
futures[1] = asyncService.nameToUpperCase("guoyaozhan"); | ||
futures[2] = asyncService.nameToUpperCase("genius"); | ||
|
||
CompletableFuture.allOf(futures).join(); | ||
|
||
logger.info("Elapsed time: " + (System.currentTimeMillis() - start)); | ||
logger.info(futures[0].get()); | ||
logger.info(futures[1].get()); | ||
logger.info(futures[2].get()); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
async/src/main/java/name/guolanren/config/AsyncConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package name.guolanren.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.EnableAsync; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; | ||
|
||
import java.util.concurrent.Executor; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@Configuration | ||
@EnableAsync | ||
public class AsyncConfiguration { | ||
|
||
@Bean("asyncExecutor") | ||
public Executor asyncExecutor() { | ||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); | ||
executor.setCorePoolSize(3); | ||
executor.setMaxPoolSize(3); | ||
executor.setQueueCapacity(500); | ||
executor.setThreadNamePrefix("NameToUpperCase-"); | ||
executor.initialize(); | ||
return executor; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
async/src/main/java/name/guolanren/service/AsyncService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package name.guolanren.service; | ||
|
||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author guolanren | ||
*/ | ||
@Service | ||
public class AsyncService { | ||
|
||
@Async("asyncExecutor") | ||
public CompletableFuture<String> nameToUpperCase(String name) { | ||
try { | ||
TimeUnit.SECONDS.sleep(10); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
return CompletableFuture.completedFuture(Thread.currentThread().getName() + ": " + name.toUpperCase()); | ||
} | ||
|
||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters