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

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

Go to download

Provides an embedded Kafka cluster consisting of Apache ZooKeeper, Apache Kafka Brokers and Kafka Connect workers in distributed mode along with a rich set of convenient accessors and fault injectors to interact with the embedded Kafka cluster. Supports working against external clusters as well.

There is a newer version: 3.6.0
Show newest version
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.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

@Getter
@ToString
@RequiredArgsConstructor
public class SendValuesTransactional {

    public static class SendValuesTransactionalBuilder {

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

        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 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();
            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);
            return new SendValuesTransactional<>(valuesPerTopic, producerPros);
        }
    }

    private final Map> valuesPerTopic;
    private final Properties producerProps;

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy