|
18 | 18 |
|
19 | 19 | import java.lang.reflect.Type;
|
20 | 20 | import java.nio.charset.StandardCharsets;
|
| 21 | +import java.time.Duration; |
21 | 22 | import java.util.Collection;
|
22 | 23 | import java.util.HashMap;
|
23 | 24 | import java.util.Map;
|
@@ -103,6 +104,10 @@ public class KafkaProducerMessageHandler<K, V> extends AbstractReplyProducingMes
|
103 | 104 | */
|
104 | 105 | private static final int DEFAULT_TIMEOUT_BUFFER = 5000;
|
105 | 106 |
|
| 107 | + private static final int TWENTY = 20; |
| 108 | + |
| 109 | + private static final Duration DEFAULT_ASSIGNMENT_TIMEOUT = Duration.ofSeconds(TWENTY); |
| 110 | + |
106 | 111 | private final Map<String, Set<Integer>> replyTopicsAndPartitions = new HashMap<>();
|
107 | 112 |
|
108 | 113 | private final KafkaTemplate<K, V> kafkaTemplate;
|
@@ -162,6 +167,8 @@ public class KafkaProducerMessageHandler<K, V> extends AbstractReplyProducingMes
|
162 | 167 |
|
163 | 168 | private boolean useTemplateConverter;
|
164 | 169 |
|
| 170 | + private Duration assignmentDuration = DEFAULT_ASSIGNMENT_TIMEOUT; |
| 171 | + |
165 | 172 | private volatile byte[] singleReplyTopic;
|
166 | 173 |
|
167 | 174 | public KafkaProducerMessageHandler(final KafkaTemplate<K, V> kafkaTemplate) {
|
@@ -415,6 +422,17 @@ public void setUseTemplateConverter(boolean useTemplateConverter) {
|
415 | 422 | this.useTemplateConverter = useTemplateConverter;
|
416 | 423 | }
|
417 | 424 |
|
| 425 | + /** |
| 426 | + * Set the time to wait for partition assignment, when used as a gateway, to determine |
| 427 | + * the default reply-to topic/partition. |
| 428 | + * @param assignmentDuration the assignmentDuration to set. |
| 429 | + * @since 6.0 |
| 430 | + */ |
| 431 | + public void setAssignmentDuration(Duration assignmentDuration) { |
| 432 | + Assert.notNull(assignmentDuration, "'assignmentDuration' cannot be null"); |
| 433 | + this.assignmentDuration = assignmentDuration; |
| 434 | + } |
| 435 | + |
418 | 436 | @Override
|
419 | 437 | public String getComponentType() {
|
420 | 438 | return this.isGateway ? "kafka:outbound-gateway" : "kafka:outbound-channel-adapter";
|
@@ -647,6 +665,12 @@ private byte[] getSingleReplyTopic() {
|
647 | 665 |
|
648 | 666 | private void determineValidReplyTopicsAndPartitions() {
|
649 | 667 | ReplyingKafkaTemplate<?, ?, ?> rkt = (ReplyingKafkaTemplate<?, ?, ?>) this.kafkaTemplate;
|
| 668 | + try { |
| 669 | + rkt.waitForAssignment(this.assignmentDuration); |
| 670 | + } |
| 671 | + catch (InterruptedException e) { |
| 672 | + Thread.currentThread().interrupt(); |
| 673 | + } |
650 | 674 | Collection<TopicPartition> replyTopics = rkt.getAssignedReplyTopicPartitions();
|
651 | 675 | Map<String, Set<Integer>> topicsAndPartitions = new HashMap<>();
|
652 | 676 | if (replyTopics != null) {
|
|
0 commit comments