使用场景
Spring Boot中@Async和Future的使用场景适合于以下场景:
- 当前运行的任务可以分为N步分解时,例如一个统计需要统计三项数据,分别来源于三个表,那么我们可以把统计分为三个接口,在控制层使用Future调用任务。这种情况在控制层还是处于阻塞状态。
- 当前运行的任务不关心另外一个任务的运行结果,我们可以直接使用@Async实现异步调用。
- 其它需要异步调用的方法。
下面我们使用一个Spring Boot工程说明这1、2两种情况:
代码详解
- pom.xml引入spring-boot-starter-web。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 新建一个接口:PiceaService,里面包含三个方法
public interface PiceaService {
//无返回参数方法
void asyncTask() throws Exception;
//有返回参数方法
Future<String> asyncTaskFuture() throws Exception;
//有返回参数方法2
Future<String> asyncTaskFuture2() throws Exception;
}
- 新建接口实现类 PiceaServiceImpl。
要点:
1)注意每个方法上面的注解@Async,这个一定要写,否则就不是异步任务
@Service
public class PiceaServiceImpl implements PiceaService {
@Async
@Override
public void asyncTask() throws Exception {
System.out.println("异步线程,线程名:" + Thread.currentThread().getName());
System.out.println("异步处理方法-----start-------");
System.out.println("------------------------在看貂蝉,不要打扰--------------");
Thread.sleep(1000);
System.out.println("异步处理方法------end--------");
}
@Async
@Override
public Future<String> asyncTaskFuture() throws Exception {
System.out.println("异步线程,线程名:" + Thread.currentThread().getName());
System.out.println("异步处理方法-----start-------asyncTaskFuture---");
int k = 1;
Thread.sleep(1000);
System.out.println("异步处理方法-----end---------asyncTaskFuture---");
return new AsyncResult<String> (String.valueOf(k));
}
@Async
@Override
public Future<String> asyncTaskFuture2() throws Exception {
System.out.println("异步线程,线程名:" + Thread.currentThread().getName());
System.out.println("异步处理方法-----start-------asyncTaskFuture-----2---");
int k = 2;
System.out.println("异步处理方法-----end---------asyncTaskFuture-----2---");
return new AsyncResult<String> (String.valueOf(k));
}
}
- 然后我们建立一个控制类 PiceaContoller。
@RestController
public class PiceaContoller {
@Autowired
private PiceaService piceaService;
@RequestMapping("/asyncTask")
public void asyncTask() throws Exception {
piceaService.asyncTask();
}
@RequestMapping("/asyncTaskFuture")
public String asyncTaskFuture() throws Exception {
String ret = null;
//异步先执行任务1
Future<String> future = piceaService.asyncTaskFuture();
//异步执行任务2
Future<String> future2 = piceaService.asyncTaskFuture2();
String ret1 = null;
String ret2 = null;
//获取任务1执行结果
while (true) {
if (future.isDone()){
ret1 = future.get();
break;
}
}
//获取任务2执行结果
while (true) {
if (future2.isDone()) {
ret2 = future2.get();
break;
}
}
//任务1结果+任务2结果
ret = ret1 + "+" + ret2;
//最终返回任何合集
return ret;
}
}
- 最后一步,不要忘记在启动类加异步服务开启的注解@EnableAsync。
@EnableAsync
@SpringBootApplication
public class SpringBootAsync1Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootAsync1Application.class, args);
}
}
大功告成!赶紧回家找妈妈!
-
查看结果
从结果上,可以明显看出两个异步执行的任务,因为我在代码中,让任务1休眠了1秒。
Spring-boot-asyncFuture.png
其它注意
本文SpringBoot版本为2.1.3
本文章样例:
工程名:spring-boot-async-1
GitHub:https://github.com/zzyjb/SpringBootLearning
- Web工程Async异步-本章
https://www.jianshu.com/p/7482721e3ac8 - 自定义线程池请移步
https://www.jianshu.com/p/dabd65f617bb - 统一异常处理请移步
https://www.jianshu.com/p/11c78717799b
网友评论