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

org.springframework.kafka.core.reactive.ReactiveKafkaProducerTemplate Maven / Gradle / Ivy

There is a newer version: 3.1.4
Show newest version
/*
 * Copyright 2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.kafka.core.reactive;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.Metric;
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.PartitionInfo;
import org.reactivestreams.Publisher;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.kafka.support.converter.MessagingMessageConverter;
import org.springframework.kafka.support.converter.RecordMessageConverter;
import org.springframework.messaging.Message;
import org.springframework.util.Assert;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.kafka.sender.KafkaSender;
import reactor.kafka.sender.SenderOptions;
import reactor.kafka.sender.SenderRecord;
import reactor.kafka.sender.SenderResult;
import reactor.kafka.sender.TransactionManager;
import reactor.util.function.Tuple2;
import reactor.util.function.Tuples;

/**
 * Reactive kafka producer operations implementation.
 *
 * @param  the key type.
 * @param  the value type.
 *
 * @author Mark Norkin
 *
 * @since 2.3.0
 */
public class ReactiveKafkaProducerTemplate implements AutoCloseable, DisposableBean {

	private final KafkaSender sender;

	private final RecordMessageConverter messageConverter;

	public ReactiveKafkaProducerTemplate(SenderOptions senderOptions) {
		this(senderOptions, new MessagingMessageConverter());
	}

	public ReactiveKafkaProducerTemplate(SenderOptions senderOptions, RecordMessageConverter messageConverter) {
		Assert.notNull(senderOptions, "Sender options can not be null");
		Assert.notNull(messageConverter, "Message converter can not be null");
		this.sender = KafkaSender.create(senderOptions);
		this.messageConverter = messageConverter;
	}

	public  Flux> sendTransactionally(Publisher> records) {
		Flux>> sendTransactionally = this.sender.sendTransactionally(Flux.just(records));
		return sendTransactionally.flatMap(Function.identity());
	}

	public  Mono> sendTransactionally(SenderRecord record) {
		Flux> sendTransactionally = sendTransactionally(Mono.just(record));
		return sendTransactionally.single();
	}

	public Mono> send(String topic, V value) {
		return send(new ProducerRecord<>(topic, value));
	}

	public Mono> send(String topic, K key, V value) {
		return send(new ProducerRecord<>(topic, key, value));
	}

	public Mono> send(String topic, int partition, K key, V value) {
		return send(new ProducerRecord<>(topic, partition, key, value));
	}

	public Mono> send(String topic, int partition, long timestamp, K key, V value) {
		return send(new ProducerRecord<>(topic, partition, timestamp, key, value));
	}

	public Mono> send(String topic, Message message) {
		@SuppressWarnings("unchecked")
		ProducerRecord producerRecord = (ProducerRecord) this.messageConverter.fromMessage(message, topic);
		if (!producerRecord.headers().iterator().hasNext()) { // possibly no Jackson
			byte[] correlationId = message.getHeaders().get(KafkaHeaders.CORRELATION_ID, byte[].class);
			if (correlationId != null) {
				producerRecord.headers().add(KafkaHeaders.CORRELATION_ID, correlationId);
			}
		}
		return send(producerRecord);
	}

	public Mono> send(ProducerRecord record) {
		return send(SenderRecord.create(record, null));
	}

	public  Mono> send(SenderRecord record) {
		return send(Mono.just(record)).single();
	}

	public  Flux> send(Publisher> records) {
		return this.sender.send(records);
	}

	public Mono flush() {
		return doOnProducer(producer -> {
			producer.flush();
			return null;
		});
	}

	public Flux partitionsFromProducerFor(String topic) {
		Mono> partitionsInfo = doOnProducer(producer -> producer.partitionsFor(topic));
		return partitionsInfo.flatMapIterable(Function.identity());
	}

	public Flux> metricsFromProducer() {
		return doOnProducer(Producer::metrics)
				.flatMapIterable(Map::entrySet)
				.map(m -> Tuples.of(m.getKey(), m.getValue()));
	}

	public  Mono doOnProducer(Function, ? extends T> action) {
		return this.sender.doOnProducer(action);
	}

	public TransactionManager transactionManager() {
		return this.sender.transactionManager();
	}

	@Override
	public void destroy() {
		doClose();
	}

	@Override
	public void close() {
		doClose();
	}

	private void doClose() {
		this.sender.close();
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy