Browse Source

Support HandlerMethod parameter in @MessageExceptionHandler

Issue: SPR-13196
pull/835/head
Sebastien Deleuze 11 years ago
parent
commit
97936140cc
  1. 2
      spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java
  2. 26
      spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java

2
spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java

@ -493,7 +493,7 @@ public abstract class AbstractMethodMessageHandler<T> @@ -493,7 +493,7 @@ public abstract class AbstractMethodMessageHandler<T>
logger.debug("Invoking " + invocable.getShortLogMessage());
}
try {
Object returnValue = invocable.invoke(message, ex);
Object returnValue = invocable.invoke(message, ex, handlerMethod);
MethodParameter returnType = invocable.getReturnType();
if (void.class == returnType.getParameterType()) {
return;

26
spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java

@ -39,6 +39,7 @@ import org.springframework.messaging.MessageChannel; @@ -39,6 +39,7 @@ import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.messaging.handler.HandlerMethod;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
@ -207,6 +208,20 @@ public class SimpAnnotationMethodMessageHandlerTests { @@ -207,6 +208,20 @@ public class SimpAnnotationMethodMessageHandlerTests {
assertEquals("handleValidationException", this.testController.method);
}
@Test
public void exceptionWithHandlerMethodArg() {
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
headers.setSessionId("session1");
headers.setSessionAttributes(new ConcurrentHashMap<>());
headers.setDestination("/pre/illegalState");
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
this.messageHandler.handleMessage(message);
assertEquals("handleExceptionWithHandlerMethodArg", this.testController.method);
HandlerMethod handlerMethod = (HandlerMethod) this.testController.arguments.get("handlerMethod");
assertNotNull(handlerMethod);
assertEquals("illegalState", handlerMethod.getMethod().getName());
}
@Test
public void simpScope() {
Map<String, Object> map = new ConcurrentHashMap<>();
@ -405,11 +420,22 @@ public class SimpAnnotationMethodMessageHandlerTests { @@ -405,11 +420,22 @@ public class SimpAnnotationMethodMessageHandlerTests {
this.arguments.put("message", payload);
}
@MessageMapping("/illegalState")
public void illegalState() {
throw new IllegalStateException();
}
@MessageExceptionHandler(MethodArgumentNotValidException.class)
public void handleValidationException() {
this.method = "handleValidationException";
}
@MessageExceptionHandler(IllegalStateException.class)
public void handleExceptionWithHandlerMethodArg(HandlerMethod handlerMethod) {
this.method = "handleExceptionWithHandlerMethodArg";
this.arguments.put("handlerMethod", handlerMethod);
}
@MessageMapping("/scope")
public void scope() {
SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();

Loading…
Cancel
Save