38
38
import org .springframework .kafka .core .DefaultKafkaProducerFactory ;
39
39
import org .springframework .kafka .core .KafkaTemplate ;
40
40
import org .springframework .kafka .core .ProducerFactory ;
41
+ import org .springframework .kafka .support .SendResult ;
41
42
import org .springframework .kafka .test .rule .EmbeddedKafkaRule ;
42
43
import org .springframework .kafka .test .utils .KafkaTestUtils ;
43
44
import org .springframework .util .concurrent .ListenableFuture ;
@@ -186,12 +187,16 @@ public void testValidation() {
186
187
}
187
188
188
189
@ Test
189
- public void testReadFromSinglePartition () {
190
+ public void testReadFromSinglePartition () throws ExecutionException , InterruptedException {
190
191
this .template .setDefaultTopic ("topic1" );
191
- this .template .sendDefault ("val0" );
192
- this .template .sendDefault ("val1" );
193
- this .template .sendDefault ("val2" );
194
- this .template .sendDefault ("val3" );
192
+ List <ListenableFuture <SendResult <String , String >>> futures = new ArrayList <>();
193
+ futures .add (this .template .sendDefault ("val0" ));
194
+ futures .add (this .template .sendDefault ("val1" ));
195
+ futures .add (this .template .sendDefault ("val2" ));
196
+ futures .add (this .template .sendDefault ("val3" ));
197
+ for (ListenableFuture <SendResult <String , String >> future : futures ) {
198
+ future .get ();
199
+ }
195
200
196
201
this .reader = new KafkaItemReader <>(this .consumerProperties , "topic1" , 0 );
197
202
this .reader .setPollTimeout (Duration .ofSeconds (1 ));
@@ -216,12 +221,16 @@ public void testReadFromSinglePartition() {
216
221
}
217
222
218
223
@ Test
219
- public void testReadFromSinglePartitionFromCustomOffset () {
224
+ public void testReadFromSinglePartitionFromCustomOffset () throws ExecutionException , InterruptedException {
220
225
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
226
+ List <ListenableFuture <SendResult <String , String >>> futures = new ArrayList <>();
227
+ futures .add (this .template .sendDefault ("val0" )); // <-- offset 0
228
+ futures .add (this .template .sendDefault ("val1" )); // <-- offset 1
229
+ futures .add (this .template .sendDefault ("val2" )); // <-- offset 2
230
+ futures .add (this .template .sendDefault ("val3" )); // <-- offset 3
231
+ for (ListenableFuture <SendResult <String , String >> future : futures ) {
232
+ future .get ();
233
+ }
225
234
226
235
this .reader = new KafkaItemReader <>(this .consumerProperties , "topic5" , 0 );
227
236
@@ -250,9 +259,12 @@ public void testReadFromSinglePartitionFromTheOffsetStoredInKafka() throws Excep
250
259
// first run: read a topic from the beginning
251
260
252
261
this .template .setDefaultTopic ("topic6" );
253
- this .template .sendDefault ("val0" ); // <-- offset 0
254
- this .template .sendDefault ("val1" ); // <-- offset 1
255
-
262
+ List <ListenableFuture <SendResult <String , String >>> futures = new ArrayList <>();
263
+ futures .add (this .template .sendDefault ("val0" )); // <-- offset 0
264
+ futures .add (this .template .sendDefault ("val1" )); // <-- offset 1
265
+ for (ListenableFuture <SendResult <String , String >> future : futures ) {
266
+ future .get ();
267
+ }
256
268
this .reader = new KafkaItemReader <>(this .consumerProperties , "topic6" , 0 );
257
269
this .reader .setPollTimeout (Duration .ofSeconds (1 ));
258
270
this .reader .open (new ExecutionContext ());
@@ -299,12 +311,16 @@ public void testReadFromSinglePartitionFromTheOffsetStoredInKafka() throws Excep
299
311
}
300
312
301
313
@ Test
302
- public void testReadFromMultiplePartitions () {
314
+ public void testReadFromMultiplePartitions () throws ExecutionException , InterruptedException {
303
315
this .template .setDefaultTopic ("topic2" );
304
- this .template .sendDefault ("val0" );
305
- this .template .sendDefault ("val1" );
306
- this .template .sendDefault ("val2" );
307
- this .template .sendDefault ("val3" );
316
+ List <ListenableFuture <SendResult <String , String >>> futures = new ArrayList <>();
317
+ futures .add (this .template .sendDefault ("val0" ));
318
+ futures .add (this .template .sendDefault ("val1" ));
319
+ futures .add (this .template .sendDefault ("val2" ));
320
+ futures .add (this .template .sendDefault ("val3" ));
321
+ for (ListenableFuture <SendResult <String , String >> future : futures ) {
322
+ future .get ();
323
+ }
308
324
309
325
this .reader = new KafkaItemReader <>(this .consumerProperties , "topic2" , 0 , 1 );
310
326
this .reader .setPollTimeout (Duration .ofSeconds (1 ));
@@ -323,14 +339,17 @@ public void testReadFromMultiplePartitions() {
323
339
}
324
340
325
341
@ Test
326
- public void testReadFromSinglePartitionAfterRestart () {
342
+ public void testReadFromSinglePartitionAfterRestart () throws ExecutionException , InterruptedException {
327
343
this .template .setDefaultTopic ("topic3" );
328
- this .template .sendDefault ("val0" );
329
- this .template .sendDefault ("val1" );
330
- this .template .sendDefault ("val2" );
331
- this .template .sendDefault ("val3" );
332
- this .template .sendDefault ("val4" );
333
-
344
+ List <ListenableFuture <SendResult <String , String >>> futures = new ArrayList <>();
345
+ futures .add (this .template .sendDefault ("val0" ));
346
+ futures .add (this .template .sendDefault ("val1" ));
347
+ futures .add (this .template .sendDefault ("val2" ));
348
+ futures .add (this .template .sendDefault ("val3" ));
349
+ futures .add (this .template .sendDefault ("val4" ));
350
+ for (ListenableFuture <SendResult <String , String >> future : futures ) {
351
+ future .get ();
352
+ }
334
353
ExecutionContext executionContext = new ExecutionContext ();
335
354
Map <TopicPartition , Long > offsets = new HashMap <>();
336
355
offsets .put (new TopicPartition ("topic3" , 0 ), 1L );
0 commit comments