Binding name as a consumer/producer property

Make binding names available through Consumer/Producer properties.
Currently, the binders use a ThreadLocal to store the binding name for internal use.
These changes introduce the binding name as a property for both producer/consumer bindings.

Resolves https://github.com/spring-cloud/spring-cloud-stream/issues/2380
This commit is contained in:
Soby Chacko
2022-05-10 16:27:32 -04:00
committed by Gary Russell
parent 112a93c5f0
commit dae959999a
6 changed files with 73 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2022 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.
@@ -207,8 +207,6 @@ public class KafkaMessageChannelBinder extends
*/
public static final String X_ORIGINAL_TIMESTAMP_TYPE = "x-original-timestamp-type";
private static final ThreadLocal<String> bindingNameHolder = new ThreadLocal<>();
private static final Pattern interceptorNeededPattern = Pattern.compile("(payload|#root|#this)");
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
@@ -337,13 +335,11 @@ public class KafkaMessageChannelBinder extends
@Override
public KafkaConsumerProperties getExtendedConsumerProperties(String channelName) {
bindingNameHolder.set(channelName);
return this.extendedBindingProperties.getExtendedConsumerProperties(channelName);
}
@Override
public KafkaProducerProperties getExtendedProducerProperties(String channelName) {
bindingNameHolder.set(channelName);
return this.extendedBindingProperties.getExtendedProducerProperties(channelName);
}
@@ -573,8 +569,7 @@ public class KafkaMessageChannelBinder extends
props.putAll(kafkaProducerProperties.getConfiguration());
}
if (this.producerConfigCustomizer != null) {
this.producerConfigCustomizer.configure(props, bindingNameHolder.get(), destination);
bindingNameHolder.remove();
this.producerConfigCustomizer.configure(props, producerProperties.getBindingName(), destination);
}
DefaultKafkaProducerFactory<byte[], byte[]> producerFactory = new DefaultKafkaProducerFactory<>(
props);
@@ -855,8 +850,7 @@ public class KafkaMessageChannelBinder extends
Assert.isTrue(!extendedConsumerProperties.getExtension().isResetOffsets(),
"'resetOffsets' cannot be set when a KafkaBindingRebalanceListener is provided");
final String bindingName = bindingNameHolder.get();
bindingNameHolder.remove();
final String bindingName = extendedConsumerProperties.getBindingName();
Assert.notNull(bindingName, "'bindingName' cannot be null");
final KafkaBindingRebalanceListener userRebalanceListener = this.rebalanceListener;
containerProperties
@@ -1457,7 +1451,7 @@ public class KafkaMessageChannelBinder extends
}
if (this.consumerConfigCustomizer != null) {
this.consumerConfigCustomizer.configure(props, bindingNameHolder.get(), destination);
this.consumerConfigCustomizer.configure(props, consumerProperties.getBindingName(), destination);
}
DefaultKafkaConsumerFactory<Object, Object> factory = new DefaultKafkaConsumerFactory<>(props);
factory.setBeanName(beanName);

View File

@@ -1157,7 +1157,7 @@ public class KafkaBinderTests extends
assertThat(receivedMessage.getHeaders()
.get(KafkaMessageChannelBinder.X_EXCEPTION_FQCN)).isNotNull();
assertThat(receivedMessage.getHeaders()
.get(KafkaHeaders.RECEIVED_KEY)).isEqualTo(expectedDlqPartition);
.get(KafkaHeaders.RECEIVED_PARTITION)).isEqualTo(expectedDlqPartition);
}
else if (!HeaderMode.none.equals(headerMode)) {
assertThat(handler.getInvocationCount())
@@ -1205,7 +1205,7 @@ public class KafkaBinderTests extends
.get(KafkaMessageChannelBinder.X_EXCEPTION_FQCN)).isNotNull();
assertThat(receivedMessage.getHeaders()
.get(KafkaHeaders.RECEIVED_KEY)).isEqualTo(expectedDlqPartition);
.get(KafkaHeaders.RECEIVED_PARTITION)).isEqualTo(expectedDlqPartition);
}
else {
assertThat(receivedMessage.getHeaders()