Example usage for org.springframework.kafka.support TopicPartitionInitialOffset TopicPartitionInitialOffset

List of usage examples for org.springframework.kafka.support TopicPartitionInitialOffset TopicPartitionInitialOffset

Introduction

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

Prototype

public TopicPartitionInitialOffset(String topic, int partition, Long offset, @Nullable SeekPosition position) 

Source Link

Document

Construct an instance with the provided SeekPosition .

Usage

From source file:org.springframework.kafka.annotation.KafkaListenerAnnotationBeanPostProcessor.java

private List<TopicPartitionInitialOffset> resolveTopicPartitionsList(TopicPartition topicPartition) {
    Object topic = resolveExpression(topicPartition.topic());
    Assert.state(topic instanceof String,
            "topic in @TopicPartition must resolve to a String, not " + topic.getClass());
    Assert.state(StringUtils.hasText((String) topic), "topic in @TopicPartition must not be empty");
    String[] partitions = topicPartition.partitions();
    PartitionOffset[] partitionOffsets = topicPartition.partitionOffsets();
    Assert.state(partitions.length > 0 || partitionOffsets.length > 0,
            "At least one 'partition' or 'partitionOffset' required in @TopicPartition for topic '" + topic
                    + "'");
    List<TopicPartitionInitialOffset> result = new ArrayList<>();
    for (int i = 0; i < partitions.length; i++) {
        resolvePartitionAsInteger((String) topic, resolveExpression(partitions[i]), result);
    }/*  w w  w  .  j  a  v a  2  s  . c o  m*/

    for (PartitionOffset partitionOffset : partitionOffsets) {
        Object partitionValue = resolveExpression(partitionOffset.partition());
        Integer partition;
        if (partitionValue instanceof String) {
            Assert.state(StringUtils.hasText((String) partitionValue),
                    "partition in @PartitionOffset for topic '" + topic + "' cannot be empty");
            partition = Integer.valueOf((String) partitionValue);
        } else if (partitionValue instanceof Integer) {
            partition = (Integer) partitionValue;
        } else {
            throw new IllegalArgumentException(String.format(
                    "@PartitionOffset for topic '%s' can't resolve '%s' as an Integer or String, resolved to '%s'",
                    topic, partitionOffset.partition(), partitionValue.getClass()));
        }

        Object initialOffsetValue = resolveExpression(partitionOffset.initialOffset());
        Long initialOffset;
        if (initialOffsetValue instanceof String) {
            Assert.state(StringUtils.hasText((String) initialOffsetValue),
                    "'initialOffset' in @PartitionOffset for topic '" + topic + "' cannot be empty");
            initialOffset = Long.valueOf((String) initialOffsetValue);
        } else if (initialOffsetValue instanceof Long) {
            initialOffset = (Long) initialOffsetValue;
        } else {
            throw new IllegalArgumentException(String.format(
                    "@PartitionOffset for topic '%s' can't resolve '%s' as a Long or String, resolved to '%s'",
                    topic, partitionOffset.initialOffset(), initialOffsetValue.getClass()));
        }

        Object relativeToCurrentValue = resolveExpression(partitionOffset.relativeToCurrent());
        Boolean relativeToCurrent;
        if (relativeToCurrentValue instanceof String) {
            relativeToCurrent = Boolean.valueOf((String) relativeToCurrentValue);
        } else if (relativeToCurrentValue instanceof Boolean) {
            relativeToCurrent = (Boolean) relativeToCurrentValue;
        } else {
            throw new IllegalArgumentException(String.format(
                    "@PartitionOffset for topic '%s' can't resolve '%s' as a Boolean or String, resolved to '%s'",
                    topic, partitionOffset.relativeToCurrent(), relativeToCurrentValue.getClass()));
        }

        TopicPartitionInitialOffset topicPartitionOffset = new TopicPartitionInitialOffset((String) topic,
                partition, initialOffset, relativeToCurrent);
        if (!result.contains(topicPartitionOffset)) {
            result.add(topicPartitionOffset);
        } else {
            throw new IllegalArgumentException(
                    String.format("@TopicPartition can't have the same partition configuration twice: [%s]",
                            topicPartitionOffset));
        }
    }
    return result;
}