com.clivern.kafka.Utils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kafka-sdk Show documentation
Show all versions of kafka-sdk Show documentation
A Java SDK for Apache Kafka
The newest version!
/*
* Copyright (C) 2021 Clivern
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.clivern.kafka;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.common.errors.TopicExistsException;
/** Utils Class */
public class Utils {
/**
* Create a Topic
*
* @param topic a topic
* @param configs kafka configs
* @throws RuntimeException throws exception if creation failed
*/
public static void createTopic(String topic, Configs configs) {
NewTopic newTopic = new NewTopic(topic, Optional.empty(), Optional.empty());
try (AdminClient adminClient = AdminClient.create(configs.getProperties())) {
adminClient.createTopics(Collections.singletonList(newTopic)).all().get();
} catch (InterruptedException | ExecutionException e) {
// Ignore if TopicExistsException, which may be valid if topic exists
if (!(e.getCause() instanceof TopicExistsException)) {
throw new RuntimeException(e);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy