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

com.noleme.flow.connect.kafka.loader.KafkaLoader Maven / Gradle / Ivy

The newest version!
package com.noleme.flow.connect.kafka.loader;

import com.noleme.flow.actor.loader.Loader;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.Function;

/**
 * @author Pierre LECERF ([email protected])
 * Created on 22/09/2021
 */
public class KafkaLoader implements Loader
{
    private static final Logger logger = LoggerFactory.getLogger(KafkaLoader.class);

    private final String topic;
    private final Producer producer;
    private final Function keyMapper;
    private final boolean autoflush;

    public KafkaLoader(Producer producer, Function keyMapper, String topic)
    {
        this(producer, keyMapper, true, topic);
    }

    public KafkaLoader(Producer producer, Function keyMapper, boolean autoflush, String topic)
    {
        this.topic = topic;
        this.producer = producer;
        this.keyMapper = keyMapper;
        this.autoflush = autoflush;
    }

    @Override
    public void load(V input)
    {
        logger.info("Loading message into kafka topic: {} ({})", this.topic, (this.autoflush ? "autoflush" : "no-flush"));

        K key = this.keyMapper.apply(input);

        ProducerRecord record = new ProducerRecord<>(this.topic, key, input);
        this.producer.send(record);

        if (this.autoflush)
            this.producer.flush();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy