Skip to content

Commit 576e854

Browse files
cenkakinartembilan
authored andcommitted
GH-2554: Fix RetryTopicBeanNames duplications
Fixes #2557 * Remove new IT and modify RetryTopicSameContainerFactoryIntegrationTests instead * Re-add BasicKafkaListener # Conflicts: # spring-kafka/src/test/java/org/springframework/kafka/retrytopic/RetryTopicSameContainerFactoryIntegrationTests.java
1 parent 4b45d2b commit 576e854

File tree

2 files changed

+55
-22
lines changed

2 files changed

+55
-22
lines changed

spring-kafka/src/main/java/org/springframework/kafka/retrytopic/RetryTopicBeanNames.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-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.
@@ -62,6 +62,6 @@ private RetryTopicBeanNames() {
6262
* The bean name of the internally registered scheduler wrapper, if needed.
6363
*/
6464
public static final String DEFAULT_SCHEDULER_WRAPPER_BEAN_NAME =
65-
"defaultRetryTopicKafkaTemplate";
65+
"defaultRetryTopicSchedulerWrapper";
6666

6767
}

spring-kafka/src/test/java/org/springframework/kafka/retrytopic/RetryTopicSameContainerFactoryIntegrationTests.java

+53-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2022 the original author or authors.
2+
* Copyright 2021-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.
@@ -18,6 +18,7 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.fail;
21+
import static org.mockito.Mockito.spy;
2122

2223
import java.util.HashMap;
2324
import java.util.Map;
@@ -52,21 +53,20 @@
5253
import org.springframework.kafka.test.context.EmbeddedKafka;
5354
import org.springframework.messaging.handler.annotation.Header;
5455
import org.springframework.retry.annotation.Backoff;
55-
import org.springframework.scheduling.TaskScheduler;
56-
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
5756
import org.springframework.stereotype.Component;
5857
import org.springframework.test.annotation.DirtiesContext;
5958
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
6059
import org.springframework.util.backoff.FixedBackOff;
6160

