17
17
package org .springframework .kafka .requestreply ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
20
21
import static org .assertj .core .api .Assertions .fail ;
21
22
import static org .mockito .ArgumentMatchers .any ;
22
23
import static org .mockito .ArgumentMatchers .isNull ;
@@ -151,7 +152,7 @@ public void captureTestName(TestInfo info) {
151
152
public void testGood () throws Exception {
152
153
ReplyingKafkaTemplate <Integer , String , String > template = createTemplate (A_REPLY );
153
154
try {
154
- template .setReplyTimeout ( 30_000 );
155
+ template .setDefaultReplyTimeout ( Duration . ofSeconds ( 30 ) );
155
156
Headers headers = new RecordHeaders ();
156
157
headers .add ("baz" , "buz" .getBytes ());
157
158
ProducerRecord <Integer , String > record = new ProducerRecord <>(A_REQUEST , null , null , null , "foo" , headers );
@@ -165,6 +166,11 @@ public void testGood() throws Exception {
165
166
assertThat (receivedHeaders ).hasSize (2 );
166
167
assertThat (this .registry .getListenerContainer (A_REQUEST ).getContainerProperties ().isMissingTopicsFatal ())
167
168
.isFalse ();
169
+ ProducerRecord <Integer , String > record2 =
170
+ new ProducerRecord <>(A_REQUEST , null , null , null , "slow" , headers );
171
+ assertThatExceptionOfType (ExecutionException .class )
172
+ .isThrownBy (() -> template .sendAndReceive (record2 , Duration .ZERO ).get (10 , TimeUnit .SECONDS ))
173
+ .withCauseExactlyInstanceOf (KafkaReplyTimeoutException .class );
168
174
}
169
175
finally {
170
176
template .stop ();
@@ -176,7 +182,7 @@ public void testGood() throws Exception {
176
182
public void testMultiListenerMessageReturn () throws Exception {
177
183
ReplyingKafkaTemplate <Integer , String , String > template = createTemplate (C_REPLY );
178
184
try {
179
- template .setReplyTimeout ( 30_000 );
185
+ template .setDefaultReplyTimeout ( Duration . ofSeconds ( 30 ) );
180
186
ProducerRecord <Integer , String > record = new ProducerRecord <>(C_REQUEST , "foo" );
181
187
record .headers ().add (new RecordHeader (KafkaHeaders .REPLY_TOPIC , C_REPLY .getBytes ()));
182
188
RequestReplyFuture <Integer , String , String > future = template .sendAndReceive (record );
@@ -195,7 +201,7 @@ public void testGoodDefaultReplyHeaders() throws Exception {
195
201
ReplyingKafkaTemplate <Integer , String , String > template = createTemplate (
196
202
new TopicPartitionOffset (A_REPLY , 3 ));
197
203
try {
198
- template .setReplyTimeout ( 30_000 );
204
+ template .setDefaultReplyTimeout ( Duration . ofSeconds ( 30 ) );
199
205
ProducerRecord <Integer , String > record = new ProducerRecord <>(A_REQUEST , "bar" );
200
206
RequestReplyFuture <Integer , String , String > future = template .sendAndReceive (record );
201
207
future .getSendFuture ().get (10 , TimeUnit .SECONDS ); // send ok
@@ -213,7 +219,7 @@ public void testGoodDefaultReplyHeaders() throws Exception {
213
219
public void testGoodSamePartition () throws Exception {
214
220
ReplyingKafkaTemplate <Integer , String , String > template = createTemplate (A_REPLY );
215
221
try {
216
- template .setReplyTimeout ( 30_000 );
222
+ template .setDefaultReplyTimeout ( Duration . ofSeconds ( 30 ) );
217
223
ProducerRecord <Integer , String > record = new ProducerRecord <>(A_REQUEST , 2 , null , "baz" );
218
224
record .headers ().add (new RecordHeader (KafkaHeaders .REPLY_TOPIC , A_REPLY .getBytes ()));
219
225
record .headers ()
@@ -235,7 +241,7 @@ public void testGoodSamePartition() throws Exception {
235
241
public void testTimeout () throws Exception {
236
242
ReplyingKafkaTemplate <Integer , String , String > template = createTemplate (A_REPLY );
237
243
try {
238
- template .setReplyTimeout ( 1 );
244
+ template .setDefaultReplyTimeout ( Duration . ofMillis ( 1 ) );
239
245
ProducerRecord <Integer , String > record = new ProducerRecord <>(A_REQUEST , "fiz" );
240
246
record .headers ().add (new RecordHeader (KafkaHeaders .REPLY_TOPIC , A_REPLY .getBytes ()));
241
247
RequestReplyFuture <Integer , String , String > future = template .sendAndReceive (record );
@@ -265,7 +271,7 @@ public void testTimeout() throws Exception {
265
271
public void testGoodWithSimpleMapper () throws Exception {
266
272
ReplyingKafkaTemplate <Integer , String , String > template = createTemplate (B_REPLY );
267
273
try {
268
- template .setReplyTimeout ( 30_000 );
274
+ template .setDefaultReplyTimeout ( Duration . ofSeconds ( 30 ) );
269
275
Headers headers = new RecordHeaders ();
270
276
headers .add ("baz" , "buz" .getBytes ());
271
277
ProducerRecord <Integer , String > record = new ProducerRecord <>(B_REQUEST , null , null , null , "qux" , headers );
@@ -291,7 +297,7 @@ public void testAggregateNormal() throws Exception {
291
297
AggregatingReplyingKafkaTemplate <Integer , String , String > template = aggregatingTemplate (
292
298
new TopicPartitionOffset (D_REPLY , 0 ), 2 );
293
299
try {
294
- template .setReplyTimeout ( 30_000 );
300
+ template .setDefaultReplyTimeout ( Duration . ofSeconds ( 30 ) );
295
301
ProducerRecord <Integer , String > record = new ProducerRecord <>(D_REQUEST , null , null , null , "foo" );
296
302
RequestReplyFuture <Integer , String , Collection <ConsumerRecord <Integer , String >>> future =
297
303
template .sendAndReceive (record );
@@ -320,7 +326,7 @@ public void testAggregateTimeout() throws Exception {
320
326
AggregatingReplyingKafkaTemplate <Integer , String , String > template = aggregatingTemplate (
321
327
new TopicPartitionOffset (E_REPLY , 0 ), 3 );
322
328
try {
323
- template .setReplyTimeout ( 5_000 );
329
+ template .setDefaultReplyTimeout ( Duration . ofSeconds ( 5 ) );
324
330
ProducerRecord <Integer , String > record = new ProducerRecord <>(E_REQUEST , null , null , null , "foo" );
325
331
RequestReplyFuture <Integer , String , Collection <ConsumerRecord <Integer , String >>> future =
326
332
template .sendAndReceive (record );
@@ -355,7 +361,7 @@ public void testAggregateTimeoutPartial() throws Exception {
355
361
new TopicPartitionOffset (F_REPLY , 0 ), 3 );
356
362
template .setReturnPartialOnTimeout (true );
357
363
try {
358
- template .setReplyTimeout ( 5_000 );
364
+ template .setDefaultReplyTimeout ( Duration . ofSeconds ( 5 ) );
359
365
ProducerRecord <Integer , String > record = new ProducerRecord <>(F_REQUEST , null , null , null , "foo" );
360
366
RequestReplyFuture <Integer , String , Collection <ConsumerRecord <Integer , String >>> future =
361
367
template .sendAndReceive (record );
@@ -395,7 +401,7 @@ public void testAggregateOrphansNotStored() throws Exception {
395
401
return null ;
396
402
}).given (producer ).send (any (), any ());
397
403
AggregatingReplyingKafkaTemplate template = new AggregatingReplyingKafkaTemplate (pf , container , coll -> true );
398
- template .setReplyTimeout ( 30_000 );
404
+ template .setDefaultReplyTimeout ( Duration . ofSeconds ( 30 ) );
399
405
template .start ();
400
406
List <ConsumerRecord > records = new ArrayList <>();
401
407
ConsumerRecord record = new ConsumerRecord ("two" , 0 , 0L , null , "test1" );
@@ -497,7 +503,7 @@ public void withCustomHeaders() throws Exception {
497
503
template .setReplyTopicHeaderName ("custom.reply.to" );
498
504
template .setReplyPartitionHeaderName ("custom.reply.partition" );
499
505
try {
500
- template .setReplyTimeout ( 30_000 );
506
+ template .setDefaultReplyTimeout ( Duration . ofSeconds ( 30 ) );
501
507
Headers headers = new RecordHeaders ();
502
508
ProducerRecord <Integer , String > record = new ProducerRecord <>(G_REQUEST , null , null , null , "foo" , headers );
503
509
RequestReplyFuture <Integer , String , String > future = template .sendAndReceive (record );
@@ -575,7 +581,10 @@ public Map<String, Object> additionalHeaders() {
575
581
576
582
@ KafkaListener (id = A_REQUEST , topics = A_REQUEST )
577
583
@ SendTo // default REPLY_TOPIC header
578
- public String handleA (String in ) {
584
+ public String handleA (String in ) throws InterruptedException {
585
+ if (in .equals ("slow" )) {
586
+ Thread .sleep (50 );
587
+ }
579
588
return in .toUpperCase ();
580
589
}
581
590
0 commit comments