com.github.lontime.extkafka.provider.Utf8Provider Maven / Gradle / Ivy
package com.github.lontime.extkafka.provider;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Future;
import com.github.lontime.base.commonj.constants.Consts;
import com.github.lontime.base.serial.MsgpackSerial;
import com.twitter.serial.serializer.Serializer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
/**
* @since 1.0
* @author lontime
*/
public interface Utf8Provider extends Provider {
/**
* send.
* @param name name
* @param record record
* @return RecordMetadata
*/
default Future sendUtf8Bytes(String name, ProducerRecord record) {
return sendBytes(name, new ProducerRecord<>(record.topic(), record.partition(),
record.key().getBytes(StandardCharsets.UTF_8), record.value()));
}
/**
* send.
* @param record record
* @return RecordMetadata
*/
default Future sendUtf8Bytes(ProducerRecord record) {
return sendUtf8Bytes(Consts.DEFAULT_OBJECT_NAME, record);
}
/**
* send.
* @param name name
* @param key key
* @param value value
* @return RecordMetadata
*/
default Future sendUtf8Bytes(String name, String key, byte[] value) {
return sendBytes(name, key.getBytes(StandardCharsets.UTF_8), value);
}
/**
* send.
* @param key key
* @param value value
* @return RecordMetadata
*/
default Future sendUtf8Bytes(String key, byte[] value) {
return sendUtf8Bytes(Consts.DEFAULT_OBJECT_NAME, key, value);
}
/**
* send.
* @param name name
* @param key key
* @param value value
* @param valueSerializer valueSerializer
* @param V
* @return RecordMetadata
*/
default Future send(String name, String key, V value, Serializer valueSerializer) {
final byte[] bytes = MsgpackSerial.INSTANCE.toByteArrayUnchecked(value, valueSerializer);
return sendUtf8Bytes(name, key, bytes);
}
/**
* send.
* @param key key
* @param value value
* @param valueSerializer valueSerializer
* @param V
* @return RecordMetadata
*/
default Future send(String key, V value, Serializer valueSerializer) {
return send(Consts.DEFAULT_OBJECT_NAME, key, value, valueSerializer);
}
/**
* send.
* @param name name
* @param record record
* @param V
* @param valueSerializer valueSerializer
* @return RecordMetadata
*/
default Future send(String name, ProducerRecord record, Serializer valueSerializer) {
final byte[] val = MsgpackSerial.INSTANCE.toByteArrayUnchecked(record.value(), valueSerializer);
return sendUtf8Bytes(name, new ProducerRecord<>(record.topic(), record.partition(), record.key(), val));
}
/**
* send.
* @param record record
* @param V
* @param valueSerializer valueSerializer
* @return RecordMetadata
*/
default Future send(ProducerRecord record, Serializer valueSerializer) {
return send(Consts.DEFAULT_OBJECT_NAME, record, valueSerializer);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy