diff --git a/whatsmars-spring-boot-samples/pom.xml b/whatsmars-spring-boot-samples/pom.xml index 379d71622396d4735865666967a0814f0db8c260..342c7ab8d7a716200d0147bf787db0ad5a083baa 100644 --- a/whatsmars-spring-boot-samples/pom.xml +++ b/whatsmars-spring-boot-samples/pom.xml @@ -26,6 +26,7 @@ whatsmars-boot-sample-beans whatsmars-boot-sample-actuator whatsmars-boot-sample-web + whatsmars-boot-sample-async diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/pom.xml b/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..3461dc26a4b14a74ab6a570ed8b26b8145b57d8c --- /dev/null +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/pom.xml @@ -0,0 +1,21 @@ + + + + whatsmars-spring-boot-samples + org.hongxi + Rocket.S8 + + 4.0.0 + + whatsmars-boot-sample-async + + + + org.springframework.boot + spring-boot-starter + + + + \ No newline at end of file diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/src/main/java/org/hongxi/whatsmars/boot/sample/async/Application.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/src/main/java/org/hongxi/whatsmars/boot/sample/async/Application.java new file mode 100644 index 0000000000000000000000000000000000000000..89ff54bac069f4e04681fe7849732b2ad71e50f4 --- /dev/null +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/src/main/java/org/hongxi/whatsmars/boot/sample/async/Application.java @@ -0,0 +1,32 @@ +package org.hongxi.whatsmars.boot.sample.async; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.scheduling.annotation.EnableAsync; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +/** + * Created by shenhongxi on 2020/8/16. + */ +@EnableAsync +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = + SpringApplication.run(Application.class, args); + MessageService messageService = applicationContext.getBean(MessageService.class); + CompletableFuture future = messageService.hello("world"); + messageService.send("world"); + try { + System.out.println(future.get()); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } + } +} diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/src/main/java/org/hongxi/whatsmars/boot/sample/async/AsyncAutoConfiguration.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/src/main/java/org/hongxi/whatsmars/boot/sample/async/AsyncAutoConfiguration.java new file mode 100644 index 0000000000000000000000000000000000000000..012aaaa55c489496b2fb51d71f62029d59a2ba42 --- /dev/null +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/src/main/java/org/hongxi/whatsmars/boot/sample/async/AsyncAutoConfiguration.java @@ -0,0 +1,19 @@ +package org.hongxi.whatsmars.boot.sample.async; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +/** + * Created by shenhongxi on 2020/8/16. + */ +@Configuration +public class AsyncAutoConfiguration { + + @Bean + public Executor taskExecutor() { + return Executors.newFixedThreadPool(10); + } +} diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/src/main/java/org/hongxi/whatsmars/boot/sample/async/MessageService.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/src/main/java/org/hongxi/whatsmars/boot/sample/async/MessageService.java new file mode 100644 index 0000000000000000000000000000000000000000..66321f5c7f3919b6b812096ab577e05a550d99f7 --- /dev/null +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-async/src/main/java/org/hongxi/whatsmars/boot/sample/async/MessageService.java @@ -0,0 +1,28 @@ +package org.hongxi.whatsmars.boot.sample.async; + +import org.springframework.scheduling.annotation.Async; +import org.springframework.stereotype.Service; + +import java.util.concurrent.CompletableFuture; + +/** + * Created by shenhongxi on 2018/5/8. + */ +@Service +public class MessageService { + + @Async("taskExecutor") + public CompletableFuture hello(String message) { + return CompletableFuture.completedFuture("Hello, " + message); + } + + @Async("taskExecutor") + public void send(String message) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("send " + message); + } +}