@@ -239,72 +239,71 @@ public RetryTopicConfigurer(DestinationTopicProcessor destinationTopicProcessor,
239
239
/**
240
240
* Entrypoint for creating and configuring the retry and dlt endpoints, as well as the
241
241
* container factory that will create the corresponding listenerContainer.
242
- *
243
242
* @param endpointProcessor function that will process the endpoints
244
243
* processListener method.
245
244
* @param mainEndpoint the endpoint based on which retry and dlt endpoints are also
246
245
* created and processed.
247
246
* @param configuration the configuration for the topic.
248
- * @param registrar The {@link KafkaListenerEndpointRegistrar} that will register the
249
- * endpoints.
247
+ * @param registrar The {@link KafkaListenerEndpointRegistrar} that will register the endpoints.
250
248
* @param factory The factory provided in the {@link org.springframework.kafka.annotation.KafkaListener}
251
- * annotation, if any.
249
+ * @param defaultContainerFactoryBeanName The default factory bean name for the
250
+ * {@link org.springframework.kafka.annotation.KafkaListener}
252
251
*
253
252
*/
254
253
public void processMainAndRetryListeners (EndpointProcessor endpointProcessor ,
255
254
MethodKafkaListenerEndpoint <?, ?> mainEndpoint ,
256
255
RetryTopicConfiguration configuration ,
257
256
KafkaListenerEndpointRegistrar registrar ,
258
- @ Nullable KafkaListenerContainerFactory <?> factory ) {
257
+ @ Nullable KafkaListenerContainerFactory <?> factory ,
258
+ String defaultContainerFactoryBeanName ) {
259
259
throwIfMultiMethodEndpoint (mainEndpoint );
260
- configureMainEndpoint (mainEndpoint , endpointProcessor , registrar , factory , configuration );
261
260
DestinationTopicProcessor .Context context =
262
261
new DestinationTopicProcessor .Context (configuration .getDestinationTopicProperties ());
263
- configureRetryAndDltEndpoints (mainEndpoint , endpointProcessor , factory , registrar , configuration , context );
262
+ configureEndpoints (mainEndpoint , endpointProcessor , factory , registrar , configuration , context ,
263
+ defaultContainerFactoryBeanName );
264
264
this .destinationTopicProcessor .processRegisteredDestinations (getTopicCreationFunction (configuration ), context );
265
265
}
266
266
267
- private void configureMainEndpoint (MethodKafkaListenerEndpoint <?, ?> mainEndpoint ,
267
+ private void configureEndpoints (MethodKafkaListenerEndpoint <?, ?> mainEndpoint ,
268
268
EndpointProcessor endpointProcessor ,
269
- KafkaListenerEndpointRegistrar registrar ,
270
269
KafkaListenerContainerFactory <?> factory ,
271
- RetryTopicConfiguration configuration ) {
272
- ConcurrentKafkaListenerContainerFactory <?, ?> resolvedFactory =
273
- resolveAndConfigureFactoryForMainEndpoint (factory , configuration );
274
- endpointProcessor .process (mainEndpoint );
275
- registrar .registerEndpoint (mainEndpoint , resolvedFactory );
276
- mainEndpoint .setBeanFactory (this .beanFactory );
277
- }
278
-
279
- private void configureRetryAndDltEndpoints (MethodKafkaListenerEndpoint <?, ?> mainEndpoint ,
280
- EndpointProcessor endpointProcessor ,
281
- KafkaListenerContainerFactory <?> factory ,
282
- KafkaListenerEndpointRegistrar registrar ,
283
- RetryTopicConfiguration configuration ,
284
- DestinationTopicProcessor .Context context ) {
285
- ConcurrentKafkaListenerContainerFactory <?, ?> resolvedFactory =
286
- resolveAndConfigureFactoryForRetryEndpoint (factory , configuration );
287
-
270
+ KafkaListenerEndpointRegistrar registrar ,
271
+ RetryTopicConfiguration configuration ,
272
+ DestinationTopicProcessor .Context context ,
273
+ String defaultContainerFactoryBeanName ) {
288
274
this .destinationTopicProcessor
289
275
.processDestinationTopicProperties (destinationTopicProperties ->
290
- processAndRegisterRetryDltDestination (mainEndpoint ,
276
+ processAndRegisterEndpoints (mainEndpoint ,
291
277
endpointProcessor ,
292
- resolvedFactory ,
278
+ factory ,
279
+ defaultContainerFactoryBeanName ,
293
280
registrar ,
294
281
configuration ,
295
282
context ,
296
283
destinationTopicProperties ),
297
284
context );
298
285
}
299
286
300
- private void processAndRegisterRetryDltDestination (MethodKafkaListenerEndpoint <?, ?> mainEndpoint , EndpointProcessor endpointProcessor ,
301
- KafkaListenerContainerFactory <?> resolvedFactory , KafkaListenerEndpointRegistrar registrar ,
302
- RetryTopicConfiguration configuration , DestinationTopicProcessor .Context context ,
303
- DestinationTopic .Properties destinationTopicProperties ) {
304
- MethodKafkaListenerEndpoint <Object , Object > endpoint = new MethodKafkaListenerEndpoint <>();
287
+ private void processAndRegisterEndpoints (MethodKafkaListenerEndpoint <?, ?> mainEndpoint , EndpointProcessor endpointProcessor ,
288
+ KafkaListenerContainerFactory <?> factory ,
289
+ String defaultFactoryBeanName ,
290
+ KafkaListenerEndpointRegistrar registrar ,
291
+ RetryTopicConfiguration configuration , DestinationTopicProcessor .Context context ,
292
+ DestinationTopic .Properties destinationTopicProperties ) {
293
+
294
+ ConcurrentKafkaListenerContainerFactory <?, ?> resolvedFactory =
295
+ destinationTopicProperties .isMainEndpoint ()
296
+ ? resolveAndConfigureFactoryForMainEndpoint (factory , defaultFactoryBeanName , configuration )
297
+ : resolveAndConfigureFactoryForRetryEndpoint (factory , defaultFactoryBeanName , configuration );
298
+
299
+ MethodKafkaListenerEndpoint <?, ?> endpoint = destinationTopicProperties .isMainEndpoint ()
300
+ ? mainEndpoint
301
+ : new MethodKafkaListenerEndpoint <>();
302
+
305
303
endpointProcessor .accept (endpoint );
306
304
307
- EndpointHandlerMethod endpointBeanMethod = getEndpointHandlerMethod (mainEndpoint , configuration , destinationTopicProperties );
305
+ EndpointHandlerMethod endpointBeanMethod =
306
+ getEndpointHandlerMethod (mainEndpoint , configuration , destinationTopicProperties );
308
307
309
308
createEndpointCustomizer (endpointBeanMethod , destinationTopicProperties )
310
309
.customizeEndpointAndCollectTopics (endpoint )
@@ -319,7 +318,8 @@ private void processAndRegisterRetryDltDestination(MethodKafkaListenerEndpoint<?
319
318
}
320
319
321
320
private EndpointHandlerMethod getEndpointHandlerMethod (MethodKafkaListenerEndpoint <?, ?> mainEndpoint ,
322
- RetryTopicConfiguration configuration , DestinationTopic .Properties props ) {
321
+ RetryTopicConfiguration configuration ,
322
+ DestinationTopic .Properties props ) {
323
323
EndpointHandlerMethod dltHandlerMethod = configuration .getDltHandlerMethod ();
324
324
EndpointHandlerMethod retryBeanMethod = new EndpointHandlerMethod (mainEndpoint .getBean (), mainEndpoint .getMethod ());
325
325
return props .isDltTopic () ? getDltEndpointHandlerMethodOrDefault (dltHandlerMethod ) : retryBeanMethod ;
@@ -355,17 +355,20 @@ private EndpointHandlerMethod getDltEndpointHandlerMethodOrDefault(EndpointHandl
355
355
356
356
private ConcurrentKafkaListenerContainerFactory <?, ?> resolveAndConfigureFactoryForMainEndpoint (
357
357
KafkaListenerContainerFactory <?> providedFactory ,
358
- RetryTopicConfiguration configuration ) {
358
+ String defaultFactoryBeanName , RetryTopicConfiguration configuration ) {
359
359
360
360
return this .listenerContainerFactoryConfigurer
361
361
.configure (this .containerFactoryResolver .resolveFactoryForMainEndpoint (providedFactory ,
362
+ defaultFactoryBeanName ,
362
363
configuration .forContainerFactoryResolver ()));
363
364
}
364
365
365
366
private ConcurrentKafkaListenerContainerFactory <?, ?> resolveAndConfigureFactoryForRetryEndpoint (KafkaListenerContainerFactory <?> providedFactory ,
367
+ String defaultFactoryBeanName ,
366
368
RetryTopicConfiguration configuration ) {
367
369
return this .listenerContainerFactoryConfigurer
368
370
.configure (this .containerFactoryResolver .resolveFactoryForRetryEndpoint (providedFactory ,
371
+ defaultFactoryBeanName ,
369
372
configuration .forContainerFactoryResolver ()));
370
373
}
371
374
@@ -435,7 +438,8 @@ private EndpointCustomizer addSuffixesAndMethod(String topicSuffix, Object bean,
435
438
};
436
439
}
437
440
438
- private Collection <TopicNamesHolder > customizeAndRegisterTopics (Suffixer suffixer , MethodKafkaListenerEndpoint <?, ?> endpoint ) {
441
+ private Collection <TopicNamesHolder > customizeAndRegisterTopics (Suffixer suffixer ,
442
+ MethodKafkaListenerEndpoint <?, ?> endpoint ) {
439
443
return getTopics (endpoint )
440
444
.stream ()
441
445
.map (topic -> new TopicNamesHolder (topic , suffixer .maybeAddTo (topic )))
@@ -494,7 +498,8 @@ public EndpointHandlerMethod(Class<?> beanClass, String methodName) {
494
498
this .method = Arrays .stream (ReflectionUtils .getDeclaredMethods (beanClass ))
495
499
.filter (mthd -> mthd .getName ().equals (methodName ))
496
500
.findFirst ()
497
- .orElseThrow (() -> new IllegalArgumentException (String .format ("No method %s in class %s" , methodName , beanClass )));
501
+ .orElseThrow (() -> new IllegalArgumentException (
502
+ String .format ("No method %s in class %s" , methodName , beanClass )));
498
503
this .beanClass = beanClass ;
499
504
}
500
505
@@ -528,13 +533,12 @@ static class LoggingDltListenerHandlerMethod {
528
533
529
534
public void logMessage (Object message ) {
530
535
if (message instanceof ConsumerRecord ) {
531
- LOGGER .info (() -> "Received message in dlt listener: " + ListenerUtils .recordToString ((ConsumerRecord <?, ?>) message ));
536
+ LOGGER .info (() -> "Received message in dlt listener: "
537
+ + ListenerUtils .recordToString ((ConsumerRecord <?, ?>) message ));
532
538
}
533
539
else {
534
540
LOGGER .info (() -> "Received message in dlt listener." );
535
541
}
536
542
}
537
543
}
538
544
}
539
-
540
-
0 commit comments