AbstractMethodMessageHandler processes Error as MessageHandlingException

Issue: SPR-16912
This commit is contained in:
Juergen Hoeller 2018-06-10 23:59:07 +02:00
parent e2ccd55d14
commit 0c8cfa05b5
2 changed files with 15 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ public interface MessageHandler {
/** /**
* Handle the given message. * Handle the given message.
* @param message the message to be handled * @param message the message to be handled
* @throws MessagingException if the handler failed to process the message
*/ */
void handleMessage(Message<?> message) throws MessagingException; void handleMessage(Message<?> message) throws MessagingException;

View File

@ -38,6 +38,7 @@ import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.MessagingException; import org.springframework.messaging.MessagingException;
import org.springframework.messaging.handler.DestinationPatternsMessageCondition; import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
import org.springframework.messaging.handler.HandlerMethod; import org.springframework.messaging.handler.HandlerMethod;
@ -395,6 +396,7 @@ public abstract class AbstractMethodMessageHandler<T>
if (lookupDestination == null) { if (lookupDestination == null) {
return; return;
} }
MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message); MessageHeaderAccessor headerAccessor = MessageHeaderAccessor.getMutableAccessor(message);
headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination); headerAccessor.setHeader(DestinationPatternsMessageCondition.LOOKUP_DESTINATION_HEADER, lookupDestination);
headerAccessor.setLeaveMutable(true); headerAccessor.setLeaveMutable(true);
@ -454,9 +456,9 @@ public abstract class AbstractMethodMessageHandler<T>
handleNoMatch(this.handlerMethods.keySet(), lookupDestination, message); handleNoMatch(this.handlerMethods.keySet(), lookupDestination, message);
return; return;
} }
Comparator<Match> comparator = new MatchComparator(getMappingComparator(message)); Comparator<Match> comparator = new MatchComparator(getMappingComparator(message));
matches.sort(comparator); matches.sort(comparator);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Found " + matches.size() + " handler methods: " + matches); logger.trace("Found " + matches.size() + " handler methods: " + matches);
} }
@ -533,16 +535,16 @@ public abstract class AbstractMethodMessageHandler<T>
processHandlerMethodException(handlerMethod, ex, message); processHandlerMethodException(handlerMethod, ex, message);
} }
catch (Throwable ex) { catch (Throwable ex) {
if (logger.isErrorEnabled()) { Exception handlingException =
logger.error("Error while processing message " + message, ex); new MessageHandlingException(message, "Unexpected handler method invocation error", ex);
} processHandlerMethodException(handlerMethod, handlingException, message);
} }
} }
protected void processHandlerMethodException(HandlerMethod handlerMethod, Exception ex, Message<?> message) { protected void processHandlerMethodException(HandlerMethod handlerMethod, Exception exception, Message<?> message) {
InvocableHandlerMethod invocable = getExceptionHandlerMethod(handlerMethod, ex); InvocableHandlerMethod invocable = getExceptionHandlerMethod(handlerMethod, exception);
if (invocable == null) { if (invocable == null) {
logger.error("Unhandled exception from message handler method", ex); logger.error("Unhandled exception from message handler method", exception);
return; return;
} }
invocable.setMessageMethodArgumentResolvers(this.argumentResolvers); invocable.setMessageMethodArgumentResolvers(this.argumentResolvers);
@ -550,7 +552,10 @@ public abstract class AbstractMethodMessageHandler<T>
logger.debug("Invoking " + invocable.getShortLogMessage()); logger.debug("Invoking " + invocable.getShortLogMessage());
} }
try { try {
Object returnValue = invocable.invoke(message, ex, handlerMethod); Throwable cause = exception.getCause();
Object returnValue = (cause != null ?
invocable.invoke(message, exception, cause, handlerMethod) :
invocable.invoke(message, exception, handlerMethod));
MethodParameter returnType = invocable.getReturnType(); MethodParameter returnType = invocable.getReturnType();
if (void.class == returnType.getParameterType()) { if (void.class == returnType.getParameterType()) {
return; return;