Skip to content

Commit 76831cb

Browse files
committed
线程池拒绝异常捕获
1 parent c163bf3 commit 76831cb

File tree

2 files changed

+106
-6
lines changed

2 files changed

+106
-6
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.example.exception;
2+
3+
import cn.hutool.core.util.ObjectUtil;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.bind.annotation.ResponseStatus;
10+
import org.springframework.web.bind.annotation.RestControllerAdvice;
11+
import org.springframework.web.servlet.NoHandlerFoundException;
12+
13+
import javax.servlet.http.HttpServletRequest;
14+
import java.util.concurrent.ExecutionException;
15+
import java.util.concurrent.RejectedExecutionException;
16+
17+
/**
18+
* 全局异常控制处理器
19+
* 未找到指定异常处理方法exception,则以全局异常方法globalException兜底
20+
*
21+
* @author wliduo[i@dolyw.com]
22+
* @date 2021/10/12 15:46
23+
*/
24+
@RestControllerAdvice
25+
public class ExceptionAdvice {
26+
27+
/**
28+
* logger
29+
*/
30+
private static final Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);
31+
32+
@Autowired
33+
private HttpServletRequest request;
34+
35+
/**
36+
* 捕捉RejectedExecutionException-线程拒绝策略异常
37+
*
38+
* @param e
39+
* @return java.lang.Object
40+
* @throws
41+
* @author wliduo[i@dolyw.com]
42+
* @date 2021/11/9 13:49
43+
*/
44+
@ResponseStatus(HttpStatus.OK)
45+
@ExceptionHandler(RejectedExecutionException.class)
46+
public Object exception(RejectedExecutionException e) {
47+
// logger.error("{}接口线程服务异常:{}", request.getRequestURI(), ExceptionUtil.stacktraceToOneLineString(e.getCause(), 10000000));
48+
logger.error("{}接口线程服务异常:{}", request.getRequestURI(), e);
49+
return e.getMessage();
50+
}
51+
52+
/**
53+
* 捕捉ExecutionException-FutureTask异步线程异常
54+
*
55+
* @param e
56+
* @return java.lang.Object
57+
* @throws
58+
* @author wliduo[i@dolyw.com]
59+
* @date 2021/11/9 13:46
60+
*/
61+
@ResponseStatus(HttpStatus.OK)
62+
@ExceptionHandler(ExecutionException.class)
63+
public Object exception(ExecutionException e) {
64+
// logger.error("{}接口异步线程服务异常:{}", request.getRequestURI(), ExceptionUtil.stacktraceToOneLineString(e.getCause(), 10000000));
65+
logger.error("{}接口异步线程服务异常:{}", request.getRequestURI(), e.getCause());
66+
if (ObjectUtil.isNotEmpty(e.getCause().getMessage())) {
67+
return e.getCause().getMessage();
68+
} else {
69+
return e.getMessage();
70+
}
71+
}
72+
73+
/**
74+
* 捕捉404未找到路径异常
75+
*
76+
* @param e
77+
* @return java.lang.Object
78+
* @throws
79+
* @author wliduo[i@dolyw.com]
80+
* @date 2021/10/12 16:08
81+
*/
82+
@ResponseStatus(HttpStatus.OK)
83+
@ExceptionHandler(NoHandlerFoundException.class)
84+
public Object exception(NoHandlerFoundException e) {
85+
return "404";
86+
}
87+
88+
/**
89+
* 捕捉其他所有Exception异常
90+
*
91+
* @param e
92+
* @return com.pcic.app.common.dto.ResponseMessage
93+
* @throws
94+
* @author wliduo[i@dolyw.com]
95+
* @date 2021/10/12 16:09
96+
*/
97+
@ResponseStatus(HttpStatus.OK)
98+
@ExceptionHandler(Exception.class)
99+
public Object globalException(Exception e) {
100+
// logger.error("{}接口服务异常:{}", request.getRequestURI(), ExceptionUtil.stacktraceToOneLineString(e, 10000000));
101+
logger.error("{}接口服务异常:{}", request.getRequestURI(), e);
102+
return e.getMessage();
103+
}
104+
}

SpringBoot/ThreadPool/src/main/java/com/example/util/SmsUtil.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,10 @@ public class SmsUtil {
2727
* @date 2020/5/20 10:53
2828
*/
2929
@Async("jdkThreadPoolExecutor")
30-
public void sendCode(String phone, String code) {
30+
public void sendCode(String phone, String code) throws Exception {
3131
logger.info("开始发送验证码...");
3232
// 模拟调用接口发验证码的耗时
33-
try {
34-
Thread.sleep(3000);
35-
} catch (InterruptedException e) {
36-
e.printStackTrace();
37-
}
33+
Thread.sleep(3000);
3834
logger.info("发送成功: {}", phone);
3935
}
4036

0 commit comments

Comments
 (0)