I have a simple integration flow which sends request to an external resource with several attempts. I am creating RequestHandlerRetryAdvice
for this purpose:
RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice();
requestHandlerRetryAdvice.setRecoveryCallback(retryContext -> {
Message<?> failedMessage = ((MessagingException) retryContext.getLastThrowable()).getFailedMessage();
//TODO: get http status
return failedMessage.getPayload();
});
What I need to know is the http status. How can I get it?
The HTTP Status is a part of a thrown exception:
((MessagingException) retryContext.getLastThrowable()).getCause()
should contain an HttpStatusCodeException
with the getStatusCode()
property.
My tests shows this:
retryContext = ...
lastException = org.springframework.messaging.MessagingException: ...
failedMessage = ...
backtrace = {Object[6]@7673}
detailMessage = "Failed to handle"
cause = org.springframework.messaging.MessageHandlingException: ...
failedMessage = ...
cause = {HttpClientErrorException$Forbidden@7685} "org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: [no body]"
statusCode = {HttpStatus@7690} "403 FORBIDDEN"
rawStatusCode = 403
statusText = "Forbidden"
The code is like this:
.handle(Http.outboundGateway("/service/internal?{params}")
.uriVariable("params", "payload")
.expectedResponseType(String.class),
e -> e.advice(retryAdvice()))
private RequestHandlerRetryAdvice retryAdvice() {
RequestHandlerRetryAdvice requestHandlerRetryAdvice = new RequestHandlerRetryAdvice();
requestHandlerRetryAdvice.setRecoveryCallback(retryContext -> {
MessagingException lastThrowable = (MessagingException) retryContext.getLastThrowable();
return ((HttpStatusCodeException) lastThrowable.getCause().getCause()).getStatusCode();
});
return requestHandlerRetryAdvice;
}