|
19 | 19 | import static org.assertj.core.api.Assertions.assertThat;
|
20 | 20 | import static org.assertj.core.api.Assertions.fail;
|
21 | 21 |
|
| 22 | +import java.lang.reflect.Method; |
22 | 23 | import java.util.ArrayList;
|
23 | 24 | import java.util.Arrays;
|
| 25 | +import java.util.Collection; |
24 | 26 | import java.util.Collections;
|
25 | 27 | import java.util.HashMap;
|
26 | 28 | import java.util.List;
|
27 | 29 | import java.util.Map;
|
28 | 30 | import java.util.concurrent.CountDownLatch;
|
29 | 31 | import java.util.concurrent.TimeUnit;
|
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | +import java.util.concurrent.atomic.AtomicReference; |
30 | 34 |
|
31 | 35 | import org.apache.kafka.clients.admin.AdminClientConfig;
|
| 36 | +import org.apache.kafka.clients.admin.NewTopic; |
| 37 | +import org.apache.kafka.clients.admin.TopicDescription; |
32 | 38 | import org.apache.kafka.clients.consumer.ConsumerConfig;
|
33 | 39 | import org.apache.kafka.clients.producer.ProducerConfig;
|
34 | 40 | import org.apache.kafka.common.serialization.StringDeserializer;
|
|
47 | 53 | import org.springframework.kafka.annotation.RetryableTopic;
|
48 | 54 | import org.springframework.kafka.annotation.TopicPartition;
|
49 | 55 | import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
|
| 56 | +import org.springframework.kafka.config.TopicBuilder; |
50 | 57 | import org.springframework.kafka.core.ConsumerFactory;
|
51 | 58 | import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
|
52 | 59 | import org.springframework.kafka.core.DefaultKafkaProducerFactory;
|
53 | 60 | import org.springframework.kafka.core.KafkaAdmin;
|
| 61 | +import org.springframework.kafka.core.KafkaAdmin.NewTopics; |
54 | 62 | import org.springframework.kafka.core.KafkaTemplate;
|
55 | 63 | import org.springframework.kafka.core.ProducerFactory;
|
56 | 64 | import org.springframework.kafka.listener.ContainerProperties;
|
@@ -133,11 +141,30 @@ void shouldRetrySecondTopic() {
|
133 | 141 | }
|
134 | 142 |
|
135 | 143 | @Test
|
136 |
| - void shouldRetryThirdTopicWithTimeout() { |
| 144 | + void shouldRetryThirdTopicWithTimeout(@Autowired KafkaAdmin admin) throws Exception { |
137 | 145 | logger.debug("Sending message to topic " + THIRD_TOPIC);
|
138 | 146 | kafkaTemplate.send(THIRD_TOPIC, "Testing topic 3");
|
139 | 147 | assertThat(awaitLatch(latchContainer.countDownLatch3)).isTrue();
|
140 | 148 | assertThat(awaitLatch(latchContainer.countDownLatchDltOne)).isTrue();
|
| 149 | + Map<String, TopicDescription> topics = admin.describeTopics(THIRD_TOPIC, THIRD_TOPIC + "-dlt", FOURTH_TOPIC); |
| 150 | + assertThat(topics.get(THIRD_TOPIC).partitions()).hasSize(2); |
| 151 | + assertThat(topics.get(THIRD_TOPIC + "-dlt").partitions()).hasSize(3); |
| 152 | + assertThat(topics.get(FOURTH_TOPIC).partitions()).hasSize(2); |
| 153 | + AtomicReference<Method> method = new AtomicReference<>(); |
| 154 | + org.springframework.util.ReflectionUtils.doWithMethods(KafkaAdmin.class, m -> { |
| 155 | + m.setAccessible(true); |
| 156 | + method.set(m); |
| 157 | + }, m -> m.getName().equals("newTopics")); |
| 158 | + @SuppressWarnings("unchecked") |
| 159 | + Collection<NewTopic> weededTopics = (Collection<NewTopic>) method.get().invoke(admin); |
| 160 | + AtomicInteger weeded = new AtomicInteger(); |
| 161 | + weededTopics.forEach(topic -> { |
| 162 | + if (topic.name().equals(THIRD_TOPIC) || topic.name().equals(FOURTH_TOPIC)) { |
| 163 | + assertThat(topic).isExactlyInstanceOf(NewTopic.class); |
| 164 | + weeded.incrementAndGet(); |
| 165 | + } |
| 166 | + }); |
| 167 | + assertThat(weeded.get()).isEqualTo(2); |
141 | 168 | }
|
142 | 169 |
|
143 | 170 | @Test
|
@@ -527,6 +554,16 @@ public KafkaAdmin kafkaAdmin() {
|
527 | 554 | return new KafkaAdmin(configs);
|
528 | 555 | }
|
529 | 556 |
|
| 557 | + @Bean |
| 558 | + public NewTopic topic() { |
| 559 | + return TopicBuilder.name(THIRD_TOPIC).partitions(2).replicas(1).build(); |
| 560 | + } |
| 561 | + |
| 562 | + @Bean |
| 563 | + public NewTopics topics() { |
| 564 | + return new NewTopics(TopicBuilder.name(FOURTH_TOPIC).partitions(2).replicas(1).build()); |
| 565 | + } |
| 566 | + |
530 | 567 | @Bean
|
531 | 568 | public ConsumerFactory<String, String> consumerFactory() {
|
532 | 569 | Map<String, Object> props = new HashMap<>();
|
|
0 commit comments