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

com.softwaremill.react.kafka.PropertiesBuilder Maven / Gradle / Ivy

package com.softwaremill.react.kafka;

import kafka.serializer.Decoder;
import kafka.serializer.DefaultDecoder;
import kafka.serializer.Encoder;
import scala.collection.JavaConverters;
import scala.collection.immutable.HashMap;

import java.util.Map;

/**
 * Builder class wrapping Consumer & Producer properties creation in Java API.
 */
public class PropertiesBuilder {

    /**
     * Base properties required by consumers and producers
     */
    private static class BaseProperties {

        private String brokerList;
        private String zooKeeperHost;
        private String topic;

        public BaseProperties(String brokerList, String zooKeeperHost, String topic) {
            this.brokerList = brokerList;
            this.zooKeeperHost = zooKeeperHost;
            this.topic = topic;
        }

        /**
         * Determines if a broker list and zookeeper host are both not null
         *
         * @return boolean if both are not null
         */
        private boolean hasConnectionPropertiesSet() {
            return this.brokerList != null && this.zooKeeperHost != null;
        }

        public String getBrokerList() {
            return brokerList;
        }

        public String getZooKeeperHost() {
            return zooKeeperHost;
        }

        public String getTopic() {
            return topic;
        }
    }


    /**
     * The Consumer Builder
     */
    public static class Consumer extends BaseProperties {

        private Decoder decoder;
        private Decoder keyDecoder = new DefaultDecoder(null);
        private String groupId;
        private scala.collection.immutable.Map consumerParams = new HashMap<>();

        public Consumer(String brokerList, String zooKeeperHost, String topic, String groupId, Decoder decoder) {
            super(brokerList, zooKeeperHost, topic);
            this.decoder = decoder;
            this.groupId = groupId;
        }

        public Consumer withParams(Map params) {
            this.consumerParams = new HashMap().$plus$plus(JavaConverters.mapAsScalaMapConverter(params).asScala());
            return this;
        }

        /**
         * Create a ConsumerProperties object
         *
         * @param  type of key used for partitions
         * @param  type of message
         * @return a fully constructed ConsumerProperties
         */
        public  ConsumerProperties build() {
            if (super.hasConnectionPropertiesSet()) {
                return ConsumerProperties.apply(getBrokerList(), getZooKeeperHost(), getTopic(), groupId, decoder, keyDecoder);
            }
            return new ConsumerProperties(consumerParams, getTopic(), groupId, decoder, keyDecoder);
        }

    }

    /**
     * The Producer Builder
     */
    public static class Producer extends BaseProperties {

        private String clientId;
        private Encoder encoder;
        private scala.collection.immutable.Map producerParams = new HashMap<>();

        public Producer(String brokerList, String zooKeeperHost, String topic, String clientId, Encoder encoder) {
            super(brokerList, zooKeeperHost, topic);
            this.clientId = clientId;
            this.encoder = encoder;
        }

        public Producer withParams(Map params) {
            this.producerParams = new HashMap().$plus$plus(JavaConverters.mapAsScalaMapConverter(params).asScala());
            return this;
        }

        /**
         * Create a ProducerProperties object
         *
         * @param 

the type of Producer to construct * @return a fully constructed ProducerProperties */ public

ProducerProperties

build() { if (super.hasConnectionPropertiesSet()) { return ProducerProperties.

apply(getBrokerList(), getTopic(), clientId, encoder); } return new ProducerProperties(producerParams, getTopic(), clientId, encoder, null); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy