提交 12899a3c 编写于 作者: 武汉红喜's avatar 武汉红喜

whatsmars-boot-sample-async

上级 683fd20d
......@@ -26,6 +26,7 @@
<module>whatsmars-boot-sample-beans</module>
<module>whatsmars-boot-sample-actuator</module>
<module>whatsmars-boot-sample-web</module>
<module>whatsmars-boot-sample-async</module>
</modules>
<dependencies>
......
<?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">
<parent>
<artifactId>whatsmars-spring-boot-samples</artifactId>
<groupId>org.hongxi</groupId>
<version>Rocket.S8</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>whatsmars-boot-sample-async</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
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<String> future = messageService.hello("world");
messageService.send("world");
try {
System.out.println(future.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
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);
}
}
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<String> 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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册