|
1 | 1 | /*
|
2 |
| - * Copyright 2019 the original author or authors. |
| 2 | + * Copyright 2019-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
26 | 26 |
|
27 | 27 | import org.apache.kafka.clients.admin.NewTopic;
|
28 | 28 | import org.apache.kafka.clients.consumer.ConsumerConfig;
|
| 29 | +import org.apache.kafka.clients.consumer.OffsetAndMetadata; |
29 | 30 | import org.apache.kafka.common.TopicPartition;
|
30 | 31 | import org.apache.kafka.common.serialization.StringDeserializer;
|
31 | 32 | import org.junit.Before;
|
@@ -67,7 +68,9 @@ public static void setUpTopics() {
|
67 | 68 | new NewTopic("topic1", 1, (short) 1),
|
68 | 69 | new NewTopic("topic2", 2, (short) 1),
|
69 | 70 | new NewTopic("topic3", 1, (short) 1),
|
70 |
| - new NewTopic("topic4", 2, (short) 1) |
| 71 | + new NewTopic("topic4", 2, (short) 1), |
| 72 | + new NewTopic("topic5", 1, (short) 1), |
| 73 | + new NewTopic("topic6", 1, (short) 1) |
71 | 74 | );
|
72 | 75 | }
|
73 | 76 |
|
@@ -212,6 +215,89 @@ public void testReadFromSinglePartition() {
|
212 | 215 | this.reader.close();
|
213 | 216 | }
|
214 | 217 |
|
| 218 | + @Test |
| 219 | + public void testReadFromSinglePartitionFromCustomOffset() { |
| 220 | + this.template.setDefaultTopic("topic5"); |
| 221 | + this.template.sendDefault("val0"); // <-- offset 0 |
| 222 | + this.template.sendDefault("val1"); // <-- offset 1 |
| 223 | + this.template.sendDefault("val2"); // <-- offset 2 |
| 224 | + this.template.sendDefault("val3"); // <-- offset 3 |
| 225 | + |
| 226 | + this.reader = new KafkaItemReader<>(this.consumerProperties, "topic5", 0); |
| 227 | + |
| 228 | + // specify which offset to start from |
| 229 | + Map<TopicPartition, Long> partitionOffsets = new HashMap<>(); |
| 230 | + partitionOffsets.put(new TopicPartition("topic5", 0), 2L); |
| 231 | + this.reader.setPartitionOffsets(partitionOffsets); |
| 232 | + |
| 233 | + this.reader.setPollTimeout(Duration.ofSeconds(1)); |
| 234 | + this.reader.open(new ExecutionContext()); |
| 235 | + |
| 236 | + String item = this.reader.read(); |
| 237 | + assertThat(item, is("val2")); |
| 238 | + |
| 239 | + item = this.reader.read(); |
| 240 | + assertThat(item, is("val3")); |
| 241 | + |
| 242 | + item = this.reader.read(); |
| 243 | + assertNull(item); |
| 244 | + |
| 245 | + this.reader.close(); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + public void testReadFromSinglePartitionFromTheOffsetStoredInKafka() throws Exception { |
| 250 | + // first run: read a topic from the beginning |
| 251 | + |
| 252 | + this.template.setDefaultTopic("topic6"); |
| 253 | + this.template.sendDefault("val0"); // <-- offset 0 |
| 254 | + this.template.sendDefault("val1"); // <-- offset 1 |
| 255 | + |
| 256 | + this.reader = new KafkaItemReader<>(this.consumerProperties, "topic6", 0); |
| 257 | + this.reader.setPollTimeout(Duration.ofSeconds(1)); |
| 258 | + this.reader.open(new ExecutionContext()); |
| 259 | + |
| 260 | + String item = this.reader.read(); |
| 261 | + assertThat(item, is("val0")); |
| 262 | + |
| 263 | + item = this.reader.read(); |
| 264 | + assertThat(item, is("val1")); |
| 265 | + |
| 266 | + item = this.reader.read(); |
| 267 | + assertNull(item); |
| 268 | + |
| 269 | + this.reader.close(); |
| 270 | + |
| 271 | + // The offset stored in Kafka should be equal to 2 at this point |
| 272 | + OffsetAndMetadata currentOffset = KafkaTestUtils.getCurrentOffset( |
| 273 | + embeddedKafka.getEmbeddedKafka().getBrokersAsString(), |
| 274 | + "1", "topic6", |
| 275 | + 0); |
| 276 | + assertEquals(2, currentOffset.offset()); |
| 277 | + |
| 278 | + // second run (with same consumer group ID): new messages arrived since the last run. |
| 279 | + |
| 280 | + this.template.sendDefault("val2"); // <-- offset 2 |
| 281 | + this.template.sendDefault("val3"); // <-- offset 3 |
| 282 | + |
| 283 | + this.reader = new KafkaItemReader<>(this.consumerProperties, "topic6", 0); |
| 284 | + // Passing an empty map means the reader should start from the offset stored in Kafka (offset 2 in this case) |
| 285 | + this.reader.setPartitionOffsets(new HashMap<>()); |
| 286 | + this.reader.setPollTimeout(Duration.ofSeconds(1)); |
| 287 | + this.reader.open(new ExecutionContext()); |
| 288 | + |
| 289 | + item = this.reader.read(); |
| 290 | + assertThat(item, is("val2")); |
| 291 | + |
| 292 | + item = this.reader.read(); |
| 293 | + assertThat(item, is("val3")); |
| 294 | + |
| 295 | + item = this.reader.read(); |
| 296 | + assertNull(item); |
| 297 | + |
| 298 | + this.reader.close(); |
| 299 | + } |
| 300 | + |
215 | 301 | @Test
|
216 | 302 | public void testReadFromMultiplePartitions() {
|
217 | 303 | this.template.setDefaultTopic("topic2");
|
|
0 commit comments