Skip to content

Commit f00ede4

Browse files
authored
Allow PulsarTopicBuilder#init null values (#777)
* Allow PulsarTopicBuilder#init null values See #756 * Clarify the deprecation of PulsarTopic.builder
1 parent e854e78 commit f00ede4

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

spring-pulsar-docs/src/main/antora/modules/ROOT/pages/whats-new.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ See xref:./reference/default-tenant-namespace.adoc[Default Tenant / Namespace] f
2020
Version `3.3.1` of the Pulsar client deprecates the `getPartitionsForTopic(java.lang.String)` in favor of `getPartitionsForTopic(java.lang.String, boolean metadataAutoCreationEnabled)`.
2121

2222
==== PulsarTopic#builder
23-
The `PulsarTopicBuilder` is now a registered bean that is configured with default values for domain, tenant, and namespace.
24-
As such, this convenience method is no longer needed.
25-
Instead, inject the builder bean where needed.
23+
When using Spring Boot the `PulsarTopicBuilder` is now a registered bean that is configured with default values for domain, tenant, and namespace.
24+
Therefore, if you are using Spring Boot, you can simply inject the builder where needed.
25+
Otherwise, use one of the `PulsarTopicBuilder` constructors directly.
2626

2727
=== Breaking Changes
2828

spring-pulsar/src/main/java/org/springframework/pulsar/core/PulsarTopic.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ public record PulsarTopic(String topicName, int numberOfPartitions) {
6161
* Convenience method to create a topic builder with the specified topic name.
6262
* @param topicName the name of the topic
6363
* @return the topic builder instance
64-
* @deprecated As of version 1.2.0 topic builder is a registered bean - instead use an
65-
* injected instance where needed
64+
* @deprecated since 1.2.0 for removal in 1.4.0 in favor of
65+
* {@link PulsarTopicBuilder#PulsarTopicBuilder()} or
66+
* {@link PulsarTopicBuilder#PulsarTopicBuilder(TopicDomain, String, String)}
6667
*/
6768
@Deprecated(since = "1.2.0", forRemoval = true)
6869
public static PulsarTopicBuilder builder(String topicName) {

spring-pulsar/src/main/java/org/springframework/pulsar/core/PulsarTopicBuilder.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.lang.Nullable;
2222
import org.springframework.util.Assert;
23+
import org.springframework.util.StringUtils;
2324

2425
/**
2526
* Builder class to create {@link PulsarTopic} instances.
@@ -60,17 +61,18 @@ public PulsarTopicBuilder() {
6061

6162
/**
6263
* Create a builder instance that uses the specified defaults.
63-
* @param defaultDomain domain to use for the topic when not present in the name
64-
* @param defaultTenant tentant to use for the topic when not present in the name
65-
* @param defaultNamespace namespace to use for the topic when not present in the name
64+
* @param defaultDomain domain to use when topic name is not fully-qualified
65+
* @param defaultTenant tenant to use when topic name is not fully-qualified or null
66+
* to use the Pulsar default tenant of 'public'
67+
* @param defaultNamespace namespace to use when topic name is not fully-qualified or
68+
* null to use the Pulsar default namespace of 'namespace'
6669
*/
67-
public PulsarTopicBuilder(TopicDomain defaultDomain, String defaultTenant, String defaultNamespace) {
70+
public PulsarTopicBuilder(TopicDomain defaultDomain, @Nullable String defaultTenant,
71+
@Nullable String defaultNamespace) {
6872
Assert.notNull(defaultDomain, "defaultDomain must not be null");
69-
Assert.hasText(defaultTenant, "defaultTenant must be specified");
70-
Assert.hasText(defaultNamespace, "defaultNamespace must be specified");
7173
this.defaultDomain = defaultDomain;
72-
this.defaultTenant = defaultTenant;
73-
this.defaultNamespace = defaultNamespace;
74+
this.defaultTenant = StringUtils.hasText(defaultTenant) ? defaultTenant : DEFAULT_TENANT;
75+
this.defaultNamespace = StringUtils.hasText(defaultNamespace) ? defaultNamespace : DEFAULT_NAMESPACE;
7476
}
7577

7678
/**

spring-pulsar/src/test/java/org/springframework/pulsar/core/PulsarTopicBuilderTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,18 @@ private static Stream<Arguments> nameIsAlwaysFullyQualifiedProvider() {
8080
Arguments.of(customBuilder, "persistent://foo/bar/my-topic", "persistent://foo/bar/my-topic"));
8181
}
8282

83+
@Test
84+
void whenConstructedWithNullTenantThenPulsarDefaultTenantIsUsed() {
85+
var topicBuilder = new PulsarTopicBuilder(TopicDomain.persistent, null, "foo");
86+
var fqTopic = topicBuilder.name("my-topic").build();
87+
assertThat(fqTopic.topicName()).isEqualTo("persistent://public/foo/my-topic");
88+
}
89+
90+
@Test
91+
void whenConstructedWithNullNamespaceThenPulsarDefaultNamespaceIsUsed() {
92+
var topicBuilder = new PulsarTopicBuilder(TopicDomain.persistent, "foo", null);
93+
var fqTopic = topicBuilder.name("my-topic").build();
94+
assertThat(fqTopic.topicName()).isEqualTo("persistent://foo/default/my-topic");
95+
}
96+
8397
}

0 commit comments

Comments
 (0)