com.networknt.eventuate.kafka.producer.EventuateKafkaProducer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eventuate-server-kafka Show documentation
Show all versions of eventuate-server-kafka Show documentation
A module that is responsible for Kafka communication
package com.networknt.eventuate.kafka.producer;
import com.networknt.config.Config;
import com.networknt.eventuate.kafka.KafkaConfig;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
/**
* A Kafka Producer that send messages to defined Kafka instance
*
* *@param bootstrapServers Kafka bootstrap Servers string
*/
public class EventuateKafkaProducer {
private Producer producer;
private Properties producerProps;
static final KafkaConfig config = (KafkaConfig) Config.getInstance().getJsonObjectConfig(KafkaConfig.CONFIG_NAME, KafkaConfig.class);
public EventuateKafkaProducer() {
producerProps = new Properties();
producerProps.put("bootstrap.servers", config.getBootstrapServers());
producerProps.put("acks", config.getAcks());
producerProps.put("retries", config.getRetries());
producerProps.put("batch.size", config.getBatchSize());
producerProps.put("linger.ms", config.getLingerms());
producerProps.put("buffer.memory", config.getBufferMemory());
producerProps.put("key.serializer", config.getKeySerializer());
producerProps.put("value.serializer", config.getValueSerializer());
producer = new KafkaProducer<>(producerProps);
}
public void setProducer (Producer producer) {
this.producer = producer;
}
/**
* Update the aggregate
* @param topic Kafka topic
* @param key key the message in the topic
* @param body message body
* @return the CompletableFuture, which process asynchronous
*/
public CompletableFuture> send(String topic, String key, String body) {
CompletableFuture
© 2015 - 2025 Weber Informatics LLC | Privacy Policy