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

com.github.lontime.extkafka.provider.Provider Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package com.github.lontime.extkafka.provider;

import java.nio.charset.StandardCharsets;
import java.util.concurrent.Future;

import com.github.lontime.base.commonj.components.ComponentLifecycle;
import com.github.lontime.base.commonj.constants.Consts;
import com.twitter.serial.serializer.CoreSerializers;
import com.twitter.serial.serializer.Serializer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

/**
 * Provider
 * @author lontime
 */
public interface Provider extends ComponentLifecycle {

    /**
     * send.
     * @param name name
     * @param record record
     * @param  K
     * @param  V
     * @param keySerializer keySerializer
     * @param valueSerializer valueSerializer
     * @return RecordMetadata
     */
     Future send(String name, ProducerRecord record, Serializer keySerializer, Serializer valueSerializer);

    /**
     * send.
     * @param name name
     * @param key key
     * @param value value
     * @param  K
     * @param  V
     * @param keySerializer keySerializer
     * @param valueSerializer valueSerializer
     * @return RecordMetadata
     */
     Future send(String name, K key, V value, Serializer keySerializer, Serializer valueSerializer);

    /**
     * send.
     * @param record record
     * @param  K
     * @param  V
     * @param keySerializer keySerializer
     * @param valueSerializer valueSerializer
     * @return RecordMetadata
     */
    default  Future send(ProducerRecord record, Serializer keySerializer, Serializer valueSerializer) {
        return send(Consts.DEFAULT_OBJECT_NAME, record, keySerializer, valueSerializer);
    }


    /**
     * send.
     * @param key key
     * @param value value
     * @param  K
     * @param  V
     * @param keySerializer keySerializer
     * @param valueSerializer valueSerializer
     * @return RecordMetadata
     */
    default  Future send(K key, V value, Serializer keySerializer, Serializer valueSerializer) {
        return send(Consts.DEFAULT_OBJECT_NAME, key, value, keySerializer, valueSerializer);
    }

    /**
     * send.
     * @param name name
     * @param record record
     * @return RecordMetadata
     */
    Future sendBytes(String name, ProducerRecord record);

    /**
     * send.
     * @param record record
     * @return RecordMetadata
     */
    default Future sendBytes(ProducerRecord record) {
        return sendBytes(Consts.DEFAULT_OBJECT_NAME, record);
    }

    /**
     * send.
     * @param name name
     * @param key key
     * @param value value
     * @return RecordMetadata
     */
    Future sendBytes(String name, byte[] key, byte[] value);

    /**
     * send.
     * @param key key
     * @param value value
     * @return RecordMetadata
     */
    default Future sendBytes(byte[] key, byte[] value) {
        return sendBytes(Consts.DEFAULT_OBJECT_NAME, key, value);
    }

    /**
     * send.
     * @param name name
     * @param record record
     * @return RecordMetadata
     */
    default Future sendStr(String name, ProducerRecord record) {
        return send(name, record, CoreSerializers.STRING, CoreSerializers.STRING);
    }

    /**
     * send.
     * @param record record
     * @return RecordMetadata
     */
    default Future sendStr(ProducerRecord record) {
        return sendStr(Consts.DEFAULT_OBJECT_NAME, record);
    }

    /**
     * send.
     * @param name name
     * @param key key
     * @param value value
     * @return RecordMetadata
     */
    default Future sendStr(String name, String key, String value) {
        return send(name, key, value, CoreSerializers.STRING, CoreSerializers.STRING);
    }

    /**
     * send.
     * @param key key
     * @param value value
     * @return RecordMetadata
     */
    default Future sendStr(String key, String value) {
        return sendStr(Consts.DEFAULT_OBJECT_NAME, key, value);
    }


    /**
     * send.
     * @param name name
     * @param record record
     * @return RecordMetadata
     */
    default Future sendUtf8(String name, ProducerRecord record) {
        final ProducerRecord recordNew =
                new ProducerRecord<>(record.topic(), record.partition(),
                        record.key().getBytes(StandardCharsets.UTF_8),
                        record.value().getBytes(StandardCharsets.UTF_8));
        return sendBytes(name, recordNew);
    }

    /**
     * send.
     * @param record record
     * @return RecordMetadata
     */
    default Future sendUtf8(ProducerRecord record) {
        return sendUtf8(Consts.DEFAULT_OBJECT_NAME, record);
    }

    /**
     * send.
     * @param name name
     * @param key key
     * @param value value
     * @return RecordMetadata
     */
    default Future sendUtf8(String name, String key, String value) {
        return sendBytes(name, key.getBytes(StandardCharsets.UTF_8), value.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * send.
     * @param key key
     * @param value value
     * @return RecordMetadata
     */
    default Future sendUtf8(String key, String value) {
        return sendUtf8(Consts.DEFAULT_OBJECT_NAME, key, value);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy