Skip to content

Commit 552e13c

Browse files
authored
GH-2642: Fix Race in KLABeanPostProcessor
Resolves #2642 Protect field `ListenerScope` from multi-threaded use (e.g. when creating prototype listeners). **cherry-pick to 2.9.x**
1 parent e4c1015 commit 552e13c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

spring-kafka/src/main/java/org/springframework/kafka/annotation/KafkaListenerAnnotationBeanPostProcessor.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2022 the original author or authors.
2+
* Copyright 2014-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -268,7 +268,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
268268
* {@link #setEndpointRegistry endpoint registry} has to be explicitly configured.
269269
* @param beanFactory the {@link BeanFactory} to be used.
270270
*/
271-
public void setBeanFactory(BeanFactory beanFactory) {
271+
public synchronized void setBeanFactory(BeanFactory beanFactory) {
272272
this.beanFactory = beanFactory;
273273
if (beanFactory instanceof ConfigurableListableBeanFactory clbf) {
274274
this.resolver = clbf.getBeanExpressionResolver();
@@ -449,8 +449,8 @@ private KafkaListener enhance(AnnotatedElement element, KafkaListener ann) {
449449
}
450450
}
451451

452-
private void processMultiMethodListeners(Collection<KafkaListener> classLevelListeners, List<Method> multiMethods,
453-
Object bean, String beanName) {
452+
private synchronized void processMultiMethodListeners(Collection<KafkaListener> classLevelListeners,
453+
List<Method> multiMethods, Object bean, String beanName) {
454454

455455
List<Method> checkedMethods = new ArrayList<>();
456456
Method defaultMethod = null;
@@ -477,7 +477,9 @@ private void processMultiMethodListeners(Collection<KafkaListener> classLevelLis
477477
}
478478
}
479479

480-
protected void processKafkaListener(KafkaListener kafkaListener, Method method, Object bean, String beanName) {
480+
protected synchronized void processKafkaListener(KafkaListener kafkaListener, Method method, Object bean,
481+
String beanName) {
482+
481483
Method methodToUse = checkProxy(method, bean);
482484
MethodKafkaListenerEndpoint<K, V> endpoint = new MethodKafkaListenerEndpoint<>();
483485
endpoint.setMethod(methodToUse);

0 commit comments

Comments
 (0)