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

io.confluent.parallelconsumer.examples.reactor.ReactorApp Maven / Gradle / Ivy

There is a newer version: 0.5.3.2
Show newest version
package io.confluent.parallelconsumer.examples.reactor;

/*-
 * Copyright (C) 2020-2024 Confluent, Inc.
 */

import io.confluent.parallelconsumer.ParallelConsumerOptions;
import io.confluent.parallelconsumer.reactor.ReactorProcessor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import pl.tlinkowski.unij.api.UniMaps;
import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.Properties;

import static pl.tlinkowski.unij.api.UniLists.of;

@Slf4j
public class ReactorApp {

    static String inputTopic = "input-topic-" + RandomUtils.nextInt();

    Consumer getKafkaConsumer() {
        return new KafkaConsumer<>(new Properties());
    }

    Producer getKafkaProducer() {
        return new KafkaProducer<>(new Properties());
    }

    ReactorProcessor parallelConsumer;


    void run() {
        Consumer kafkaConsumer = getKafkaConsumer();
        Producer kafkaProducer = getKafkaProducer();
        var options = ParallelConsumerOptions.builder()
                .ordering(ParallelConsumerOptions.ProcessingOrder.KEY)
                .consumer(kafkaConsumer)
                .producer(kafkaProducer)
                .build();

        this.parallelConsumer = new ReactorProcessor<>(options);
        parallelConsumer.subscribe(of(inputTopic));

        postSetup();

        // tag::example[]
        parallelConsumer.react(context -> {
            var consumerRecord = context.getSingleRecord().getConsumerRecord();
            log.info("Concurrently constructing and returning RequestInfo from record: {}", consumerRecord);
            Map params = UniMaps.of("recordKey", consumerRecord.key(), "payload", consumerRecord.value());
            return Mono.just("something todo"); // <1>
        });
        // end::example[]
    }

    void close() {
        this.parallelConsumer.closeDrainFirst();
    }

    protected void postSetup() {
        // no-op, for testing
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy