|
1 | 1 | /*
|
2 |
| - * Copyright 2013-2021 the original author or authors. |
| 2 | + * Copyright 2013-2023 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.
|
@@ -423,6 +423,7 @@ public String getComponentType() {
|
423 | 423 | return this.isGateway ? "kafka:outbound-gateway" : "kafka:outbound-channel-adapter";
|
424 | 424 | }
|
425 | 425 |
|
| 426 | + @Nullable |
426 | 427 | protected MessageChannel getSendFailureChannel() {
|
427 | 428 | if (this.sendFailureChannel != null) {
|
428 | 429 | return this.sendFailureChannel;
|
@@ -500,19 +501,27 @@ protected Object handleRequestMessage(final Message<?> message) {
|
500 | 501 | }
|
501 | 502 | ListenableFuture<SendResult<K, V>> sendFuture;
|
502 | 503 | RequestReplyFuture<K, V, Object> gatewayFuture = null;
|
503 |
| - if (this.isGateway && (!preBuilt || producerRecord.headers().lastHeader(KafkaHeaders.REPLY_TOPIC) == null)) { |
504 |
| - producerRecord.headers().add(new RecordHeader(KafkaHeaders.REPLY_TOPIC, getReplyTopic(message))); |
505 |
| - gatewayFuture = ((ReplyingKafkaTemplate<K, V, Object>) this.kafkaTemplate).sendAndReceive(producerRecord); |
506 |
| - sendFuture = gatewayFuture.getSendFuture(); |
507 |
| - } |
508 |
| - else { |
509 |
| - if (this.transactional && !this.kafkaTemplate.inTransaction() && !this.allowNonTransactional) { |
510 |
| - sendFuture = this.kafkaTemplate.executeInTransaction(template -> template.send(producerRecord)); |
| 504 | + try { |
| 505 | + if (this.isGateway |
| 506 | + && (!preBuilt || producerRecord.headers().lastHeader(KafkaHeaders.REPLY_TOPIC) == null)) { |
| 507 | + producerRecord.headers().add(new RecordHeader(KafkaHeaders.REPLY_TOPIC, getReplyTopic(message))); |
| 508 | + gatewayFuture = ((ReplyingKafkaTemplate<K, V, Object>) this.kafkaTemplate) |
| 509 | + .sendAndReceive(producerRecord); |
| 510 | + sendFuture = gatewayFuture.getSendFuture(); |
511 | 511 | }
|
512 | 512 | else {
|
513 |
| - sendFuture = this.kafkaTemplate.send(producerRecord); |
| 513 | + if (this.transactional && !this.kafkaTemplate.inTransaction() && !this.allowNonTransactional) { |
| 514 | + sendFuture = this.kafkaTemplate.executeInTransaction(template -> template.send(producerRecord)); |
| 515 | + } |
| 516 | + else { |
| 517 | + sendFuture = this.kafkaTemplate.send(producerRecord); |
| 518 | + } |
514 | 519 | }
|
515 | 520 | }
|
| 521 | + catch (RuntimeException rtex) { |
| 522 | + sendFailure(message, producerRecord, getSendFailureChannel(), rtex); |
| 523 | + throw rtex; |
| 524 | + } |
516 | 525 | sendFutureIfRequested(sendFuture, futureToken);
|
517 | 526 | if (flush) {
|
518 | 527 | this.kafkaTemplate.flush();
|
@@ -680,11 +689,7 @@ public void onSuccess(SendResult<K, V> result) {
|
680 | 689 |
|
681 | 690 | @Override
|
682 | 691 | public void onFailure(Throwable ex) {
|
683 |
| - if (failureChannel != null) { |
684 |
| - KafkaProducerMessageHandler.this.messagingTemplate.send(failureChannel, |
685 |
| - KafkaProducerMessageHandler.this.errorMessageStrategy.buildErrorMessage( |
686 |
| - new KafkaSendFailureException(message, producerRecord, ex), null)); |
687 |
| - } |
| 692 | + sendFailure(message, producerRecord, failureChannel, ex); |
688 | 693 | }
|
689 | 694 |
|
690 | 695 | });
|
@@ -713,6 +718,16 @@ public void onFailure(Throwable ex) {
|
713 | 718 | }
|
714 | 719 | }
|
715 | 720 |
|
| 721 | + private void sendFailure(final Message<?> message, final ProducerRecord<K, V> producerRecord, |
| 722 | + @Nullable MessageChannel failureChannel, Throwable exception) { |
| 723 | + |
| 724 | + if (failureChannel != null) { |
| 725 | + KafkaProducerMessageHandler.this.messagingTemplate.send(failureChannel, |
| 726 | + KafkaProducerMessageHandler.this.errorMessageStrategy.buildErrorMessage( |
| 727 | + new KafkaSendFailureException(message, producerRecord, exception), null)); |
| 728 | + } |
| 729 | + } |
| 730 | + |
716 | 731 | private Future<?> processReplyFuture(@Nullable RequestReplyFuture<?, ?, Object> future) {
|
717 | 732 | if (future == null) {
|
718 | 733 | return null;
|
|
0 commit comments