Example usage for org.springframework.kafka KafkaException KafkaException

List of usage examples for org.springframework.kafka KafkaException KafkaException

Introduction

In this page you can find the example usage for org.springframework.kafka KafkaException KafkaException.

Prototype

public KafkaException(String message, @Nullable Throwable cause) 

Source Link

Usage

From source file:org.springframework.kafka.config.StreamsBuilderFactoryBean.java

@SuppressWarnings("deprecation")
@Override//from w ww . j  a v  a  2  s .  com
public synchronized void start() {
    if (!this.running) {
        try {
            Assert.state(this.streamsConfig != null || this.properties != null,
                    "'streamsConfig' or streams configuration properties must not be null");
            Topology topology = getObject().build(); // NOSONAR
            if (logger.isDebugEnabled()) {
                logger.debug(topology.describe());
            }
            if (this.properties != null) {
                this.kafkaStreams = new KafkaStreams(topology, this.properties, this.clientSupplier);
            } else {
                this.kafkaStreams = new KafkaStreams(topology, this.streamsConfig, this.clientSupplier);
            }
            this.kafkaStreams.setStateListener(this.stateListener);
            this.kafkaStreams.setGlobalStateRestoreListener(this.stateRestoreListener);
            this.kafkaStreams.setUncaughtExceptionHandler(this.uncaughtExceptionHandler);
            if (this.kafkaStreamsCustomizer != null) {
                this.kafkaStreamsCustomizer.customize(this.kafkaStreams);
            }
            if (this.cleanupConfig.cleanupOnStart()) {
                this.kafkaStreams.cleanUp();
            }
            this.kafkaStreams.start();
            this.running = true;
        } catch (Exception e) {
            throw new KafkaException("Could not start stream: ", e);
        }
    }
}

From source file:org.springframework.kafka.core.KafkaAdmin.java

private Map<String, NewPartitions> checkPartitions(Map<String, NewTopic> topicNameToTopic,
        DescribeTopicsResult topicInfo, List<NewTopic> topicsToAdd) {
    Map<String, NewPartitions> topicsToModify = new HashMap<>();
    topicInfo.values().forEach((n, f) -> {
        NewTopic topic = topicNameToTopic.get(n);
        try {//from   w w w.j  a  v a  2s  .  c  o m
            TopicDescription topicDescription = f.get(this.operationTimeout, TimeUnit.SECONDS);
            if (topic.numPartitions() < topicDescription.partitions().size()) {
                if (logger.isInfoEnabled()) {
                    logger.info(
                            String.format("Topic '%s' exists but has a different partition count: %d not %d", n,
                                    topicDescription.partitions().size(), topic.numPartitions()));
                }
            } else if (topic.numPartitions() > topicDescription.partitions().size()) {
                if (logger.isInfoEnabled()) {
                    logger.info(String.format(
                            "Topic '%s' exists but has a different partition count: %d not %d, increasing "
                                    + "if the broker supports it",
                            n, topicDescription.partitions().size(), topic.numPartitions()));
                }
                topicsToModify.put(n, NewPartitions.increaseTo(topic.numPartitions()));
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (TimeoutException e) {
            throw new KafkaException("Timed out waiting to get existing topics", e);
        } catch (ExecutionException e) {
            topicsToAdd.add(topic);
        }
    });
    return topicsToModify;
}

From source file:org.springframework.kafka.core.KafkaAdmin.java

private void addTopics(AdminClient adminClient, List<NewTopic> topicsToAdd) {
    CreateTopicsResult topicResults = adminClient.createTopics(topicsToAdd);
    try {/*  w ww  .  ja  v  a2s . c  o  m*/
        topicResults.all().get(this.operationTimeout, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        logger.error("Interrupted while waiting for topic creation results", e);
    } catch (TimeoutException e) {
        throw new KafkaException("Timed out waiting for create topics results", e);
    } catch (ExecutionException e) {
        logger.error("Failed to create topics", e.getCause());
        throw new KafkaException("Failed to create topics", e.getCause()); // NOSONAR
    }
}

From source file:org.springframework.kafka.core.KafkaAdmin.java

private void modifyTopics(AdminClient adminClient, Map<String, NewPartitions> topicsToModify) {
    CreatePartitionsResult partitionsResult = adminClient.createPartitions(topicsToModify);
    try {/*from  w w w. java 2s  . c o  m*/
        partitionsResult.all().get(this.operationTimeout, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        logger.error("Interrupted while waiting for partition creation results", e);
    } catch (TimeoutException e) {
        throw new KafkaException("Timed out waiting for create partitions results", e);
    } catch (ExecutionException e) {
        logger.error("Failed to create partitions", e.getCause());
        if (!(e.getCause() instanceof UnsupportedVersionException)) {
            throw new KafkaException("Failed to create partitions", e.getCause()); // NOSONAR
        }
    }
}

From source file:org.springframework.kafka.listener.SeekToCurrentErrorHandler.java

@Override
public void handle(Exception thrownException, List<ConsumerRecord<?, ?>> records, Consumer<?, ?> consumer,
        MessageListenerContainer container) {
    if (!SeekUtils.doSeeks(records, consumer, thrownException, true, this.failureTracker::skip, logger)) {
        throw new KafkaException("Seek to current after exception", thrownException);
    }//from  w  ww. j a  v a2 s  .co  m
}