6261
/**
6362
* @author Tomaz Fernandes
63+
* @author Cenk Akin
6464
* @since 2.8.3
6565
*/
6666
@SpringJUnitConfig
6767
@DirtiesContext
6868
@EmbeddedKafka(topics = { RetryTopicSameContainerFactoryIntegrationTests.FIRST_TOPIC,
69-
RetryTopicSameContainerFactoryIntegrationTests.SECOND_TOPIC}, partitions = 1)
69+
RetryTopicSameContainerFactoryIntegrationTests.SECOND_TOPIC, RetryTopicSameContainerFactoryIntegrationTests.THIRD_TOPIC}, partitions = 1)
7070
public class RetryTopicSameContainerFactoryIntegrationTests {
7171

7272
private static final Logger logger = LoggerFactory.getLogger(RetryTopicSameContainerFactoryIntegrationTests.class);
@@ -75,6 +75,8 @@ public class RetryTopicSameContainerFactoryIntegrationTests {
7575

7676
public final static String SECOND_TOPIC = "myRetryTopic2";
7777

78+
public final static String THIRD_TOPIC = "myRetryTopic3";
79+
7880
@Autowired
7981
private KafkaTemplate<String, String> sendKafkaTemplate;
8082

@@ -87,9 +89,13 @@ void shouldRetryFirstAndSecondTopics() {
8789
sendKafkaTemplate.send(FIRST_TOPIC, "Testing topic 1");
8890
logger.debug("Sending message to topic " + SECOND_TOPIC);
8991
sendKafkaTemplate.send(SECOND_TOPIC, "Testing topic 2");
90-
assertThat(awaitLatch(latchContainer.countDownLatch1)).isTrue();
92+
logger.debug("Sending message to topic " + THIRD_TOPIC);
93+
sendKafkaTemplate.send(THIRD_TOPIC, "Testing topic 3");
94+
assertThat(awaitLatch(latchContainer.countDownLatchFirstRetryable)).isTrue();
9195
assertThat(awaitLatch(latchContainer.countDownLatchDltOne)).isTrue();
92-
assertThat(awaitLatch(latchContainer.countDownLatch2)).isTrue();
96+
assertThat(awaitLatch(latchContainer.countDownLatchSecondRetryable)).isTrue();
97+
assertThat(awaitLatch(latchContainer.countDownLatchDltSecond)).isTrue();
98+
assertThat(awaitLatch(latchContainer.countDownLatchBasic)).isTrue();
9399
assertThat(awaitLatch(latchContainer.customizerLatch)).isTrue();
94100
}
95101

@@ -104,7 +110,7 @@ private boolean awaitLatch(CountDownLatch latch) {
104110
}
105111

106112
@Component
107-
static class RetryableKafkaListener {
113+
static class FirstRetryableKafkaListener {
108114

109115
@Autowired
110116
CountDownLatchContainer countDownLatchContainer;
@@ -116,9 +122,9 @@ static class RetryableKafkaListener {
116122
topicSuffixingStrategy = TopicSuffixingStrategy.SUFFIX_WITH_INDEX_VALUE)
117123
@KafkaListener(topics = RetryTopicSameContainerFactoryIntegrationTests.FIRST_TOPIC)
118124
public void listen(String in, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
119-
countDownLatchContainer.countDownLatch1.countDown();
125+
countDownLatchContainer.countDownLatchFirstRetryable.countDown();
120126
logger.warn(in + " from " + topic);
121-
throw new RuntimeException("test");
127+
throw new RuntimeException("from FirstRetryableKafkaListener");
122128
}
123129

124130
@DltHandler
@@ -129,30 +135,52 @@ public void dlt(String in, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
129135
}
130136

131137
@Component
132-
static class BasicKafkaListener {
138+
static class SecondRetryableKafkaListener {
133139

134140
@Autowired
135141
CountDownLatchContainer countDownLatchContainer;
136142

143+
@RetryableTopic
137144
@KafkaListener(topics = RetryTopicSameContainerFactoryIntegrationTests.SECOND_TOPIC)
145+
public void listen(String in, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
146+
countDownLatchContainer.countDownLatchSecondRetryable.countDown();
147+
logger.info(in + " from " + topic);
148+
throw new RuntimeException("from SecondRetryableKafkaListener");
149+
}
150+
151+
@DltHandler
152+
public void dlt(String in, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
153+
countDownLatchContainer.countDownLatchDltSecond.countDown();
154+
logger.warn(in + " from " + topic);
155+
}
156+
}
157+
158+
159+
@Component
160+
static class BasicKafkaListener {
161+
162+
@KafkaListener(topics = RetryTopicSameContainerFactoryIntegrationTests.THIRD_TOPIC)
138163
public void listen(String in, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
139164
logger.info(in + " from " + topic);
140-
throw new RuntimeException("another test");
165+
throw new RuntimeException("from BasicKafkaListener");
141166
}
142167
}
143168

144169
@Component
145170
static class CountDownLatchContainer {
146171

147-
CountDownLatch countDownLatch1 = new CountDownLatch(4);
148-
CountDownLatch countDownLatch2 = new CountDownLatch(1);
172+
CountDownLatch countDownLatchFirstRetryable = new CountDownLatch(4);
173+
CountDownLatch countDownLatchSecondRetryable = new CountDownLatch(3);
149174
CountDownLatch countDownLatchDltOne = new CountDownLatch(1);
150-
CountDownLatch customizerLatch = new CountDownLatch(6);
175+
CountDownLatch countDownLatchDltSecond = new CountDownLatch(1);
176+
177+
CountDownLatch countDownLatchBasic = new CountDownLatch(1);
178+
CountDownLatch customizerLatch = new CountDownLatch(10);
151179
}
152180

153181
@EnableKafka
154182
@Configuration
155-
static class Config extends RetryTopicConfigurationSupport {
183+
static class Config {
156184

157185
@Autowired
158186
EmbeddedKafkaBroker broker;
@@ -163,8 +191,13 @@ CountDownLatchContainer latchContainer() {
163191
}
164192

165193
@Bean
166-
RetryableKafkaListener retryableKafkaListener() {
167-
return new RetryableKafkaListener();
194+
FirstRetryableKafkaListener firstRetryableKafkaListener() {
195+
return new FirstRetryableKafkaListener();
196+
}
197+
198+
@Bean
199+
SecondRetryableKafkaListener secondRetryableKafkaListener() {
200+
return new SecondRetryableKafkaListener();
168201
}
169202

170203
@Bean
@@ -184,7 +217,7 @@ public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerCont
184217
props.setIdlePartitionEventInterval(100L);
185218
factory.setConsumerFactory(consumerFactory);
186219
DefaultErrorHandler errorHandler = new DefaultErrorHandler(
187-
(cr, ex) -> latchContainer.countDownLatch2.countDown(),
220+
(cr, ex) -> latchContainer.countDownLatchBasic.countDown(),
188221
new FixedBackOff(0, 2));
189222
factory.setCommonErrorHandler(errorHandler);
190223
factory.setConcurrency(1);
@@ -236,8 +269,8 @@ public ConsumerFactory<String, String> consumerFactory() {
236269
}
237270

238271
@Bean
239-
TaskScheduler sched() {
240-
return new ThreadPoolTaskScheduler();
272+
RetryTopicComponentFactory componentFactory() {
273+
return spy(new RetryTopicComponentFactory());
241274
}
242275

243276
}

0 commit comments

Comments
 (0)