Allow PulsarTopicBuilder#init null values (#777)

* Allow PulsarTopicBuilder#init null values

See #756

* Clarify the deprecation of PulsarTopic.builder
This commit is contained in:
Chris Bono
2024-08-13 10:59:52 -05:00
committed by GitHub
parent e854e7881f
commit f00ede41f0
4 changed files with 30 additions and 13 deletions

View File

@@ -20,9 +20,9 @@ See xref:./reference/default-tenant-namespace.adoc[Default Tenant / Namespace] f
Version `3.3.1` of the Pulsar client deprecates the `getPartitionsForTopic(java.lang.String)` in favor of `getPartitionsForTopic(java.lang.String, boolean metadataAutoCreationEnabled)`.
==== PulsarTopic#builder
The `PulsarTopicBuilder` is now a registered bean that is configured with default values for domain, tenant, and namespace.
As such, this convenience method is no longer needed.
Instead, inject the builder bean where needed.
When using Spring Boot the `PulsarTopicBuilder` is now a registered bean that is configured with default values for domain, tenant, and namespace.
Therefore, if you are using Spring Boot, you can simply inject the builder where needed.
Otherwise, use one of the `PulsarTopicBuilder` constructors directly.
=== Breaking Changes

View File

@@ -61,8 +61,9 @@ public record PulsarTopic(String topicName, int numberOfPartitions) {
* Convenience method to create a topic builder with the specified topic name.
* @param topicName the name of the topic
* @return the topic builder instance
* @deprecated As of version 1.2.0 topic builder is a registered bean - instead use an
* injected instance where needed
* @deprecated since 1.2.0 for removal in 1.4.0 in favor of
* {@link PulsarTopicBuilder#PulsarTopicBuilder()} or
* {@link PulsarTopicBuilder#PulsarTopicBuilder(TopicDomain, String, String)}
*/
@Deprecated(since = "1.2.0", forRemoval = true)
public static PulsarTopicBuilder builder(String topicName) {

View File

@@ -20,6 +20,7 @@ import org.apache.pulsar.common.naming.TopicDomain;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Builder class to create {@link PulsarTopic} instances.
@@ -60,17 +61,18 @@ public class PulsarTopicBuilder {
/**
* Create a builder instance that uses the specified defaults.
* @param defaultDomain domain to use for the topic when not present in the name
* @param defaultTenant tentant to use for the topic when not present in the name
* @param defaultNamespace namespace to use for the topic when not present in the name
* @param defaultDomain domain to use when topic name is not fully-qualified
* @param defaultTenant tenant to use when topic name is not fully-qualified or null
* to use the Pulsar default tenant of 'public'
* @param defaultNamespace namespace to use when topic name is not fully-qualified or
* null to use the Pulsar default namespace of 'namespace'
*/
public PulsarTopicBuilder(TopicDomain defaultDomain, String defaultTenant, String defaultNamespace) {
public PulsarTopicBuilder(TopicDomain defaultDomain, @Nullable String defaultTenant,
@Nullable String defaultNamespace) {
Assert.notNull(defaultDomain, "defaultDomain must not be null");
Assert.hasText(defaultTenant, "defaultTenant must be specified");
Assert.hasText(defaultNamespace, "defaultNamespace must be specified");
this.defaultDomain = defaultDomain;
this.defaultTenant = defaultTenant;
this.defaultNamespace = defaultNamespace;
this.defaultTenant = StringUtils.hasText(defaultTenant) ? defaultTenant : DEFAULT_TENANT;
this.defaultNamespace = StringUtils.hasText(defaultNamespace) ? defaultNamespace : DEFAULT_NAMESPACE;
}
/**

View File

@@ -80,4 +80,18 @@ class PulsarTopicBuilderTests {
Arguments.of(customBuilder, "persistent://foo/bar/my-topic", "persistent://foo/bar/my-topic"));
}
@Test
void whenConstructedWithNullTenantThenPulsarDefaultTenantIsUsed() {
var topicBuilder = new PulsarTopicBuilder(TopicDomain.persistent, null, "foo");
var fqTopic = topicBuilder.name("my-topic").build();
assertThat(fqTopic.topicName()).isEqualTo("persistent://public/foo/my-topic");
}
@Test
void whenConstructedWithNullNamespaceThenPulsarDefaultNamespaceIsUsed() {
var topicBuilder = new PulsarTopicBuilder(TopicDomain.persistent, "foo", null);
var fqTopic = topicBuilder.name("my-topic").build();
assertThat(fqTopic.topicName()).isEqualTo("persistent://foo/default/my-topic");
}
}