diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java index 1aaa0da811..b28641a2a8 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -83,17 +83,13 @@ public abstract class AbstractMethodMessageHandler protected final Log logger = LogFactory.getLog(getClass()); - private Collection destinationPrefixes = new ArrayList(); - private final List customArgumentResolvers = new ArrayList(4); private final List customReturnValueHandlers = new ArrayList(4); - private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite(); + private final HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite(); - private HandlerMethodReturnValueHandlerComposite returnValueHandlers =new HandlerMethodReturnValueHandlerComposite(); - - private ApplicationContext applicationContext; + private final HandlerMethodReturnValueHandlerComposite returnValueHandlers =new HandlerMethodReturnValueHandlerComposite(); private final Map handlerMethods = new LinkedHashMap(); @@ -105,6 +101,16 @@ public abstract class AbstractMethodMessageHandler private final Map exceptionHandlerAdviceCache = new LinkedHashMap(64); + private Collection destinationPrefixes = new ArrayList(); + + private ApplicationContext applicationContext; + + /** + * Return the configured destination prefixes. + */ + public Collection getDestinationPrefixes() { + return this.destinationPrefixes; + } /** * When this property is configured only messages to destinations matching @@ -125,10 +131,10 @@ public abstract class AbstractMethodMessageHandler } /** - * Return the configured destination prefixes. + * Return the configured custom argument resolvers, if any. */ - public Collection getDestinationPrefixes() { - return this.destinationPrefixes; + public List getCustomArgumentResolvers() { + return this.customArgumentResolvers; } /** @@ -144,10 +150,10 @@ public abstract class AbstractMethodMessageHandler } /** - * Return the configured custom argument resolvers, if any. + * Return the configured custom return value handlers, if any. */ - public List getCustomArgumentResolvers() { - return this.customArgumentResolvers; + public List getCustomReturnValueHandlers() { + return this.customReturnValueHandlers; } /** @@ -163,10 +169,10 @@ public abstract class AbstractMethodMessageHandler } /** - * Return the configured custom return value handlers, if any. + * Return the configured argument resolvers, if any. */ - public List getCustomReturnValueHandlers() { - return this.customReturnValueHandlers; + public List getArgumentResolvers() { + return this.argumentResolvers.getResolvers(); } /** @@ -182,8 +188,11 @@ public abstract class AbstractMethodMessageHandler this.argumentResolvers.addResolvers(argumentResolvers); } - public List getArgumentResolvers() { - return this.argumentResolvers.getResolvers(); + /** + * Return the configured return value handlers, if any. + */ + public List getReturnValueHandlers() { + return this.returnValueHandlers.getReturnValueHandlers(); } /** @@ -199,15 +208,8 @@ public abstract class AbstractMethodMessageHandler this.returnValueHandlers.addHandlers(returnValueHandlers); } - public List getReturnValueHandlers() { - return this.returnValueHandlers.getReturnValueHandlers(); - } - - /** - * Return a map with all handler methods and their mappings. - */ - public Map getHandlerMethods() { - return Collections.unmodifiableMap(this.handlerMethods); + public ApplicationContext getApplicationContext() { + return this.applicationContext; } @Override @@ -215,11 +217,6 @@ public abstract class AbstractMethodMessageHandler this.applicationContext = applicationContext; } - public ApplicationContext getApplicationContext() { - return this.applicationContext; - } - - @Override public void afterPropertiesSet() { if (this.argumentResolvers.getResolvers().isEmpty()) { @@ -366,6 +363,13 @@ public abstract class AbstractMethodMessageHandler this.exceptionHandlerAdviceCache.put(bean, resolver); } + /** + * Return a map with all handler methods and their mappings. + */ + public Map getHandlerMethods() { + return Collections.unmodifiableMap(this.handlerMethods); + } + @Override public void handleMessage(Message message) throws MessagingException { @@ -427,14 +431,14 @@ public abstract class AbstractMethodMessageHandler addMatchesToCollection(allMappings, message, matches); } if (matches.isEmpty()) { - handleNoMatch(handlerMethods.keySet(), lookupDestination, message); + handleNoMatch(this.handlerMethods.keySet(), lookupDestination, message); return; } Comparator comparator = new MatchComparator(getMappingComparator(message)); Collections.sort(matches, comparator); if (logger.isTraceEnabled()) { - logger.trace("Found " + matches.size() + " methods: " + matches); + logger.trace("Found " + matches.size() + " handler methods: " + matches); } Match bestMatch = matches.get(0); @@ -451,7 +455,6 @@ public abstract class AbstractMethodMessageHandler handleMatch(bestMatch.mapping, bestMatch.handlerMethod, lookupDestination, message); } - private void addMatchesToCollection(Collection mappingsToCheck, Message message, List matches) { for (T mapping : mappingsToCheck) { T match = getMatchingMapping(mapping, message); @@ -470,6 +473,10 @@ public abstract class AbstractMethodMessageHandler */ protected abstract T getMatchingMapping(T mapping, Message message); + protected void handleNoMatch(Set ts, String lookupDestination, Message message) { + logger.debug("No matching message handler methods."); + } + /** * Return a comparator for sorting matching mappings. * The returned comparator should sort 'better' matches higher. @@ -478,7 +485,6 @@ public abstract class AbstractMethodMessageHandler */ protected abstract Comparator getMappingComparator(Message message); - protected void handleMatch(T mapping, HandlerMethod handlerMethod, String lookupDestination, Message message) { if (logger.isDebugEnabled()) { logger.debug("Invoking " + handlerMethod.getShortLogMessage()); @@ -515,7 +521,7 @@ public abstract class AbstractMethodMessageHandler protected void processHandlerMethodException(HandlerMethod handlerMethod, Exception ex, Message message) { InvocableHandlerMethod invocable = getExceptionHandlerMethod(handlerMethod, ex); if (invocable == null) { - logger.error("Unhandled exception", ex); + logger.error("Unhandled exception from message handler method", ex); return; } invocable.setMessageMethodArgumentResolvers(this.argumentResolvers); @@ -535,8 +541,6 @@ public abstract class AbstractMethodMessageHandler } } - protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor(Class beanType); - /** * Find an {@code @MessageExceptionHandler} method for the given exception. * The default implementation searches methods in the class hierarchy of the @@ -575,9 +579,9 @@ public abstract class AbstractMethodMessageHandler return null; } - protected void handleNoMatch(Set ts, String lookupDestination, Message message) { - logger.debug("No matching methods."); - } + protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor( + Class beanType); + @Override public String toString() { @@ -596,7 +600,7 @@ public abstract class AbstractMethodMessageHandler private final HandlerMethod handlerMethod; - private Match(T mapping, HandlerMethod handlerMethod) { + public Match(T mapping, HandlerMethod handlerMethod) { this.mapping = mapping; this.handlerMethod = handlerMethod; } @@ -622,13 +626,13 @@ public abstract class AbstractMethodMessageHandler } } + private class ReturnValueListenableFutureCallback implements ListenableFutureCallback { private final InvocableHandlerMethod handlerMethod; private final Message message; - public ReturnValueListenableFutureCallback(InvocableHandlerMethod handlerMethod, Message message) { this.handlerMethod = handlerMethod; this.message = message; @@ -651,7 +655,7 @@ public abstract class AbstractMethodMessageHandler } private void handleFailure(Throwable ex) { - Exception cause = (ex instanceof Exception ? (Exception) ex : new RuntimeException(ex)); + Exception cause = (ex instanceof Exception ? (Exception) ex : new IllegalStateException(ex)); processHandlerMethodException(this.handlerMethod, cause, this.message); } }