40
40
import org .springframework .messaging .Message ;
41
41
import org .springframework .messaging .MessageHeaders ;
42
42
import org .springframework .messaging .converter .MessageConverter ;
43
- import org .springframework .messaging .handler .HandlerMethod ;
44
43
import org .springframework .messaging .handler .annotation .Header ;
45
44
import org .springframework .messaging .handler .annotation .SendTo ;
46
45
import org .springframework .messaging .handler .annotation .support .PayloadMethodArgumentResolver ;
@@ -75,6 +74,8 @@ public class DelegatingInvocableHandler {
75
74
76
75
private final Map <InvocableHandlerMethod , Boolean > handlerReturnsMessage = new ConcurrentHashMap <>();
77
76
77
+ private final Map <InvocableHandlerMethod , Boolean > handlerMetadataAware = new ConcurrentHashMap <>();
78
+
78
79
private final Object bean ;
79
80
80
81
private final BeanExpressionResolver resolver ;
@@ -102,11 +103,12 @@ public DelegatingInvocableHandler(List<InvocableHandlerMethod> handlers,
102
103
@ Nullable BeanExpressionContext beanExpressionContext ,
103
104
@ Nullable BeanFactory beanFactory , @ Nullable Validator validator ) {
104
105
105
- this .handlers = new ArrayList <>();
106
+ this .handlers = new ArrayList <>(handlers );
106
107
for (InvocableHandlerMethod handler : handlers ) {
107
- this . handlers . add ( wrapIfNecessary ( handler ) );
108
+ checkSpecial ( handler );
108
109
}
109
- this .defaultHandler = wrapIfNecessary (defaultHandler );
110
+ this .defaultHandler = defaultHandler ;
111
+ checkSpecial (defaultHandler );
110
112
this .bean = bean ;
111
113
this .resolver = beanExpressionResolver ;
112
114
this .beanExpressionContext = beanExpressionContext ;
@@ -116,18 +118,17 @@ public DelegatingInvocableHandler(List<InvocableHandlerMethod> handlers,
116
118
this .validator = validator == null ? null : new PayloadValidator (validator );
117
119
}
118
120
119
- @ Nullable
120
- private InvocableHandlerMethod wrapIfNecessary (@ Nullable InvocableHandlerMethod handler ) {
121
+ private void checkSpecial (@ Nullable InvocableHandlerMethod handler ) {
121
122
if (handler == null ) {
122
- return null ;
123
+ return ;
123
124
}
124
125
Parameter [] parameters = handler .getMethod ().getParameters ();
125
126
for (Parameter parameter : parameters ) {
126
127
if (parameter .getType ().equals (ConsumerRecordMetadata .class )) {
127
- return new DelegatingInvocableHandler .MetadataAwareInvocableHandlerMethod (handler );
128
+ this .handlerMetadataAware .put (handler , true );
129
+ return ;
128
130
}
129
131
}
130
- return handler ;
131
132
}
132
133
133
134
/**
@@ -156,7 +157,7 @@ public Object invoke(Message<?> message, Object... providedArgs) throws Exceptio
156
157
}
157
158
}
158
159
Object result ;
159
- if (handler instanceof MetadataAwareInvocableHandlerMethod ) {
160
+ if (Boolean . TRUE . equals ( this . handlerMetadataAware . get ( handler )) ) {
160
161
Object [] args = new Object [providedArgs .length + 1 ];
161
162
args [0 ] = AdapterUtils .buildConsumerRecordMetadataFromArray (providedArgs );
162
163
System .arraycopy (providedArgs , 0 , args , 1 , providedArgs .length );
@@ -315,19 +316,6 @@ public boolean hasDefaultHandler() {
315
316
return this .defaultHandler != null ;
316
317
}
317
318
318
- /**
319
- * A handler method that is aware of {@link ConsumerRecordMetadata}.
320
- *
321
- * @since 2.5
322
- */
323
- private static final class MetadataAwareInvocableHandlerMethod extends InvocableHandlerMethod {
324
-
325
- MetadataAwareInvocableHandlerMethod (HandlerMethod handlerMethod ) {
326
- super (handlerMethod );
327
- }
328
-
329
- }
330
-
331
319
private static final class PayloadValidator extends PayloadMethodArgumentResolver {
332
320
333
321
PayloadValidator (Validator validator ) {
0 commit comments