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