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

reactor.kafka.receiver.ImmutableReceiverOptions Maven / Gradle / Ivy

There is a newer version: 1.3.23
Show newest version
/*
 * Copyright (c) 2016-2023 VMware Inc. or its affiliates, All Rights Reserved.
 *
 * 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
 *
 *   https://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 reactor.kafka.receiver;

import io.micrometer.observation.ObservationRegistry;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.serialization.Deserializer;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
import reactor.kafka.receiver.observation.KafkaReceiverObservationConvention;
import reactor.util.annotation.Nullable;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

class ImmutableReceiverOptions implements ReceiverOptions {

    private static final Duration DEFAULT_POLL_TIMEOUT = Duration.ofMillis(100);
    private static final int DEFAULT_MAX_COMMIT_ATTEMPTS = 100;
    private static final Duration DEFAULT_COMMIT_RETRY_INTERVAL = Duration.ofMillis(500);

    private final Map properties;
    private final List>> assignListeners;
    private final List>> revokeListeners;

    private final Deserializer keyDeserializer;
    private final Deserializer valueDeserializer;

    private final Duration pollTimeout;
    private final Duration closeTimeout;
    private final Duration commitInterval;
    private final int commitBatchSize;
    private final int atmostOnceCommitAheadSize;
    private final int maxCommitAttempts;
    private final Duration commitRetryInterval;
    private final int maxDeferredCommits;
    private final Duration maxDelayRebalance;
    private final long commitIntervalDuringDelay;
    private final Collection subscribeTopics;
    private final Collection assignTopicPartitions;
    private final Pattern subscribePattern;
    private final Supplier schedulerSupplier;
    private final ConsumerListener consumerListener;
    private final boolean pauseAllAfterRebalance;

    private final ObservationRegistry observationRegistry;

    @Nullable
    private final KafkaReceiverObservationConvention observationConvention;

    ImmutableReceiverOptions() {
        this(new HashMap<>());
    }

    ImmutableReceiverOptions(Properties properties) {
        this(
            properties
                .entrySet()
                .stream()
                .collect(Collectors.toMap(
                    e -> e.getKey().toString(),
                    Map.Entry::getValue
                ))
        );
    }

    ImmutableReceiverOptions(Map properties) {
        this.properties = new HashMap<>(properties);
        assignListeners = new ArrayList<>();
        revokeListeners = new ArrayList<>();
        keyDeserializer = null;
        valueDeserializer = null;
        pollTimeout = DEFAULT_POLL_TIMEOUT;
        closeTimeout = Duration.ofNanos(Long.MAX_VALUE);
        commitInterval = Duration.ofMillis(5000); // Kafka default
        commitBatchSize = 0;
        atmostOnceCommitAheadSize = 0;
        maxCommitAttempts = DEFAULT_MAX_COMMIT_ATTEMPTS;
        commitRetryInterval = DEFAULT_COMMIT_RETRY_INTERVAL;
        maxDeferredCommits = 0;
        maxDelayRebalance = Duration.ofSeconds(60);
        commitIntervalDuringDelay = 100L;
        subscribeTopics = null;
        assignTopicPartitions = null;
        subscribePattern = null;
        this.properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
        schedulerSupplier = Schedulers::immediate;
        consumerListener = null;
        pauseAllAfterRebalance = false;

        observationRegistry = ObservationRegistry.NOOP;
        observationConvention = null;
    }

    ImmutableReceiverOptions(
        Map properties,
        List>> assignListeners,
        List>> revokeListeners,
        Deserializer deserializer,
        Deserializer valueDeserializer,
        Duration pollTimeout,
        Duration closeTimeout,
        Duration commitInterval,
        int commitBatchSize,
        int atmostOnceCommitAheadSize,
        int maxCommitAttempts,
        Duration commitRetryInterval,
        int maxDeferredCommits,
        Duration maxDelayRebalance,
        long commitIntervalDuringDelay,
        Collection topics,
        Collection partitions,
        Pattern pattern,
        Supplier supplier,
        ConsumerListener consumerListener,
        boolean pauseAllAfterRebalance,
        ObservationRegistry observationRegistry,
        KafkaReceiverObservationConvention observationConvention
    ) {
        this.properties = new HashMap<>(properties);
        this.assignListeners = new ArrayList<>(assignListeners);
        this.revokeListeners = new ArrayList<>(revokeListeners);
        this.keyDeserializer = deserializer;
        this.valueDeserializer = valueDeserializer;
        this.pollTimeout = pollTimeout;
        this.closeTimeout = closeTimeout;
        this.commitInterval = commitInterval;
        this.commitBatchSize = commitBatchSize;
        this.atmostOnceCommitAheadSize = atmostOnceCommitAheadSize;
        this.maxCommitAttempts = maxCommitAttempts;
        this.commitRetryInterval = commitRetryInterval;
        this.maxDeferredCommits = maxDeferredCommits;
        this.maxDelayRebalance = maxDelayRebalance;
        this.commitIntervalDuringDelay = commitIntervalDuringDelay;
        this.subscribeTopics = topics == null ? null : new HashSet<>(topics);
        this.assignTopicPartitions = partitions == null ? null : new HashSet<>(partitions);
        this.subscribePattern = pattern;
        this.schedulerSupplier = supplier;
        this.consumerListener = consumerListener;
        this.pauseAllAfterRebalance = pauseAllAfterRebalance;
        this.observationRegistry = observationRegistry;
        this.observationConvention = observationConvention;    }

    @Override
    public Map consumerProperties() {
        return new HashMap<>(properties);
    }

    @Override
    public Object consumerProperty(String name) {
        return properties.get(name);
    }

    @Override
    public ReceiverOptions consumerProperty(String name, Object newValue) {
        Objects.requireNonNull(name);
        Objects.requireNonNull(newValue);

        HashMap properties = new HashMap<>(this.properties);
        properties.put(name, newValue);

        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public ReceiverOptions withKeyDeserializer(Deserializer keyDeserializer) {
        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                Objects.requireNonNull(keyDeserializer),
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public Deserializer keyDeserializer() {
        return keyDeserializer;
    }

    @Override
    public ReceiverOptions withValueDeserializer(Deserializer valueDeserializer) {
        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                Objects.requireNonNull(valueDeserializer),
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public Deserializer valueDeserializer() {
        return valueDeserializer;
    }

    @Override
    public Duration pollTimeout() {
        return pollTimeout;
    }

    @Override
    public ReceiverOptions pollTimeout(Duration timeout) {
        if (timeout == null || timeout.isNegative())
            throw new IllegalArgumentException("Close timeout must be >= 0");

        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                Objects.requireNonNull(timeout),
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public Duration closeTimeout() {
        return closeTimeout;
    }

    @Override
    public ReceiverOptions closeTimeout(Duration timeout) {
        if (timeout == null || timeout.isNegative())
            throw new IllegalArgumentException("Close timeout must be >= 0");

        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                Objects.requireNonNull(timeout),
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public ReceiverOptions addAssignListener(Consumer> onAssign) {
        Objects.requireNonNull(onAssign);

        ArrayList>> assignListeners = new ArrayList<>(this.assignListeners);
        assignListeners.add(onAssign);

        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public ReceiverOptions addRevokeListener(Consumer> onRevoke) {
        Objects.requireNonNull(onRevoke);

        ArrayList>> revokeListeners = new ArrayList<>(this.revokeListeners);
        revokeListeners.add(onRevoke);

        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public ReceiverOptions clearAssignListeners() {
        return new ImmutableReceiverOptions<>(
                properties,
                new ArrayList<>(),
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public ReceiverOptions clearRevokeListeners() {
        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                new ArrayList<>(),
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public List>> assignListeners() {
        return new ArrayList<>(assignListeners);
    }

    @Override
    public List>> revokeListeners() {
        return new ArrayList<>(revokeListeners);
    }

    @Override
    public ReceiverOptions subscription(Collection topics) {
        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                Objects.requireNonNull(topics),
                null,
                null,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public ReceiverOptions subscription(Pattern pattern) {
        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                null,
                null,
                Objects.requireNonNull(pattern),
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public ReceiverOptions assignment(Collection partitions) {
        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                null,
                Objects.requireNonNull(partitions),
                null,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public Collection assignment() {
        return assignTopicPartitions == null ? null : new HashSet<>(assignTopicPartitions);
    }

    @Override
    public Collection subscriptionTopics() {
        return subscribeTopics == null ? null : new HashSet<>(subscribeTopics);
    }

    @Override
    public Pattern subscriptionPattern() {
        return subscribePattern;
    }

    @Override
    public String groupId() {
        return (String) consumerProperty(ConsumerConfig.GROUP_ID_CONFIG);
    }

    @Override
    public Duration heartbeatInterval() {
        long defaultValue = 3000; // Kafka default
        long heartbeatIntervalMs = getLongOption(ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG, defaultValue);
        return Duration.ofMillis(heartbeatIntervalMs);
    }

    @Override
    public Duration commitInterval() {
        return commitInterval;
    }

    @Override
    public ReceiverOptions commitInterval(Duration commitInterval) {
        if (commitInterval == null || commitInterval.isNegative())
            throw new IllegalArgumentException("Commit interval must be >= 0");

        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public int commitBatchSize() {
        return commitBatchSize;
    }

    @Override
    public ReceiverOptions commitBatchSize(int commitBatchSize) {
        if (commitBatchSize < 0)
            throw new IllegalArgumentException("Commit batch size must be >= 0");

        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public int atmostOnceCommitAheadSize() {
        return atmostOnceCommitAheadSize;
    }

    @Override
    public ReceiverOptions atmostOnceCommitAheadSize(int commitAheadSize) {
        if (commitAheadSize < 0)
            throw new IllegalArgumentException("Commit ahead size must be >= 0");

        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                commitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public int maxCommitAttempts() {
        return maxCommitAttempts;
    }

    @Override
    public ReceiverOptions maxCommitAttempts(int maxAttempts) {
        if (maxAttempts < 0)
            throw new IllegalArgumentException("the number of attempts must be >= 0");

        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public int maxDeferredCommits() {
        return maxDeferredCommits;
    }

    @Override
    public ReceiverOptions maxDeferredCommits(int maxDeferred) {
        return new ImmutableReceiverOptions<>(
            properties,
            assignListeners,
            revokeListeners,
            keyDeserializer,
            valueDeserializer,
            pollTimeout,
            closeTimeout,
            commitInterval,
            commitBatchSize,
            atmostOnceCommitAheadSize,
            maxCommitAttempts,
            commitRetryInterval,
            maxDeferred,
            maxDelayRebalance,
            commitIntervalDuringDelay,
            subscribeTopics,
            assignTopicPartitions,
            subscribePattern,
            schedulerSupplier,
            consumerListener,
            pauseAllAfterRebalance,
            observationRegistry,
            observationConvention
        );
    }

    @Override
    public Duration maxDelayRebalance() {
        return this.maxDelayRebalance;
    }

    @Override
    public ReceiverOptions maxDelayRebalance(Duration maxDelay) {
        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelay,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
            );
    }

    @Override
    public boolean pauseAllAfterRebalance() {
        return this.pauseAllAfterRebalance;
    }

    @Override
    public ReceiverOptions pauseAllAfterRebalance(boolean pauseAll) {
        return new ImmutableReceiverOptions<>(
            properties,
            assignListeners,
            revokeListeners,
            keyDeserializer,
            valueDeserializer,
            pollTimeout,
            closeTimeout,
            commitInterval,
            commitBatchSize,
            atmostOnceCommitAheadSize,
            maxCommitAttempts,
            commitRetryInterval,
            maxDeferredCommits,
            maxDelayRebalance,
            commitIntervalDuringDelay,
            subscribeTopics,
            assignTopicPartitions,
            subscribePattern,
            schedulerSupplier,
            consumerListener,
            pauseAll,
            observationRegistry,
            observationConvention
        );
    }

    @Override
    public long commitIntervalDuringDelay() {
        return this.commitIntervalDuringDelay;
    }

    @Override
    public ReceiverOptions commitIntervalDuringDelay(long interval) {
        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                interval,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
            );
    }

    @Override
    public Supplier schedulerSupplier() {
        return schedulerSupplier;
    }

    @Override
    public ReceiverOptions schedulerSupplier(Supplier schedulerSupplier) {
        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                Objects.requireNonNull(schedulerSupplier),
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public ReceiverOptions commitRetryInterval(Duration commitRetryInterval) {
        return new ImmutableReceiverOptions<>(
            properties,
            assignListeners,
            revokeListeners,
            keyDeserializer,
            valueDeserializer,
            pollTimeout,
            closeTimeout,
            commitInterval,
            commitBatchSize,
            atmostOnceCommitAheadSize,
            maxCommitAttempts,
            commitRetryInterval,
            maxDeferredCommits,
            maxDelayRebalance,
            commitIntervalDuringDelay,
            subscribeTopics,
            assignTopicPartitions,
            subscribePattern,
            schedulerSupplier,
            consumerListener,
            pauseAllAfterRebalance,
            observationRegistry,
            observationConvention
        );
    }

    public ReceiverOptions consumerListener(@Nullable ConsumerListener consumerListener) {
        return new ImmutableReceiverOptions<>(
                properties,
                assignListeners,
                revokeListeners,
                keyDeserializer,
                valueDeserializer,
                pollTimeout,
                closeTimeout,
                commitInterval,
                commitBatchSize,
                atmostOnceCommitAheadSize,
                maxCommitAttempts,
                commitRetryInterval,
                maxDeferredCommits,
                maxDelayRebalance,
                commitIntervalDuringDelay,
                subscribeTopics,
                assignTopicPartitions,
                subscribePattern,
                schedulerSupplier,
                consumerListener,
                pauseAllAfterRebalance,
                observationRegistry,
                observationConvention
        );
    }

    @Override
    public ReceiverOptions withObservation(ObservationRegistry observationRegistry,
                                                 KafkaReceiverObservationConvention observationConvention) {

        return new ImmutableReceiverOptions<>(
            properties,
            assignListeners,
            revokeListeners,
            keyDeserializer,
            valueDeserializer,
            pollTimeout,
            closeTimeout,
            commitInterval,
            commitBatchSize,
            atmostOnceCommitAheadSize,
            maxCommitAttempts,
            commitRetryInterval,
            maxDeferredCommits,
            maxDelayRebalance,
            commitIntervalDuringDelay,
            subscribeTopics,
            assignTopicPartitions,
            subscribePattern,
            schedulerSupplier,
            consumerListener,
            pauseAllAfterRebalance,
            observationRegistry,
            observationConvention
        );
    }


    @Override
    public Duration commitRetryInterval() {
        return commitRetryInterval;
    }

    @Override
    @Nullable
    public ConsumerListener consumerListener() {
        return consumerListener;
    }

    @Override
    public ObservationRegistry observationRegistry() {
        return observationRegistry;
    }

    @Nullable
    @Override
    public KafkaReceiverObservationConvention observationConvention() {
        return observationConvention;
    }

    private long getLongOption(String optionName, long defaultValue) {
        Objects.requireNonNull(optionName);

        Object value = consumerProperty(optionName);
        long optionValue = 0;
        if (value != null) {
            if (value instanceof Long)
                optionValue = (Long) value;
            else if (value instanceof String)
                optionValue = Long.parseLong((String) value);
            else
                throw new ConfigException("Invalid value " + value);
        } else
            optionValue = defaultValue;
        return optionValue;
    }

    @Override
    public int hashCode() {
        return Objects.hash(
            properties,
            assignListeners,
            revokeListeners,
            keyDeserializer,
            valueDeserializer,
            pollTimeout,
            closeTimeout,
            commitInterval,
            commitBatchSize,
            atmostOnceCommitAheadSize,
            maxCommitAttempts,
            commitRetryInterval,
            maxDeferredCommits,
            maxDelayRebalance,
            commitIntervalDuringDelay,
            maxDeferredCommits,
            subscribeTopics,
            assignTopicPartitions,
            subscribePattern,
            consumerListener,
            observationRegistry,
            observationConvention) +
            (this.pauseAllAfterRebalance ? 1 : 0);
    }

    @Override
    public boolean equals(Object object) {
        if (object == this) return true;
        if (object != null && object.getClass().equals(getClass())) {
            @SuppressWarnings("unchecked")
            ImmutableReceiverOptions that = (ImmutableReceiverOptions) object;
            return Objects.equals(properties, that.properties)
                && Objects.equals(assignListeners, that.assignListeners)
                && Objects.equals(revokeListeners, that.revokeListeners)
                && Objects.equals(keyDeserializer, that.keyDeserializer)
                && Objects.equals(valueDeserializer, that.valueDeserializer)
                && Objects.equals(pollTimeout, that.pollTimeout)
                && Objects.equals(closeTimeout, that.closeTimeout)
                && Objects.equals(commitInterval, that.commitInterval)
                && Objects.equals(commitBatchSize, that.commitBatchSize)
                && Objects.equals(atmostOnceCommitAheadSize, that.atmostOnceCommitAheadSize)
                && Objects.equals(maxCommitAttempts, that.maxCommitAttempts)
                && Objects.equals(commitRetryInterval, that.commitRetryInterval)
                && Objects.equals(maxDelayRebalance, that.maxDelayRebalance)
                && Objects.equals(commitIntervalDuringDelay, that.commitIntervalDuringDelay)
                && Objects.equals(maxDeferredCommits, that.maxDeferredCommits)
                && Objects.equals(subscribeTopics, that.subscribeTopics)
                && Objects.equals(assignTopicPartitions, that.assignTopicPartitions)
                && Objects.equals(subscribePattern, that.subscribePattern)
                && Objects.equals(consumerListener, that.consumerListener)
                && Objects.equals(observationRegistry, that.observationRegistry)
                && Objects.equals(observationConvention, that.observationConvention)
                && pauseAllAfterRebalance == that.pauseAllAfterRebalance;
        }
        return false;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy