Skip to content

Commit

Permalink
async模块
Browse files Browse the repository at this point in the history
guolanren committed Apr 13, 2020
1 parent 5638634 commit 8ab6bf9
Showing 7 changed files with 149 additions and 1 deletion.
5 changes: 5 additions & 0 deletions async/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Spring Boot Async

## 概述

Spring Boot Async 概述...
43 changes: 43 additions & 0 deletions async/pom.xml
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>
47 changes: 47 additions & 0 deletions async/src/main/java/name/guolanren/AsyncApplication.java
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 async/src/main/java/name/guolanren/config/AsyncConfiguration.java
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 async/src/main/java/name/guolanren/service/AsyncService.java
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.
Original file line number Diff line number Diff line change
@@ -70,6 +70,6 @@ public void printFixedRate() throws InterruptedException {
@Scheduled(cron = "*/10 * * * * *")
})
public void printSchedules() {
System.out.printf("print schedules at %s...%n", new Date());
System.out.printf("%s print schedules at %s...%n", Thread.currentThread().getName(), new Date());
}
}

0 comments on commit 8ab6bf9

Please sign in to comment.