面试题答案
一键面试默认异常处理机制
在Spring Boot异步方法调用过程中,如果发生异常,默认情况下,这些异常不会被主调线程捕获,因为异步方法在独立的线程中执行。如果异步方法未对异常进行处理,异常会导致执行该异步方法的线程终止,但不会影响主调线程。
自定义异步方法的异常处理策略
- 使用@Async注解的异常处理:
- 创建一个实现
AsyncUncaughtExceptionHandler
接口的类,并重写handleUncaughtException
方法。
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import java.lang.reflect.Method; public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler { @Override public void handleUncaughtException(Throwable throwable, Method method, Object... obj) { System.out.println("Exception message - " + throwable.getMessage()); System.out.println("Method name - " + method.getName()); for (Object param : obj) { System.out.println("Parameter value - " + param); } } }
- 在配置类中配置
AsyncConfigurer
,将自定义的异常处理器注册到Spring容器中。
import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; @Configuration @EnableAsync public class AsyncConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); executor.setMaxPoolSize(20); executor.setQueueCapacity(50); executor.initialize(); return executor; } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new CustomAsyncExceptionHandler(); } }
- 创建一个实现
- 在异步方法中捕获异常:
- 在异步方法内部使用
try-catch
块捕获异常,并进行相应处理。
import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; @Service public class AsyncService { @Async public void asyncMethod() { try { // 可能会抛出异常的业务逻辑 int result = 10 / 0; } catch (Exception e) { // 异常处理逻辑 System.out.println("Caught exception in async method: " + e.getMessage()); } } }
- 在异步方法内部使用
- 使用Future获取异步结果并处理异常:
- 异步方法返回
Future
类型。
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Service; import java.util.concurrent.Future; @Service public class AsyncService { @Async public Future<String> asyncMethodWithResult() { try { // 可能会抛出异常的业务逻辑 Thread.sleep(2000); return new AsyncResult<>("Success"); } catch (InterruptedException e) { return new AsyncResult<>("Exception: " + e.getMessage()); } } }
- 在调用方获取
Future
结果时处理异常。
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @RestController public class AsyncController { @Autowired private AsyncService asyncService; @GetMapping("/async") public String asyncCall() { Future<String> future = asyncService.asyncMethodWithResult(); try { return future.get(); } catch (InterruptedException | ExecutionException e) { return "Exception: " + e.getMessage(); } } }
- 异步方法返回