1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -83,17 +83,13 @@ public abstract class AbstractMethodMessageHandler<T>
83
83
84
84
protected final Log logger = LogFactory .getLog (getClass ());
85
85
86
- private Collection <String > destinationPrefixes = new ArrayList <String >();
87
-
88
86
private final List <HandlerMethodArgumentResolver > customArgumentResolvers = new ArrayList <HandlerMethodArgumentResolver >(4 );
89
87
90
88
private final List <HandlerMethodReturnValueHandler > customReturnValueHandlers = new ArrayList <HandlerMethodReturnValueHandler >(4 );
91
89
92
- private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite ();
90
+ private final HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite ();
93
91
94
- private HandlerMethodReturnValueHandlerComposite returnValueHandlers =new HandlerMethodReturnValueHandlerComposite ();
95
-
96
- private ApplicationContext applicationContext ;
92
+ private final HandlerMethodReturnValueHandlerComposite returnValueHandlers =new HandlerMethodReturnValueHandlerComposite ();
97
93
98
94
private final Map <T , HandlerMethod > handlerMethods = new LinkedHashMap <T , HandlerMethod >();
99
95
@@ -105,6 +101,16 @@ public abstract class AbstractMethodMessageHandler<T>
105
101
private final Map <MessagingAdviceBean , AbstractExceptionHandlerMethodResolver > exceptionHandlerAdviceCache =
106
102
new LinkedHashMap <MessagingAdviceBean , AbstractExceptionHandlerMethodResolver >(64 );
107
103
104
+ private Collection <String > destinationPrefixes = new ArrayList <String >();
105
+
106
+ private ApplicationContext applicationContext ;
107
+
108
+ /**
109
+ * Return the configured destination prefixes.
110
+ */
111
+ public Collection <String > getDestinationPrefixes () {
112
+ return this .destinationPrefixes ;
113
+ }
108
114
109
115
/**
110
116
* When this property is configured only messages to destinations matching
@@ -125,10 +131,10 @@ public void setDestinationPrefixes(Collection<String> prefixes) {
125
131
}
126
132
127
133
/**
128
- * Return the configured destination prefixes .
134
+ * Return the configured custom argument resolvers, if any .
129
135
*/
130
- public Collection < String > getDestinationPrefixes () {
131
- return this .destinationPrefixes ;
136
+ public List < HandlerMethodArgumentResolver > getCustomArgumentResolvers () {
137
+ return this .customArgumentResolvers ;
132
138
}
133
139
134
140
/**
@@ -144,10 +150,10 @@ public void setCustomArgumentResolvers(List<HandlerMethodArgumentResolver> custo
144
150
}
145
151
146
152
/**
147
- * Return the configured custom argument resolvers , if any.
153
+ * Return the configured custom return value handlers , if any.
148
154
*/
149
- public List <HandlerMethodArgumentResolver > getCustomArgumentResolvers () {
150
- return this .customArgumentResolvers ;
155
+ public List <HandlerMethodReturnValueHandler > getCustomReturnValueHandlers () {
156
+ return this .customReturnValueHandlers ;
151
157
}
152
158
153
159
/**
@@ -163,10 +169,10 @@ public void setCustomReturnValueHandlers(List<HandlerMethodReturnValueHandler> c
163
169
}
164
170
165
171
/**
166
- * Return the configured custom return value handlers , if any.
172
+ * Return the configured argument resolvers , if any.
167
173
*/
168
- public List <HandlerMethodReturnValueHandler > getCustomReturnValueHandlers () {
169
- return this .customReturnValueHandlers ;
174
+ public List <HandlerMethodArgumentResolver > getArgumentResolvers () {
175
+ return this .argumentResolvers . getResolvers () ;
170
176
}
171
177
172
178
/**
@@ -182,8 +188,11 @@ public void setArgumentResolvers(List<HandlerMethodArgumentResolver> argumentRes
182
188
this .argumentResolvers .addResolvers (argumentResolvers );
183
189
}
184
190
185
- public List <HandlerMethodArgumentResolver > getArgumentResolvers () {
186
- return this .argumentResolvers .getResolvers ();
191
+ /**
192
+ * Return the configured return value handlers, if any.
193
+ */
194
+ public List <HandlerMethodReturnValueHandler > getReturnValueHandlers () {
195
+ return this .returnValueHandlers .getReturnValueHandlers ();
187
196
}
188
197
189
198
/**
@@ -199,27 +208,15 @@ public void setReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnV
199
208
this .returnValueHandlers .addHandlers (returnValueHandlers );
200
209
}
201
210
202
- public List <HandlerMethodReturnValueHandler > getReturnValueHandlers () {
203
- return this .returnValueHandlers .getReturnValueHandlers ();
204
- }
205
-
206
- /**
207
- * Return a map with all handler methods and their mappings.
208
- */
209
- public Map <T , HandlerMethod > getHandlerMethods () {
210
- return Collections .unmodifiableMap (this .handlerMethods );
211
+ public ApplicationContext getApplicationContext () {
212
+ return this .applicationContext ;
211
213
}
212
214
213
215
@ Override
214
216
public void setApplicationContext (ApplicationContext applicationContext ) {
215
217
this .applicationContext = applicationContext ;
216
218
}
217
219
218
- public ApplicationContext getApplicationContext () {
219
- return this .applicationContext ;
220
- }
221
-
222
-
223
220
@ Override
224
221
public void afterPropertiesSet () {
225
222
if (this .argumentResolvers .getResolvers ().isEmpty ()) {
@@ -366,6 +363,13 @@ protected void registerExceptionHandlerAdvice(MessagingAdviceBean bean, Abstract
366
363
this .exceptionHandlerAdviceCache .put (bean , resolver );
367
364
}
368
365
366
+ /**
367
+ * Return a map with all handler methods and their mappings.
368
+ */
369
+ public Map <T , HandlerMethod > getHandlerMethods () {
370
+ return Collections .unmodifiableMap (this .handlerMethods );
371
+ }
372
+
369
373
370
374
@ Override
371
375
public void handleMessage (Message <?> message ) throws MessagingException {
@@ -427,14 +431,14 @@ protected void handleMessageInternal(Message<?> message, String lookupDestinatio
427
431
addMatchesToCollection (allMappings , message , matches );
428
432
}
429
433
if (matches .isEmpty ()) {
430
- handleNoMatch (handlerMethods .keySet (), lookupDestination , message );
434
+ handleNoMatch (this . handlerMethods .keySet (), lookupDestination , message );
431
435
return ;
432
436
}
433
437
Comparator <Match > comparator = new MatchComparator (getMappingComparator (message ));
434
438
Collections .sort (matches , comparator );
435
439
436
440
if (logger .isTraceEnabled ()) {
437
- logger .trace ("Found " + matches .size () + " methods: " + matches );
441
+ logger .trace ("Found " + matches .size () + " handler methods: " + matches );
438
442
}
439
443
440
444
Match bestMatch = matches .get (0 );
@@ -451,7 +455,6 @@ protected void handleMessageInternal(Message<?> message, String lookupDestinatio
451
455
handleMatch (bestMatch .mapping , bestMatch .handlerMethod , lookupDestination , message );
452
456
}
453
457
454
-
455
458
private void addMatchesToCollection (Collection <T > mappingsToCheck , Message <?> message , List <Match > matches ) {
456
459
for (T mapping : mappingsToCheck ) {
457
460
T match = getMatchingMapping (mapping , message );
@@ -470,6 +473,10 @@ private void addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> me
470
473
*/
471
474
protected abstract T getMatchingMapping (T mapping , Message <?> message );
472
475
476
+ protected void handleNoMatch (Set <T > ts , String lookupDestination , Message <?> message ) {
477
+ logger .debug ("No matching message handler methods." );
478
+ }
479
+
473
480
/**
474
481
* Return a comparator for sorting matching mappings.
475
482
* The returned comparator should sort 'better' matches higher.
@@ -478,7 +485,6 @@ private void addMatchesToCollection(Collection<T> mappingsToCheck, Message<?> me
478
485
*/
479
486
protected abstract Comparator <T > getMappingComparator (Message <?> message );
480
487
481
-
482
488
protected void handleMatch (T mapping , HandlerMethod handlerMethod , String lookupDestination , Message <?> message ) {
483
489
if (logger .isDebugEnabled ()) {
484
490
logger .debug ("Invoking " + handlerMethod .getShortLogMessage ());
@@ -515,7 +521,7 @@ protected void handleMatch(T mapping, HandlerMethod handlerMethod, String lookup
515
521
protected void processHandlerMethodException (HandlerMethod handlerMethod , Exception ex , Message <?> message ) {
516
522
InvocableHandlerMethod invocable = getExceptionHandlerMethod (handlerMethod , ex );
517
523
if (invocable == null ) {
518
- logger .error ("Unhandled exception" , ex );
524
+ logger .error ("Unhandled exception from message handler method " , ex );
519
525
return ;
520
526
}
521
527
invocable .setMessageMethodArgumentResolvers (this .argumentResolvers );
@@ -535,8 +541,6 @@ protected void processHandlerMethodException(HandlerMethod handlerMethod, Except
535
541
}
536
542
}
537
543
538
- protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor (Class <?> beanType );
539
-
540
544
/**
541
545
* Find an {@code @MessageExceptionHandler} method for the given exception.
542
546
* The default implementation searches methods in the class hierarchy of the
@@ -575,9 +579,9 @@ protected InvocableHandlerMethod getExceptionHandlerMethod(HandlerMethod handler
575
579
return null ;
576
580
}
577
581
578
- protected void handleNoMatch ( Set < T > ts , String lookupDestination , Message <?> message ) {
579
- logger . debug ( "No matching methods." );
580
- }
582
+ protected abstract AbstractExceptionHandlerMethodResolver createExceptionHandlerMethodResolverFor (
583
+ Class <?> beanType );
584
+
581
585
582
586
@ Override
583
587
public String toString () {
@@ -596,7 +600,7 @@ private class Match {
596
600
597
601
private final HandlerMethod handlerMethod ;
598
602
599
- private Match (T mapping , HandlerMethod handlerMethod ) {
603
+ public Match (T mapping , HandlerMethod handlerMethod ) {
600
604
this .mapping = mapping ;
601
605
this .handlerMethod = handlerMethod ;
602
606
}
@@ -622,13 +626,13 @@ public int compare(Match match1, Match match2) {
622
626
}
623
627
}
624
628
629
+
625
630
private class ReturnValueListenableFutureCallback implements ListenableFutureCallback <Object > {
626
631
627
632
private final InvocableHandlerMethod handlerMethod ;
628
633
629
634
private final Message <?> message ;
630
635
631
-
632
636
public ReturnValueListenableFutureCallback (InvocableHandlerMethod handlerMethod , Message <?> message ) {
633
637
this .handlerMethod = handlerMethod ;
634
638
this .message = message ;
@@ -651,7 +655,7 @@ public void onFailure(Throwable ex) {
651
655
}
652
656
653
657
private void handleFailure (Throwable ex ) {
654
- Exception cause = (ex instanceof Exception ? (Exception ) ex : new RuntimeException (ex ));
658
+ Exception cause = (ex instanceof Exception ? (Exception ) ex : new IllegalStateException (ex ));
655
659
processHandlerMethodException (this .handlerMethod , cause , this .message );
656
660
}
657
661
}
0 commit comments