java异步回调应用场景,Java异步调用系列之@Async使用

一、代码清单一(成功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]

b56d02d5f744

image.png

成功。

注意

1,不加@EnableAsync注解

若在启动类上不加@EnableAsync注解,则异步调用不生效,发现都是用的主线程

b56d02d5f744

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

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值