第一部分: 无返回值异步任务
当没有加入异步任务的时候,我们创建一个service ,里面的方法需要等待3秒才能完成, controller层写一个测试方法调用时间返回的接口, 直接调用,
下面是service层代码部分
-
package com.zz.amqp1.service;
-
import org.springframework.stereotype.Service;
-
import java.util.Date;
-
/**
-
* Description: 异步任务测试类
-
* User: zhouzhou
-
* Date: 2018-08-28
-
* Time: 13:30
-
*/
-
@Service
-
public class AsyncService {
-
public void doNoReturn(){
-
try {
-
// 这个方法执行需要三秒
-
Thread.sleep(3000);
-
System.out.println("方法执行结束" + new Date());
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
}
-
}
下面是controller层代码部分
-
package com.zz.amqp1.controller;
-
import com.zz.amqp1.service.AsyncService;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestMethod;
-
import org.springframework.web.bind.annotation.RestController;
-
/**
-
* Description: 异步任务测试类
-
* User: zhouzhou
-
* Date: 2018-08-28
-
* Time: 13:33
-
*/
-
@RestController
-
public class AsyncController {
-
@Autowired
-
private AsyncService asyncService;
-
@RequestMapping(value = "/hello",method = RequestMethod.GET)
-
public String testAsyncNoRetrun(){
-
long start = System.currentTimeMillis();
-
asyncService.doNoReturn();
-
return String.format("任务执行成功,耗时{%s}", System.currentTimeMillis() - start);
-
}
-
}
启动项目在, 浏览器中输入url :http://localhost:8080/hello
返回结果如下:
3000毫秒.接下来我们在方法上加上注解 @Async后, 重启项目, 刷新页面查看调用时间
-
@Async
-
public void doNoReturn(){
-
try {
-
// 这个方法执行需要三秒
-
Thread.sleep(3000);
-
System.out.println("方法执行结束" + new Date());
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
}
并且在主启动类上开启@EnableAsync的注解
-
@EnableAsync // 开启异步任务
-
@SpringBootApplication
-
public class Amqp1Application {
-
public static void main(String[] args) {
-
SpringApplication.run(Amqp1Application.class, args);
-
}
-
}
测试完成, 异步任务如此简单,就这样.
第二部分:有返回值的异步任务
返回值用Futrue变量封装起来,下面是service层的代码
-
@Async
-
public Future<String> doReturn(int i){
-
try {
-
// 这个方法需要调用500毫秒
-
Thread.sleep(500);
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
// 消息汇总
-
return new AsyncResult<>(String.format("这个是第{%s}个异步调用的证书", i));
-
}
读取的时候,记得要批量读取不能单独读取, 下面是controller层的代码
-
@GetMapping("/hi")
-
public Map<String, Object> testAsyncReturn() throws ExecutionException, InterruptedException {
-
long start = System.currentTimeMillis();
-
Map<String, Object> map = new HashMap<>();
-
List<Future<String>> futures = new ArrayList<>();
-
for (int i = 0; i < 10; i++) {
-
Future<String> future = asyncService.doReturn(i);
-
futures.add(future);
-
}
-
List<String> response = new ArrayList<>();
-
for (Future future : futures) {
-
String string = (String) future.get();
-
response.add(string);
-
}
-
map.put("data", response);
-
map.put("消耗时间", String.format("任务执行成功,耗时{%s}毫秒", System.currentTimeMillis() - start));
-
return map;
-
}
在浏览器输入地址:http://localhost:8080/hi
结果如下: 耗时500多毫秒的意思代表,springboot自带异步任务线程池是小于10的大小的
一定要批量读取结果, 否则不能达到异步的效果!!
1、异步方法和调用类不要在同一个类中
2、注解扫描时,要注意过滤,避免重复实例化,因为存在覆盖问题,@Async就失效了