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

net.mguenther.kafka.junit.SendValuesTransactional Maven / Gradle / Ivy

package net.mguenther.kafka.junit;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

@ToString
@RequiredArgsConstructor
public class SendValuesTransactional {

    public static class SendValuesTransactionalBuilder {

        private final Map> valuesPerTopic = new HashMap<>();
        private final Properties producerPros = new Properties();
        private boolean failTransaction = false;

        SendValuesTransactionalBuilder(final String topic, final Collection values) {
            valuesPerTopic.put(topic, values);
        }

        public SendValuesTransactionalBuilder inTransaction(final String topic, final Collection values) {
            final Collection existingValuesPerTopic = valuesPerTopic.getOrDefault(topic, new ArrayList<>());
            existingValuesPerTopic.addAll(values);
            valuesPerTopic.put(topic, existingValuesPerTopic);
            return this;
        }

        public SendValuesTransactionalBuilder failTransaction() {
            return withFailTransaction(true);
        }

        public SendValuesTransactionalBuilder withFailTransaction(final boolean modifier) {
            failTransaction = modifier;
            return this;
        }

        public  SendValuesTransactionalBuilder with(final String propertyName, final T value) {
            producerPros.put(propertyName, value);
            return this;
        }

        public  SendValuesTransactionalBuilder withAll(final Properties transactionalProps) {
            this.producerPros.putAll(transactionalProps);
            return this;
        }

        private  void ifNonExisting(final String propertyName, final T value) {
            if (producerPros.get(propertyName) != null) return;
            producerPros.put(propertyName, value);
        }

        public SendValuesTransactional useDefaults() {
            producerPros.clear();
            failTransaction = false;
            return build();
        }

        public SendValuesTransactional build() {
            ifNonExisting(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
            ifNonExisting(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
            ifNonExisting(ProducerConfig.RETRIES_CONFIG, Integer.MAX_VALUE);
            ifNonExisting(ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION, 1);
            ifNonExisting(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, true);
            ifNonExisting(ProducerConfig.TRANSACTIONAL_ID_CONFIG, UUID.randomUUID().toString());
            ifNonExisting(ProducerConfig.TRANSACTION_TIMEOUT_CONFIG, 60_000);
            ifNonExisting(ProducerConfig.ACKS_CONFIG, "all");
            return new SendValuesTransactional<>(valuesPerTopic, failTransaction, producerPros);
        }
    }

    @Getter
    private final Map> valuesPerTopic;

    private final boolean failTransaction;

    @Getter
    private final Properties producerProps;

    public boolean shouldFailTransaction() {
        return failTransaction;
    }

    public static  SendValuesTransactionalBuilder inTransaction(final String topic, final Collection values) {
        return new SendValuesTransactionalBuilder<>(topic, values);
    }

    @SafeVarargs
    public static  SendValuesTransactionalBuilder inTransaction(final String topic, final V... values) {
        return new SendValuesTransactionalBuilder<>(topic, Arrays.asList(values));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy