Fix TODOs in outbound channel adapter

- always use `successChannel` for metadata
- unwrap `ExecutionException`
This commit is contained in:
Gary Russell
2018-10-04 12:09:15 -04:00
committed by Artem Bilan
parent f29bd3e322
commit 19c3c4607f
4 changed files with 9 additions and 23 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -85,8 +85,7 @@ public class KafkaOutboundChannelAdapterParser extends AbstractOutboundChannelAd
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-message-strategy");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "send-failure-channel");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "send-success-channel",
"outputChannel");
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "send-success-channel");
return builder.getBeanDefinition();
}

View File

@@ -367,9 +367,7 @@ public class KafkaProducerMessageHandler<K, V> extends AbstractReplyProducingMes
(K) messageKey, payload, headers);
ListenableFuture<SendResult<K, V>> sendFuture;
RequestReplyFuture<K, V, Object> gatewayFuture = null;
MessageChannel metadataChannel = null;
if (this.isGateway) {
metadataChannel = getSendSuccessChannel();
producerRecord.headers().add(new RecordHeader(KafkaHeaders.REPLY_TOPIC, getReplyTopic(message)));
gatewayFuture = ((ReplyingKafkaTemplate<K, V, Object>) this.kafkaTemplate).sendAndReceive(producerRecord);
sendFuture = gatewayFuture.getSendFuture();
@@ -383,27 +381,16 @@ public class KafkaProducerMessageHandler<K, V> extends AbstractReplyProducingMes
else {
sendFuture = this.kafkaTemplate.send(producerRecord);
}
// TODO: In 3.1, always use the success channel.
if (!this.noOutputChannel) {
metadataChannel = getOutputChannel();
if (metadataChannel == null) {
this.noOutputChannel = true;
}
}
if (metadataChannel == null) {
metadataChannel = getSendSuccessChannel();
}
}
try {
processSendResult(message, producerRecord, sendFuture, metadataChannel);
processSendResult(message, producerRecord, sendFuture, getSendSuccessChannel());
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MessageHandlingException(message, e);
}
catch (ExecutionException e) {
// TODO: in 3.1 change this to e.getCause()
throw new MessageHandlingException(message, e);
throw new MessageHandlingException(message, e.getCause());
}
return processReplyFuture(gatewayFuture);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,7 +20,6 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeoutException;
@@ -37,6 +36,7 @@ import org.springframework.expression.common.LiteralExpression;
import org.springframework.integration.MessageTimeoutException;
import org.springframework.integration.kafka.outbound.KafkaProducerMessageHandler;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.kafka.core.KafkaProducerException;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.messaging.support.GenericMessage;
@@ -78,7 +78,7 @@ public class KafkaOutboundAdapterParserTests {
.isSameAs(this.appContext.getBean("ems"));
assertThat(TestUtils.getPropertyValue(messageHandler, "sendFailureChannel"))
.isSameAs(this.appContext.getBean("failures"));
assertThat(TestUtils.getPropertyValue(messageHandler, "outputChannel"))
assertThat(TestUtils.getPropertyValue(messageHandler, "sendSuccessChannel"))
.isSameAs(this.appContext.getBean("successes"));
messageHandler
@@ -125,7 +125,7 @@ public class KafkaOutboundAdapterParserTests {
}
catch (Exception e) {
assertThat(e).isInstanceOf(MessageHandlingException.class);
assertThat(e.getCause()).isExactlyInstanceOf(ExecutionException.class);
assertThat(e.getCause()).isExactlyInstanceOf(KafkaProducerException.class);
assertThat(e.getCause().getCause()).isInstanceOf(RuntimeException.class);
assertThat(e.getMessage()).contains("Async Producer Mock exception");
}

View File

@@ -256,7 +256,7 @@ public class KafkaProducerMessageHandlerTests {
KafkaProducerMessageHandler<Integer, String> handler = new KafkaProducerMessageHandler<>(template);
handler.setBeanFactory(mock(BeanFactory.class));
PollableChannel successes = new QueueChannel();
handler.setOutputChannel(successes);
handler.setSendSuccessChannel(successes);
handler.afterPropertiesSet();
Message<?> message = MessageBuilder.withPayload("foo")