Skip to content

Commit 1126c46

Browse files
jonas-grgtonobc
authored andcommitted
Favor unchecked exceptions in APIs
This commit replaces usages of the checked PulsarClientException with the newly introduced unchecked PulsarException. Resolves #547
1 parent 6182d15 commit 1126c46

15 files changed

+149
-95
lines changed

spring-pulsar/src/main/java/org/springframework/pulsar/PulsarException.java

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Spring Pulsar specific {@link NestedRuntimeException} implementation.
2323
*
2424
* @author Soby Chacko
25+
* @author Jonas Geiregat
2526
*/
2627
public class PulsarException extends NestedRuntimeException {
2728

@@ -33,4 +34,8 @@ public PulsarException(String msg, Throwable cause) {
3334
super(msg, cause);
3435
}
3536

37+
public PulsarException(Throwable cause) {
38+
this(cause.getMessage(), cause);
39+
}
40+
3641
}

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

-5
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,10 @@ protected Producer<T> doCreateProducer(Schema<T> schema, @Nullable String topic,
108108

109109
private Producer<T> createCacheableProducer(Schema<T> schema, String topic,
110110
@Nullable Collection<String> encryptionKeys, @Nullable List<ProducerBuilderCustomizer<T>> customizers) {
111-
try {
112111
var producer = super.doCreateProducer(schema, topic, encryptionKeys, customizers);
113112
return new ProducerWithCloseCallback<>(producer,
114113
(p) -> this.logger.trace(() -> "Client closed producer %s but will skip actual closing"
115114
.formatted(ProducerUtils.formatProducer(producer))));
116-
}
117-
catch (PulsarClientException ex) {
118-
throw new RuntimeException(ex);
119-
}
120115
}
121116

122117
/**

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.context.EnvironmentAware;
2323
import org.springframework.core.env.Environment;
2424
import org.springframework.core.log.LogAccessor;
25+
import org.springframework.pulsar.PulsarException;
2526
import org.springframework.util.Assert;
2627

2728
/**
@@ -57,14 +58,19 @@ public DefaultPulsarClientFactory(PulsarClientBuilderCustomizer customizer) {
5758
}
5859

5960
@Override
60-
public PulsarClient createClient() throws PulsarClientException {
61+
public PulsarClient createClient() {
6162
if (this.useRestartableClient) {
6263
this.logger.info(() -> "Using restartable client");
6364
return new PulsarClientProxy(this.customizer);
6465
}
6566
var clientBuilder = PulsarClient.builder();
6667
this.customizer.customize(clientBuilder);
67-
return clientBuilder.build();
68+
try {
69+
return clientBuilder.build();
70+
}
71+
catch (PulsarClientException ex) {
72+
throw new PulsarException(ex);
73+
}
6874
}
6975

7076
@Override

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

+20-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.pulsar.client.impl.ConsumerBuilderImpl;
3333

3434
import org.springframework.lang.Nullable;
35+
import org.springframework.pulsar.PulsarException;
3536
import org.springframework.util.CollectionUtils;
3637

3738
/**
@@ -42,6 +43,7 @@
4243
* @author Alexander Preuß
4344
* @author Christophe Bornet
4445
* @author Chris Bono
46+
* @author Jonas Geiregat
4547
*/
4648
public class DefaultPulsarConsumerFactory<T> implements PulsarConsumerFactory<T> {
4749

@@ -64,15 +66,23 @@ public DefaultPulsarConsumerFactory(PulsarClient pulsarClient,
6466

6567
@Override
6668
public Consumer<T> createConsumer(Schema<T> schema, @Nullable Collection<String> topics,
67-
@Nullable String subscriptionName, ConsumerBuilderCustomizer<T> customizer) throws PulsarClientException {
68-
return createConsumer(schema, topics, subscriptionName, null,
69-
customizer != null ? Collections.singletonList(customizer) : null);
69+
@Nullable String subscriptionName, ConsumerBuilderCustomizer<T> customizer) {
70+
try {
71+
return createConsumer(schema, topics, subscriptionName, null,
72+
customizer != null ? Collections.singletonList(customizer) : null);
73+
}
74+
catch (PulsarException ex) {
75+
throw ex;
76+
}
77+
catch (Exception ex) {
78+
throw new PulsarException(PulsarClientException.unwrap(ex));
79+
}
7080
}
7181

7282
@Override
7383
public Consumer<T> createConsumer(Schema<T> schema, @Nullable Collection<String> topics,
7484
@Nullable String subscriptionName, @Nullable Map<String, String> metadataProperties,
75-
@Nullable List<ConsumerBuilderCustomizer<T>> customizers) throws PulsarClientException {
85+
@Nullable List<ConsumerBuilderCustomizer<T>> customizers) {
7686
Objects.requireNonNull(schema, "Schema must be specified");
7787
ConsumerBuilder<T> consumerBuilder = this.pulsarClient.newConsumer(schema);
7888

@@ -92,7 +102,12 @@ public Consumer<T> createConsumer(Schema<T> schema, @Nullable Collection<String>
92102
if (!CollectionUtils.isEmpty(customizers)) {
93103
customizers.forEach(customizer -> customizer.customize(consumerBuilder));
94104
}
95-
return consumerBuilder.subscribe();
105+
try {
106+
return consumerBuilder.subscribe();
107+
}
108+
catch (PulsarClientException ex) {
109+
throw new PulsarException(ex);
110+
}
96111
}
97112

98113
private void replaceTopicsOnBuilder(ConsumerBuilder<T> builder, Collection<String> topics) {

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

+30-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import org.springframework.core.log.LogAccessor;
3333
import org.springframework.lang.Nullable;
34+
import org.springframework.pulsar.PulsarException;
3435
import org.springframework.util.CollectionUtils;
3536

3637
/**
@@ -102,21 +103,37 @@ public DefaultPulsarProducerFactory(PulsarClient pulsarClient, @Nullable String
102103
}
103104

104105
@Override
105-
public Producer<T> createProducer(Schema<T> schema, @Nullable String topic) throws PulsarClientException {
106+
public Producer<T> createProducer(Schema<T> schema, @Nullable String topic) {
106107
return doCreateProducer(schema, topic, null, null);
107108
}
108109

109110
@Override
110111
public Producer<T> createProducer(Schema<T> schema, @Nullable String topic,
111-
@Nullable ProducerBuilderCustomizer<T> customizer) throws PulsarClientException {
112-
return doCreateProducer(schema, topic, null, customizer != null ? Collections.singletonList(customizer) : null);
112+
@Nullable ProducerBuilderCustomizer<T> customizer) {
113+
try {
114+
return doCreateProducer(schema, topic, null,
115+
customizer != null ? Collections.singletonList(customizer) : null);
116+
}
117+
catch (PulsarException ex) {
118+
throw ex;
119+
}
120+
catch (Exception ex) {
121+
throw new PulsarException(PulsarClientException.unwrap(ex));
122+
}
113123
}
114124

115125
@Override
116126
public Producer<T> createProducer(Schema<T> schema, @Nullable String topic,
117-
@Nullable Collection<String> encryptionKeys, @Nullable List<ProducerBuilderCustomizer<T>> customizers)
118-
throws PulsarClientException {
119-
return doCreateProducer(schema, topic, encryptionKeys, customizers);
127+
@Nullable Collection<String> encryptionKeys, @Nullable List<ProducerBuilderCustomizer<T>> customizers) {
128+
try {
129+
return doCreateProducer(schema, topic, encryptionKeys, customizers);
130+
}
131+
catch (PulsarException ex) {
132+
throw ex;
133+
}
134+
catch (Exception ex) {
135+
throw new PulsarException(PulsarClientException.unwrap(ex));
136+
}
120137
}
121138

122139
/**
@@ -134,8 +151,7 @@ public Producer<T> createProducer(Schema<T> schema, @Nullable String topic,
134151
* @throws PulsarClientException if any error occurs
135152
*/
136153
protected Producer<T> doCreateProducer(Schema<T> schema, @Nullable String topic,
137-
@Nullable Collection<String> encryptionKeys, @Nullable List<ProducerBuilderCustomizer<T>> customizers)
138-
throws PulsarClientException {
154+
@Nullable Collection<String> encryptionKeys, @Nullable List<ProducerBuilderCustomizer<T>> customizers) {
139155
Objects.requireNonNull(schema, "Schema must be specified");
140156
var resolvedTopic = resolveTopicName(topic);
141157
this.logger.trace(() -> "Creating producer for '%s' topic".formatted(resolvedTopic));
@@ -156,7 +172,12 @@ protected Producer<T> doCreateProducer(Schema<T> schema, @Nullable String topic,
156172
}
157173
producerBuilder.topic(resolvedTopic);
158174

159-
return producerBuilder.create();
175+
try {
176+
return producerBuilder.create();
177+
}
178+
catch (PulsarClientException ex) {
179+
throw new PulsarException(ex);
180+
}
160181
}
161182

162183
protected String resolveTopicName(String userSpecifiedTopic) {

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
package org.springframework.pulsar.core;
1818

1919
import org.apache.pulsar.client.api.PulsarClient;
20-
import org.apache.pulsar.client.api.PulsarClientException;
20+
21+
import org.springframework.pulsar.PulsarException;
2122

2223
/**
2324
* Pulsar client factory interface.
@@ -30,8 +31,8 @@ public interface PulsarClientFactory {
3031
/**
3132
* Create a client.
3233
* @return the created client instance
33-
* @throws PulsarClientException if an error occurs creating the client
34+
* @throws PulsarException if an error occurs creating the client
3435
*/
35-
PulsarClient createClient() throws PulsarClientException;
36+
PulsarClient createClient();
3637

3738
}

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.pulsar.client.api.Schema;
2727

2828
import org.springframework.lang.Nullable;
29+
import org.springframework.pulsar.PulsarException;
2930

3031
/**
3132
* Pulsar consumer factory interface.
@@ -34,6 +35,7 @@
3435
* @author Soby Chacko
3536
* @author Christophe Bornet
3637
* @author Chris Bono
38+
* @author Jonas Geiregat
3739
*/
3840
public interface PulsarConsumerFactory<T> {
3941

@@ -53,10 +55,11 @@ public interface PulsarConsumerFactory<T> {
5355
* that the customizer is applied last and has the potential for overriding any
5456
* specified parameters or default properties.
5557
* @return the consumer
56-
* @throws PulsarClientException if any error occurs
58+
* @throws PulsarException if any {@link PulsarClientException} occurs communicating
59+
* with Pulsar
5760
*/
5861
Consumer<T> createConsumer(Schema<T> schema, @Nullable Collection<String> topics, @Nullable String subscriptionName,
59-
ConsumerBuilderCustomizer<T> customizer) throws PulsarClientException;
62+
ConsumerBuilderCustomizer<T> customizer);
6063

6164
/**
6265
* Create a consumer.
@@ -79,10 +82,10 @@ Consumer<T> createConsumer(Schema<T> schema, @Nullable Collection<String> topics
7982
* builder. Note that the customizers are applied last and have the potential for
8083
* overriding any specified parameters or default properties.
8184
* @return the consumer
82-
* @throws PulsarClientException if any error occurs
85+
* @throws PulsarException if any {@link PulsarClientException} occurs communicating
86+
* with Pulsar
8387
*/
8488
Consumer<T> createConsumer(Schema<T> schema, @Nullable Collection<String> topics, @Nullable String subscriptionName,
85-
@Nullable Map<String, String> metadataProperties, @Nullable List<ConsumerBuilderCustomizer<T>> customizers)
86-
throws PulsarClientException;
89+
@Nullable Map<String, String> metadataProperties, @Nullable List<ConsumerBuilderCustomizer<T>> customizers);
8790

8891
}

0 commit comments

Comments
 (0)