All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dropwizard.kafka.managed.KafkaAdminClientManager Maven / Gradle / Ivy

package io.dropwizard.kafka.managed;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import io.dropwizard.lifecycle.Managed;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.NewTopic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.Objects.requireNonNull;

public class KafkaAdminClientManager implements Managed {
    private static final Logger log = LoggerFactory.getLogger(KafkaAdminClientManager.class);

    private final AdminClient adminClient;
    private final String name;
    private final Collection topics;

    public KafkaAdminClientManager(final AdminClient adminClient, final String name, final Collection topics) {
        this.adminClient = requireNonNull(adminClient);
        this.name = requireNonNull(name);
        this.topics = topics;
    }

    @Override
    public void start() throws Exception {
        log.info("Starting adminClient for name={}", name);
        if (!this.topics.isEmpty()) {
            log.trace("Searching existing topics in cluster.");
            final Set existingTopics = this.adminClient.listTopics().names().get();
            final List matchingTopics = new ArrayList<>();
            for (String t : existingTopics) {
                this.topics.removeIf(newTopic -> {
                    boolean match = newTopic.name().equals(t);
                    if (match) {
                        matchingTopics.add(t);
                    }
                    return match;
                });
            }
            if (!matchingTopics.isEmpty()) {
                log.info("Not attempting to re-create existing topics {}.", matchingTopics);
            }
            this.adminClient.createTopics(this.topics);
        }
    }

    @Override
    public void stop() throws Exception {
        log.info("Shutting down adminClient for name={}", name);
        adminClient.close();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy