use of org.springframework.messaging.handler.HandlerMethod in project spring-framework by spring-projects.
the class AbstractMethodMessageHandler method getExceptionHandlerMethod.
/**
* Find an {@code @MessageExceptionHandler} method for the given exception.
* The default implementation searches methods in the class hierarchy of the
* HandlerMethod first and if not found, it continues searching for additional
* {@code @MessageExceptionHandler} methods among the configured
* {@linkplain org.springframework.messaging.handler.MessagingAdviceBean
* MessagingAdviceBean}, if any.
* @param handlerMethod the method where the exception was raised
* @param exception the raised exception
* @return a method to handle the exception, or {@code null}
* @since 4.2
*/
@Nullable
protected InvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handlerMethod, Exception exception) {
if (logger.isDebugEnabled()) {
logger.debug("Searching methods to handle " + exception.getClass().getSimpleName());
}
Class<?> beanType = handlerMethod.getBeanType();
AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(beanType);
if (resolver == null) {
resolver = createExceptionHandlerMethodResolverFor(beanType);
this.exceptionHandlerCache.put(beanType, resolver);
}
Method method = resolver.resolveMethod(exception);
if (method != null) {
return new InvocableHandlerMethod(handlerMethod.getBean(), method);
}
for (Map.Entry<MessagingAdviceBean, AbstractExceptionHandlerMethodResolver> entry : this.exceptionHandlerAdviceCache.entrySet()) {
MessagingAdviceBean advice = entry.getKey();
if (advice.isApplicableToBeanType(beanType)) {
resolver = entry.getValue();
method = resolver.resolveMethod(exception);
if (method != null) {
return new InvocableHandlerMethod(advice.resolveBean(), method);
}
}
}
return null;
}
use of org.springframework.messaging.handler.HandlerMethod in project spring-framework by spring-projects.
the class AbstractMethodMessageHandler method registerHandlerMethod.
/**
* Register a handler method and its unique mapping.
* <p><strong>Note:</strong> As of 5.3 this method is public (rather than
* protected) and can be used both at startup and at runtime.
* @param handler the bean name of the handler or the handler instance
* @param method the method to register
* @param mapping the mapping conditions associated with the handler method
* @throws IllegalStateException if another method was already registered
* under the same mapping
*/
public final void registerHandlerMethod(Object handler, Method method, T mapping) {
Assert.notNull(mapping, "Mapping must not be null");
HandlerMethod newHandlerMethod = createHandlerMethod(handler, method);
HandlerMethod oldHandlerMethod = this.handlerMethods.get(mapping);
if (oldHandlerMethod != null && !oldHandlerMethod.equals(newHandlerMethod)) {
throw new IllegalStateException("Ambiguous mapping found. Cannot map '" + newHandlerMethod.getBean() + "' bean method \n" + newHandlerMethod + "\nto " + mapping + ": There is already '" + oldHandlerMethod.getBean() + "' bean method\n" + oldHandlerMethod + " mapped.");
}
mapping = extendMapping(mapping, newHandlerMethod);
this.handlerMethods.put(mapping, newHandlerMethod);
for (String pattern : getDirectLookupMappings(mapping)) {
List<T> values = this.destinationLookup.computeIfAbsent(pattern, p -> new CopyOnWriteArrayList<>());
values.add(mapping);
}
}
use of org.springframework.messaging.handler.HandlerMethod in project spring-framework by spring-projects.
the class SimpAnnotationMethodMessageHandlerTests method exceptionWithHandlerMethodArg.
@Test
public void exceptionWithHandlerMethodArg() {
Message<?> message = createMessage("/pre/illegalState");
this.messageHandler.registerHandler(this.testController);
this.messageHandler.handleMessage(message);
assertThat(this.testController.method).isEqualTo("handleExceptionWithHandlerMethodArg");
HandlerMethod handlerMethod = (HandlerMethod) this.testController.arguments.get("handlerMethod");
assertThat(handlerMethod).isNotNull();
assertThat(handlerMethod.getMethod().getName()).isEqualTo("illegalState");
}
use of org.springframework.messaging.handler.HandlerMethod in project spring-framework by spring-projects.
the class RSocketMessageHandlerTests method testMapping.
private static void testMapping(Object controller, String... expectedPatterns) {
RSocketMessageHandler handler = new RSocketMessageHandler();
handler.setDecoders(Collections.singletonList(StringDecoder.allMimeTypes()));
handler.setEncoders(Collections.singletonList(CharSequenceEncoder.allMimeTypes()));
handler.setHandlers(Collections.singletonList(controller));
handler.afterPropertiesSet();
Map<CompositeMessageCondition, HandlerMethod> map = handler.getHandlerMethods();
assertThat(map).hasSize(1);
CompositeMessageCondition condition = map.entrySet().iterator().next().getKey();
RSocketFrameTypeMessageCondition c1 = condition.getCondition(RSocketFrameTypeMessageCondition.class);
assertThat(c1.getFrameTypes()).contains(FrameType.SETUP, FrameType.METADATA_PUSH);
DestinationPatternsMessageCondition c2 = condition.getCondition(DestinationPatternsMessageCondition.class);
if (ObjectUtils.isEmpty(expectedPatterns)) {
assertThat(c2.getPatterns()).isEmpty();
} else {
assertThat(c2.getPatterns()).contains(expectedPatterns);
}
}
use of org.springframework.messaging.handler.HandlerMethod in project spring-framework by spring-projects.
the class SimpAnnotationMethodMessageHandlerTests method errorAsMessageHandlingException.
@Test
public void errorAsMessageHandlingException() {
Message<?> message = createMessage("/pre/error");
this.messageHandler.registerHandler(this.testController);
this.messageHandler.handleMessage(message);
assertThat(this.testController.method).isEqualTo("handleErrorWithHandlerMethodArg");
HandlerMethod handlerMethod = (HandlerMethod) this.testController.arguments.get("handlerMethod");
assertThat(handlerMethod).isNotNull();
assertThat(handlerMethod.getMethod().getName()).isEqualTo("errorAsThrowable");
}
Aggregations