Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check to see if the user can see the topic before creating it, #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,25 @@ object KafkaMessagingProvider extends MessagingProvider {
val nt = new NewTopic(topic, partitions, kafkaConfig.replicationFactor).configs(topicConfig.asJava)

def createTopic(retries: Int = 5): Try[Unit] = {
Try(client.createTopics(List(nt).asJava).values().get(topic).get())
.map(_ => logging.info(this, s"created topic $topic"))
.recoverWith {
case CausedBy(_: TopicExistsException) =>
Success(logging.info(this, s"topic $topic already existed"))
case CausedBy(t: RetriableException) if retries > 0 =>
logging.warn(this, s"topic $topic could not be created because of $t, retries left: $retries")
Thread.sleep(1.second.toMillis)
createTopic(retries - 1)
case t =>
logging.error(this, s"ensureTopic for $topic failed due to $t")
Failure(t)
}
Try(client.listTopics().names().get())
.map(topics =>
if (topics.contains(topic)) {
Success(logging.info(this, s"$topic already exists and the user can see it, skipping creation."))
} else {
Try(client.createTopics(List(nt).asJava).values().get(topic).get())
.map(_ => logging.info(this, s"created topic $topic"))
.recoverWith {
case CausedBy(_: TopicExistsException) =>
Success(logging.info(this, s"topic $topic already existed"))
case CausedBy(t: RetriableException) if retries > 0 =>
logging.warn(this, s"topic $topic could not be created because of $t, retries left: $retries")
Thread.sleep(1.second.toMillis)
createTopic(retries - 1)
case t =>
logging.error(this, s"ensureTopic for $topic failed due to $t")
Failure(t)
}
})
}

val result = createTopic()
Expand Down