From f00ede41f082177c75c3f31f2e36bd2d125973de Mon Sep 17 00:00:00 2001 From: Chris Bono Date: Tue, 13 Aug 2024 10:59:52 -0500 Subject: [PATCH] Allow PulsarTopicBuilder#init null values (#777) * Allow PulsarTopicBuilder#init null values See #756 * Clarify the deprecation of PulsarTopic.builder --- .../antora/modules/ROOT/pages/whats-new.adoc | 6 +++--- .../pulsar/core/PulsarTopic.java | 5 +++-- .../pulsar/core/PulsarTopicBuilder.java | 18 ++++++++++-------- .../pulsar/core/PulsarTopicBuilderTests.java | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/spring-pulsar-docs/src/main/antora/modules/ROOT/pages/whats-new.adoc b/spring-pulsar-docs/src/main/antora/modules/ROOT/pages/whats-new.adoc index d5ae81c3..aa178686 100644 --- a/spring-pulsar-docs/src/main/antora/modules/ROOT/pages/whats-new.adoc +++ b/spring-pulsar-docs/src/main/antora/modules/ROOT/pages/whats-new.adoc @@ -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 diff --git a/spring-pulsar/src/main/java/org/springframework/pulsar/core/PulsarTopic.java b/spring-pulsar/src/main/java/org/springframework/pulsar/core/PulsarTopic.java index 57096383..420e0408 100644 --- a/spring-pulsar/src/main/java/org/springframework/pulsar/core/PulsarTopic.java +++ b/spring-pulsar/src/main/java/org/springframework/pulsar/core/PulsarTopic.java @@ -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) { diff --git a/spring-pulsar/src/main/java/org/springframework/pulsar/core/PulsarTopicBuilder.java b/spring-pulsar/src/main/java/org/springframework/pulsar/core/PulsarTopicBuilder.java index a476a820..18041d79 100644 --- a/spring-pulsar/src/main/java/org/springframework/pulsar/core/PulsarTopicBuilder.java +++ b/spring-pulsar/src/main/java/org/springframework/pulsar/core/PulsarTopicBuilder.java @@ -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; } /** diff --git a/spring-pulsar/src/test/java/org/springframework/pulsar/core/PulsarTopicBuilderTests.java b/spring-pulsar/src/test/java/org/springframework/pulsar/core/PulsarTopicBuilderTests.java index 3bf59f0a..dbe5934f 100644 --- a/spring-pulsar/src/test/java/org/springframework/pulsar/core/PulsarTopicBuilderTests.java +++ b/spring-pulsar/src/test/java/org/springframework/pulsar/core/PulsarTopicBuilderTests.java @@ -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"); + } + }