概述
一、代码清单一(成功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 list = new ArrayList();
for (int i = 0; i < 10; i++) {
Future> 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> 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);
}
}
测试&&结果
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]
image.png
成功。
注意
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 list = new ArrayList();
for (int i = 0; i < 10; i++) {
Future> 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 list = new ArrayList();
for (int i = 0; i < 10; i++) {
Future> 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 list = testService.test1();
return list;
}
/**
* 调用类和异步调用类分离
* @return
*/
@RequestMapping(value = "/test2", method = RequestMethod.GET)
@ResponseBody
public List test2() {
List list = testService2.test2();
return list;
}
}
3, TestService
@Service
public class TestService {
Logger logger = LoggerFactory.getLogger(TestService.class);
@Async
public Future> 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);
}
@Async
public Future> 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);
}
public List test1(){
List list = new ArrayList();
for (int i = 0; i < 10; i++) {
Future> 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 list = new ArrayList();
for (int i = 0; i < 10; i++) {
Future> 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
参考
最后
以上就是活力龙猫为你收集整理的java异步回调应用场景,Java异步调用系列之@Async使用的全部内容,希望文章能够帮你解决java异步回调应用场景,Java异步调用系列之@Async使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复