一、代码清单一(成功demo)
@EnableAsync+@Async
1,启动类
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.sun.test.*"})
@EnableAsync
public class TestApplication {
public static void main(String[] args) {
SpringBootApplication.run(LogicCephApplication.class, args);
}
}
2,Controller
@Controller
@RequestMapping(value = "/api/v1/aync")
public class TestController {
@Autowired
TestService testService;
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public List testAync() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
Future<List<Integer>> res = testService.getValue(i);
try {
list.addAll(res.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return list;
}
}
3, TestService
@Service
public class TestService {
Logger logger = LoggerFactory.getLogger(TestService.class);
@Async
public Future<List<Integer>> getValue(int i) {
logger.debug("==== thread id: {}", Thread.currentThread());
logger.debug("==== i: {}", i);
List list = new ArrayList(10);
for (int j = 1; j <= 10; j++) {
// 1-10
list.add(i * 10 + j);
}
logger.debug("==== list: {}", list);
return new AsyncResult<List<Integer>>(list);
}
}
测试&&结果
curl -XGET http://localhost:8700/api/v1/aync/
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]
成功。
注意
1,不加@EnableAsync注解
若在启动类上不加@EnableAsync注解,则异步调用不生效,发现都是用的主线程
image.png
2,异步调用报错
可以捕获res.get()
语句进行处理
3,在同一类中,非异步方法调用异步方法,异步调动失效
解决办法:
1)把这两个方法分开到不同的类中;
2)把注解加到类名上面;
目前测试方法1)比较好用和控制
二、代码清单二(比较全面的测试demo)
包含
- 异步调用成功demo
- 异步调用过程中异常
- 同一类中异步调用失效
1,启动类
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.sun.test.*"})
@EnableAsync
public class TestApplication {
public static void main(String[] args) {
SpringBootApplication.run(LogicCephApplication.class, args);
}
}
2,Controller
@Controller
@RequestMapping(value = "/api/v1/aync")
public class TestController {
@Autowired
TestService testService;
@Autowired
TestService2 testService2;
/**
* 成功demo
* @return
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
@ResponseBody
public List testAync() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
Future<List<Integer>> res = testService.getValue(i);
try {
list.addAll(res.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return list;
}
/**
* 异步调用过程异常
* @return
*/
@RequestMapping(value = "/testException", method = RequestMethod.GET)
@ResponseBody
public List testAyncWithException() {
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
Future<List<Integer>> res = testService.getValueWithException(i);
try {
list.addAll(res.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return list;
}
/**
* 同一类中调用失效
* @return
*/
@RequestMapping(value = "/test1", method = RequestMethod.GET)
@ResponseBody
public List test1() {
List<Integer> list = testService.test1();
return list;
}
/**
* 调用类和异步调用类分离
* @return
*/
@RequestMapping(value = "/test2", method = RequestMethod.GET)
@ResponseBody
public List test2() {
List<Integer> list = testService2.test2();
return list;
}
}
3, TestService
@Service
public class TestService {
Logger logger = LoggerFactory.getLogger(TestService.class);
@Async
public Future<List<Integer>> getValue(int i) {
logger.debug("==== thread id: {}", Thread.currentThread());
logger.debug("==== i: {}", i);
List list = new ArrayList(10);
for (int j = 1; j <= 10; j++) {
// 1-10
list.add(i * 10 + j);
}
logger.debug("==== list: {}", list);
return new AsyncResult<List<Integer>>(list);
}
@Async
public Future<List<Integer>> getValueWithException(int i) {
logger.debug("==== thread id: {}", Thread.currentThread());
logger.debug("==== i: {}", i);
if(i == 2){
throw new RuntimeException();
}
List list = new ArrayList(10);
for (int j = 1; j <= 10; j++) {
// 1-10
list.add(i * 10 + j);
}
logger.debug("==== list: {}", list);
return new AsyncResult<List<Integer>>(list);
}
public List test1(){
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
Future<List<Integer>> res = getValue(i);
try {
list.addAll(res.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return list;
}
}
4, TestService2
@Service
public class TestService2 {
@Autowired
TestService testService;
Logger logger = LoggerFactory.getLogger(TestService2.class);
public List test2(){
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
Future<List<Integer>> res = testService.getValue(i);
try {
list.addAll(res.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return list;
}
}
5,测试
- 调用成功demo
curl -XGET http://localhost:8700/api/v1/aync/
- 调用过程中异常情况
curl -XGET http://localhost:8700/api/v1/aync/testException
- 同类异步调用失效
curl -XGET http://localhost:8700/api/v1/aync/test1
- 调用类和被调用类分离
curl -XGET http://localhost:8700/api/v1/aync/test2
参考
【1】springboot-@Async默认线程池导致OOM问题
【2】@Transaction,@Async在同一个类中注解失效的原因和解决办法
【3】在同一个类中,一个方法调用另外一个有注解(比如@Async,@Transational)的方法,注解失效的原因和解决方法
网友评论