There is a quite similar question here, but the answer does not suffice for my question.
I have this method in a @Service
class:
@Async
public void activateUser(...){
if(someCondition){
throw new GeneralSecurityException();
}
}
The controller:
@GetMapping( "/activate")
public ResponseEntity<Void> activate(...){
myService.activateUser(...);
}
And the controller advice:
@RestControllerAdvice( basePackages = "com.app.security" )
public class SecurityAdviceController extends ResponseEntityExceptionHandler {
@ExceptionHandler( GeneralSecurityException.class )
public ResponseEntity<GeneralSecurityExceptionBody> handleGeneralSecurityException( GeneralSecurityException ex ) {
return ResponseEntity
.status( HttpStatus.MOVED_PERMANENTLY )
.header( HttpHeaders.LOCATION, ex.getUrl() )
.body( null );
}
Here we are. Since the exception will be thrown in another thread, how can I proceed to make it available for @RestControllerAdvice
?
Many suggest to implement AsyncUncaughtExceptionHandler, and I agree, but that does not answer the question.
When I remove @Async
, everything is fine, and I can see that the same thread does all the tasks, but with @Async
I have 2 threads involved.
One solution would be to get the exception thrown by the parent thread (but it's too cumbersome, and I don't see how to implement that).
Thanks for your help.