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 ;
@@ -137,11 +138,12 @@ public DelegatingInvocableHandler(List<InvocableHandlerMethod> handlers,
137
138
@ Nullable BeanExpressionContext beanExpressionContext ,
138
139
@ Nullable BeanFactory beanFactory , @ Nullable Validator validator ) {
139
140
140
- this .handlers = new ArrayList <>();
141
+ this .handlers = new ArrayList <>(handlers );
141
142
for (InvocableHandlerMethod handler : handlers ) {
142
- this . handlers . add ( wrapIfNecessary ( handler ) );
143
+ checkSpecial ( handler );
143
144
}
144
- this .defaultHandler = wrapIfNecessary (defaultHandler );
145
+ this .defaultHandler = defaultHandler ;
146
+ checkSpecial (defaultHandler );
145
147
this .bean = bean ;
146
148
this .resolver = beanExpressionResolver ;
147
149
this .beanExpressionContext = beanExpressionContext ;
@@ -151,18 +153,17 @@ public DelegatingInvocableHandler(List<InvocableHandlerMethod> handlers,
151
153
this .validator = validator == null ? null : new PayloadValidator (validator );
152
154
}
153
155
154
- @ Nullable
155
- private InvocableHandlerMethod wrapIfNecessary (@ Nullable InvocableHandlerMethod handler ) {
156
+ private void checkSpecial (@ Nullable InvocableHandlerMethod handler ) {
156
157
if (handler == null ) {
157
- return null ;
158
+ return ;
158
159
}
159
160
Parameter [] parameters = handler .getMethod ().getParameters ();
160
161
for (Parameter parameter : parameters ) {
161
162
if (parameter .getType ().equals (ConsumerRecordMetadata .class )) {
162
- return new DelegatingInvocableHandler .MetadataAwareInvocableHandlerMethod (handler );
163
+ this .handlerMetadataAware .put (handler , true );
164
+ return ;
163
165
}
164
166
}
165
- return handler ;
166
167
}
167
168
168
169
/**
@@ -191,7 +192,7 @@ public Object invoke(Message<?> message, Object... providedArgs) throws Exceptio
191
192
}
192
193
}
193
194
Object result ;
194
- if (handler instanceof MetadataAwareInvocableHandlerMethod ) {
195
+ if (Boolean . TRUE . equals ( this . handlerMetadataAware . get ( handler )) ) {
195
196
Object [] args = new Object [providedArgs .length + 1 ];
196
197
args [0 ] = AdapterUtils .buildConsumerRecordMetadataFromArray (providedArgs );
197
198
System .arraycopy (providedArgs , 0 , args , 1 , providedArgs .length );
@@ -350,19 +351,6 @@ public boolean hasDefaultHandler() {
350
351
return this .defaultHandler != null ;
351
352
}
352
353
353
- /**
354
- * A handler method that is aware of {@link ConsumerRecordMetadata}.
355
- *
356
- * @since 2.5
357
- */
358
- private static final class MetadataAwareInvocableHandlerMethod extends InvocableHandlerMethod {
359
-
360
- MetadataAwareInvocableHandlerMethod (HandlerMethod handlerMethod ) {
361
- super (handlerMethod );
362
- }
363
-
364
- }
365
-
366
354
private static final class PayloadValidator extends PayloadMethodArgumentResolver {
367
355
368
356
PayloadValidator (Validator validator ) {
0 commit comments