Search in sources :

Example 1 with HandlerMethod

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;
}
Also used : Method(java.lang.reflect.Method) HandlerMethod(org.springframework.messaging.handler.HandlerMethod) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MessagingAdviceBean(org.springframework.messaging.handler.MessagingAdviceBean) Nullable(org.springframework.lang.Nullable)

Example 2 with HandlerMethod

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);
    }
}
Also used : HandlerMethod(org.springframework.messaging.handler.HandlerMethod)

Example 3 with HandlerMethod

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");
}
Also used : HandlerMethod(org.springframework.messaging.handler.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 4 with HandlerMethod

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);
    }
}
Also used : CompositeMessageCondition(org.springframework.messaging.handler.CompositeMessageCondition) DestinationPatternsMessageCondition(org.springframework.messaging.handler.DestinationPatternsMessageCondition) HandlerMethod(org.springframework.messaging.handler.HandlerMethod)

Example 5 with HandlerMethod

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");
}
Also used : HandlerMethod(org.springframework.messaging.handler.HandlerMethod) Test(org.junit.jupiter.api.Test)

Aggregations

HandlerMethod (org.springframework.messaging.handler.HandlerMethod)11 Test (org.junit.jupiter.api.Test)3 Nullable (org.springframework.lang.Nullable)3 Method (java.lang.reflect.Method)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ApplicationContext (org.springframework.context.ApplicationContext)2 MessagingAdviceBean (org.springframework.messaging.handler.MessagingAdviceBean)2 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CompositeMessageCondition (org.springframework.messaging.handler.CompositeMessageCondition)1 DestinationPatternsMessageCondition (org.springframework.messaging.handler.DestinationPatternsMessageCondition)1 AbstractExceptionHandlerMethodResolver (org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 MultiValueMap (org.springframework.util.MultiValueMap)1 RouteMatcher (org.springframework.util.RouteMatcher)1