There is a context with a listener.

@Override public void onStartup(ServletContext servletContext) throws ServletException { servletContext.addListener(SentryServletRequestListener.class); super.onStartup(servletContext); } 

and two methods. One of them is annotated with @Async

 @Async public void send(Long providerId, String name, String email) { throw new RuntimeException(); } public void load(Long providerId, String name, String email) { throw new RuntimeException(); } 

The listener responds to non-asynchronous method exceptions. The question is: is it possible to set up and how, so that the reaction was also to exceptions from the ASYNCHRONIC methods Thank you in advance

    1 answer 1

    Asynchronous methods are executed in a separate thread, the exceptions that occur and remain in it, the caller can not find out about them in any way. To handle asynchronous exceptions, you need to declare a separate listener:

     public class MyAsyncExceptionHandler 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); } } } 

    And register it in context:

     <task:annotation-driven executor="someExecutorBean" exception-handler="asyncExceptionHandler"/> <bean id="asyncExceptionHandler" class="com.example.MyAsyncExceptionHandler"/> 

    Or, if you use java-configuration:

     @Configuration @EnableAsync public class SpringAsyncConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { return new ThreadPoolTaskExecutor(); } @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new MyAsyncExceptionHandler(); } } 
    • Thank you very much for the answer! That is how the issue was resolved - YuriyK