在Spring Boot应用程序中未调用Thread.setDefaultUncaughtExceptionHandler [英] Thread.setDefaultUncaughtExceptionHandler not being called in Spring Boot application
问题描述
我正在尝试在Spring Boot应用程序中实现全局未捕获的异常处理程序,该处理程序从大量客户端应用程序中收集指标和异常等.看来在调试时已设置了处理程序,因此我假设在我的代码运行后分配了另一个处理程序,还是未在spring应用程序上下文中设置这些处理程序?我的代码如下.
I am trying to implement a global uncaught exception handler in a spring boot application, which gathers metrics and exceptions etc from a bunch of client applications. It appears that the handler is being set when I debug, so I would assume that either another handler is assigned after my code runs, or these handlers are not set on the spring application context? My code is as follows.
我已经为我的默认异常处理程序创建了此类-
I have created this class for my default exception handler -
public class JvmrtExceptionHandler implements Thread.UncaughtExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(JvmrtExceptionHandler.class);
private RestTemplate restTemplate = new RestTemplate();
@Override
public void uncaughtException(Thread thread, Throwable exception) {
String exceptionClass = exception.getStackTrace()[0].getClassName();
String exceptionMethod = exception.getStackTrace()[0].getMethodName();
String exceptionMessage = exception.getMessage();
String exceptionType = exception.getClass().getSimpleName();
String appName = exception.getClass().getPackage().getImplementationTitle();
ExceptionModel thrownException = new ExceptionModel(
exceptionMessage,
appName,
exceptionMethod,
exceptionClass,
exceptionType
);
LOGGER.error("Uncaught Exception thrown of type {}. Sending to JVMRT Main app for processing", exceptionType);
String exceptionUrl = String.format("http://%s:%s/api/", "localhost", 8090);
restTemplate.postForObject(exceptionUrl, thrownException, String.class);
}
}
在另一个将以前的应用程序作为maven依赖项的应用程序中,此类配置了我创建的默认处理程序.我希望该处理程序适用于所有线程,因此我使用了静态Thread.setDefaultUncaughtExceptionHandler方法进行设置.
And this class, in another application which has the former application as a maven dependency, configures the default handler I have created. I would like the handler to work for all threads, so I have used the static Thread.setDefaultUncaughtExceptionHandler method to set it.
@Configuration
public class Config {
@Bean
public JvmrtExceptionHandler jvmrtExceptionHandler() {
JvmrtExceptionHandler exceptionHandler = new JvmrtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(exceptionHandler);
return exceptionHandler;
}
}
我正在用以下代码测试处理程序.
I am testing the handler with the following code.
@RestController
@RequestMapping(value = "/test")
public class ExceptionTest {
@RequestMapping(value = "/exception", method = RequestMethod.GET)
public void throwException() {
throw new RuntimeException();
}
}
我做错了什么?真是在敲我的头,任何帮助将不胜感激.
What am I doing wrong? Really banging my head here, any help would be appreciated.
推荐答案
由于您的 RuntimeException 被嵌入的tomcat捕获.您可以在中找到 try catch org.apache.catalina.core.StandardWrapperValve.invoke . 它会打印完整的异常消息,并在响应中添加错误.
Because your RuntimeException is caught by embed tomcat.you can find the try catch in org.apache.catalina.core.StandardWrapperValve.invoke. it print the full exception message,and put error in response.
如果要在Spring请求映射中捕获异常,可以尝试 ControllerAdvice 和 ExceptionHandler .
if you want to catch exception in spring request mapping.you can try ControllerAdvice and ExceptionHandler.
这篇关于在Spring Boot应用程序中未调用Thread.setDefaultUncaughtExceptionHandler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!