From c6feec9ba66f66998be3680b6334821f135a646b Mon Sep 17 00:00:00 2001
From: mrbird <852252810@qq.com>
Date: Wed, 13 Mar 2019 18:30:38 +0800
Subject: [PATCH] =?UTF-8?q?Spring=20Boot=20=E4=B8=AD=E7=9A=84=E5=BC=82?=
=?UTF-8?q?=E6=AD=A5=E8=B0=83=E7=94=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
49.Spring-Boot-Async/pom.xml | 43 +++++++++++++++
.../com/example/demo/DemoApplication.java | 14 +++++
.../example/demo/config/AsyncPoolConfig.java | 31 +++++++++++
.../demo/controller/TestController.java | 52 +++++++++++++++++++
.../com/example/demo/service/TestService.java | 39 ++++++++++++++
.../src/main/resources/application.properties | 1 +
.../example/demo/DemoApplicationTests.java | 16 ++++++
7 files changed, 196 insertions(+)
create mode 100644 49.Spring-Boot-Async/pom.xml
create mode 100644 49.Spring-Boot-Async/src/main/java/com/example/demo/DemoApplication.java
create mode 100644 49.Spring-Boot-Async/src/main/java/com/example/demo/config/AsyncPoolConfig.java
create mode 100644 49.Spring-Boot-Async/src/main/java/com/example/demo/controller/TestController.java
create mode 100644 49.Spring-Boot-Async/src/main/java/com/example/demo/service/TestService.java
create mode 100644 49.Spring-Boot-Async/src/main/resources/application.properties
create mode 100644 49.Spring-Boot-Async/src/test/java/com/example/demo/DemoApplicationTests.java
diff --git a/49.Spring-Boot-Async/pom.xml b/49.Spring-Boot-Async/pom.xml
new file mode 100644
index 0000000..06b47b8
--- /dev/null
+++ b/49.Spring-Boot-Async/pom.xml
@@ -0,0 +1,43 @@
+
+
+ 4.0.0
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.1.0.RELEASE
+
+
+ com.example
+ demo
+ 0.0.1-SNAPSHOT
+ demo
+ Demo project for Spring Boot
+
+
+ 1.8
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/49.Spring-Boot-Async/src/main/java/com/example/demo/DemoApplication.java b/49.Spring-Boot-Async/src/main/java/com/example/demo/DemoApplication.java
new file mode 100644
index 0000000..89019f9
--- /dev/null
+++ b/49.Spring-Boot-Async/src/main/java/com/example/demo/DemoApplication.java
@@ -0,0 +1,14 @@
+package com.example.demo;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableAsync;
+
+@SpringBootApplication
+@EnableAsync
+public class DemoApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(DemoApplication.class, args);
+ }
+}
diff --git a/49.Spring-Boot-Async/src/main/java/com/example/demo/config/AsyncPoolConfig.java b/49.Spring-Boot-Async/src/main/java/com/example/demo/config/AsyncPoolConfig.java
new file mode 100644
index 0000000..636131b
--- /dev/null
+++ b/49.Spring-Boot-Async/src/main/java/com/example/demo/config/AsyncPoolConfig.java
@@ -0,0 +1,31 @@
+package com.example.demo.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.ThreadPoolExecutor;
+
+/**
+ * @author MrBird
+ */
+@Configuration
+public class AsyncPoolConfig {
+
+ @Bean
+ public ThreadPoolTaskExecutor asyncThreadPoolTaskExecutor(){
+ ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+ executor.setCorePoolSize(20);
+ executor.setMaxPoolSize(200);
+ executor.setQueueCapacity(25);
+ executor.setKeepAliveSeconds(200);
+ executor.setThreadNamePrefix("asyncThread");
+ executor.setWaitForTasksToCompleteOnShutdown(true);
+ executor.setAwaitTerminationSeconds(60);
+
+ executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+
+ executor.initialize();
+ return executor;
+ }
+}
diff --git a/49.Spring-Boot-Async/src/main/java/com/example/demo/controller/TestController.java b/49.Spring-Boot-Async/src/main/java/com/example/demo/controller/TestController.java
new file mode 100644
index 0000000..9e89198
--- /dev/null
+++ b/49.Spring-Boot-Async/src/main/java/com/example/demo/controller/TestController.java
@@ -0,0 +1,52 @@
+package com.example.demo.controller;
+
+import com.example.demo.service.TestService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author MrBird
+ */
+@RestController
+public class TestController {
+
+ private Logger logger = LoggerFactory.getLogger(this.getClass());
+
+ @Autowired
+ private TestService testService;
+
+ @GetMapping("async")
+ public String testAsync() throws Exception {
+ long start = System.currentTimeMillis();
+ logger.info("异步方法开始");
+
+ Future stringFuture = testService.asyncMethod();
+ String result = stringFuture.get(60, TimeUnit.SECONDS);
+ logger.info("异步方法返回值:{}", result);
+
+ logger.info("异步方法结束");
+
+ long end = System.currentTimeMillis();
+ logger.info("总耗时:{} ms", end - start);
+ return stringFuture.get();
+ }
+
+ @GetMapping("sync")
+ public void testSync() {
+ long start = System.currentTimeMillis();
+ logger.info("同步方法开始");
+
+ testService.syncMethod();
+
+ logger.info("同步方法结束");
+ long end = System.currentTimeMillis();
+ logger.info("总耗时:{} ms", end - start);
+ }
+
+}
diff --git a/49.Spring-Boot-Async/src/main/java/com/example/demo/service/TestService.java b/49.Spring-Boot-Async/src/main/java/com/example/demo/service/TestService.java
new file mode 100644
index 0000000..255c59f
--- /dev/null
+++ b/49.Spring-Boot-Async/src/main/java/com/example/demo/service/TestService.java
@@ -0,0 +1,39 @@
+package com.example.demo.service;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.AsyncResult;
+import org.springframework.stereotype.Service;
+
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @author MrBird
+ */
+@Service
+public class TestService {
+
+ private Logger logger = LoggerFactory.getLogger(this.getClass());
+
+ @Async("asyncThreadPoolTaskExecutor")
+ // @Async
+ public Future asyncMethod() {
+ sleep();
+ logger.info("异步方法内部线程名称:{}", Thread.currentThread().getName());
+ return new AsyncResult<>("hello async");
+ }
+
+ public void syncMethod() {
+ sleep();
+ }
+
+ private void sleep() {
+ try {
+ TimeUnit.SECONDS.sleep(2);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/49.Spring-Boot-Async/src/main/resources/application.properties b/49.Spring-Boot-Async/src/main/resources/application.properties
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/49.Spring-Boot-Async/src/main/resources/application.properties
@@ -0,0 +1 @@
+
diff --git a/49.Spring-Boot-Async/src/test/java/com/example/demo/DemoApplicationTests.java b/49.Spring-Boot-Async/src/test/java/com/example/demo/DemoApplicationTests.java
new file mode 100644
index 0000000..480d1ca
--- /dev/null
+++ b/49.Spring-Boot-Async/src/test/java/com/example/demo/DemoApplicationTests.java
@@ -0,0 +1,16 @@
+package com.example.demo;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class DemoApplicationTests {
+
+ @Test
+ public void contextLoads() {
+ }
+
+}