Search in sources :

Example 1 with MessagingAdviceBean

use of org.springframework.messaging.handler.MessagingAdviceBean in project spring-framework by spring-projects.

the class WebSocketAnnotationMethodMessageHandler method initMessagingAdviceCache.

private void initMessagingAdviceCache(List<MessagingAdviceBean> beans) {
    if (beans == null) {
        return;
    }
    for (MessagingAdviceBean bean : beans) {
        Class<?> type = bean.getBeanType();
        AnnotationExceptionHandlerMethodResolver resolver = new AnnotationExceptionHandlerMethodResolver(type);
        if (resolver.hasExceptionMappings()) {
            registerExceptionHandlerAdvice(bean, resolver);
            logger.info("Detected @MessageExceptionHandler methods in " + bean);
        }
    }
}
Also used : MessagingAdviceBean(org.springframework.messaging.handler.MessagingAdviceBean) AnnotationExceptionHandlerMethodResolver(org.springframework.messaging.handler.annotation.support.AnnotationExceptionHandlerMethodResolver)

Example 2 with MessagingAdviceBean

use of org.springframework.messaging.handler.MessagingAdviceBean 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 3 with MessagingAdviceBean

use of org.springframework.messaging.handler.MessagingAdviceBean in project spring-framework by spring-projects.

the class InvocableHelper method initExceptionHandlerMethod.

/**
 * Find an exception handling method for the given exception.
 * <p>The default implementation searches methods in the class hierarchy of
 * the HandlerMethod first and if not found, it continues searching for
 * additional handling methods registered via
 * {@link #registerExceptionHandlerAdvice}.
 * @param handlerMethod the method where the exception was raised
 * @param ex the exception raised or signaled
 * @return a method to handle the exception, or {@code null}
 */
@Nullable
public InvocableHandlerMethod initExceptionHandlerMethod(HandlerMethod handlerMethod, Throwable ex) {
    if (logger.isDebugEnabled()) {
        logger.debug("Searching for methods to handle " + ex.getClass().getSimpleName());
    }
    Class<?> beanType = handlerMethod.getBeanType();
    AbstractExceptionHandlerMethodResolver resolver = this.exceptionHandlerCache.get(beanType);
    if (resolver == null) {
        resolver = this.exceptionMethodResolverFactory.apply(beanType);
        this.exceptionHandlerCache.put(beanType, resolver);
    }
    InvocableHandlerMethod exceptionHandlerMethod = null;
    Method method = resolver.resolveMethod(ex);
    if (method != null) {
        exceptionHandlerMethod = new InvocableHandlerMethod(handlerMethod.getBean(), method);
    } else {
        for (Map.Entry<MessagingAdviceBean, AbstractExceptionHandlerMethodResolver> entry : this.exceptionHandlerAdviceCache.entrySet()) {
            MessagingAdviceBean advice = entry.getKey();
            if (advice.isApplicableToBeanType(beanType)) {
                resolver = entry.getValue();
                method = resolver.resolveMethod(ex);
                if (method != null) {
                    exceptionHandlerMethod = new InvocableHandlerMethod(advice.resolveBean(), method);
                    break;
                }
            }
        }
    }
    if (exceptionHandlerMethod != null) {
        logger.debug("Found exception handler " + exceptionHandlerMethod.getShortLogMessage());
        exceptionHandlerMethod.setArgumentResolvers(this.argumentResolvers.getResolvers());
    } else {
        logger.error("No exception handling method", ex);
    }
    return exceptionHandlerMethod;
}
Also used : AbstractExceptionHandlerMethodResolver(org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver) HandlerMethod(org.springframework.messaging.handler.HandlerMethod) Method(java.lang.reflect.Method) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MessagingAdviceBean(org.springframework.messaging.handler.MessagingAdviceBean) Nullable(org.springframework.lang.Nullable)

Aggregations

MessagingAdviceBean (org.springframework.messaging.handler.MessagingAdviceBean)3 Method (java.lang.reflect.Method)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Nullable (org.springframework.lang.Nullable)2 HandlerMethod (org.springframework.messaging.handler.HandlerMethod)2 AnnotationExceptionHandlerMethodResolver (org.springframework.messaging.handler.annotation.support.AnnotationExceptionHandlerMethodResolver)1 AbstractExceptionHandlerMethodResolver (org.springframework.messaging.handler.invocation.AbstractExceptionHandlerMethodResolver)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 MultiValueMap (org.springframework.util.MultiValueMap)1