My ultimate goal is to find the number of partitions for a Pulsar topic (Getting number of partitions of a Pulsar topic: could not be parsed into a proper Uri, missing scheme) but in the process of an alternative method, encountered something peculiar.
I have a hack where I loop up through the max number of partitions and keep trying to create a reader until an error happens.
for (i in 0..MAX_NUM_PARTITIONS) {
val topicPartition = "$topic-partition-$i"
var partitionReader: Reader<ByteArray>? = null
try {
partitionReader =
client
.newReader(BYTES)
.startMessageId(MessageId.latest)
.topic(topicPartition)
.create()
} catch (e: Exception) {
// TODO: This still prints a CompletionException error to the console unnecessarily.
// Either catch that or calculate # partitions in a cleaner way.
break
}
if (partitionReader != null) {
readers.add(partitionReader)
}
}
java.util.concurrent.CompletionException:
org.apache.pulsar.client.api.PulsarClientException$NotAllowedException:
{“errorMsg”:”Illegal topic partition name …
I would like to catch the exception above and not have it print to the console. Despite the try-catch above, it still prints. Why? Is this a Kotlin coroutine related phenomenon? The code does run in a suspend function.
1