Search in sources :

Example 1 with PublishedEvent

use of com.kloia.eventapis.common.PublishedEvent in project eventapis by kloiasoft.

the class AutomaticTopicConfiguration method init.

@PostConstruct
public void init() {
    AdminClient adminClient = adminClient();
    try {
        StopWatch stopWatch = new StopWatch("CheckAndCreateTopics");
        stopWatch.start("CheckAndCreateTopics");
        ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
        provider.addIncludeFilter(new AssignableTypeFilter(PublishedEvent.class));
        // provider.addExcludeFilter(new AssignableTypeFilter(ReceivedEvent.class));
        Set<BeanDefinition> candidateComponents = provider.findCandidateComponents(eventApisConfiguration.getBaseEventsPackage());
        int numberOfNodes = 1;
        try {
            Collection<Node> nodes = adminClient.describeCluster().nodes().get();
            numberOfNodes = nodes.size();
        } catch (InterruptedException | ExecutionException e) {
            log.warn("Error while finding number of Nodes:" + e.getMessage(), e);
        }
        for (BeanDefinition candidateComponent : candidateComponents) {
            Class<PublishedEvent> beanClass;
            try {
                beanClass = (Class<PublishedEvent>) Class.forName(candidateComponent.getBeanClassName());
                String topicName = beanClass.getSimpleName();
                log.info("Candidate {} to Create Topic:", topicName);
                try {
                    adminClient.describeTopics(Collections.singleton(topicName)).all().get();
                } catch (UnknownTopicOrPartitionException | ExecutionException exception) {
                    if (!(exception.getCause() instanceof UnknownTopicOrPartitionException))
                        throw exception;
                    log.warn("Topic {} does not exists, trying to create", topicName);
                    try {
                        adminClient.createTopics(Collections.singleton(new NewTopic(topicName, numberOfNodes, (short) 1)));
                        log.info("Topic {} is Created Successfully:", topicName);
                    } catch (Exception topicCreationEx) {
                        log.warn("Error while creating Topic:" + topicCreationEx.getMessage(), topicCreationEx);
                    }
                }
            } catch (ClassNotFoundException | InterruptedException | ExecutionException exception) {
                log.warn("Error while checking Topic:" + candidateComponent.toString() + " message: " + exception.getMessage(), exception);
            }
        }
        stopWatch.stop();
        log.debug(stopWatch.prettyPrint());
    } finally {
        adminClient.close();
    }
}
Also used : Node(org.apache.kafka.common.Node) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) PublishedEvent(com.kloia.eventapis.common.PublishedEvent) ExecutionException(java.util.concurrent.ExecutionException) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) StopWatch(org.springframework.util.StopWatch) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) NewTopic(org.apache.kafka.clients.admin.NewTopic) ExecutionException(java.util.concurrent.ExecutionException) AdminClient(org.apache.kafka.clients.admin.AdminClient) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter) PostConstruct(javax.annotation.PostConstruct)

Aggregations

PublishedEvent (com.kloia.eventapis.common.PublishedEvent)1 ExecutionException (java.util.concurrent.ExecutionException)1 PostConstruct (javax.annotation.PostConstruct)1 AdminClient (org.apache.kafka.clients.admin.AdminClient)1 NewTopic (org.apache.kafka.clients.admin.NewTopic)1 Node (org.apache.kafka.common.Node)1 UnknownTopicOrPartitionException (org.apache.kafka.common.errors.UnknownTopicOrPartitionException)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)1 AssignableTypeFilter (org.springframework.core.type.filter.AssignableTypeFilter)1 StopWatch (org.springframework.util.StopWatch)1