KafkaProducer
public class KafkaProducer: KafkaClient
A threadsafe KafkaClient
for producing messages to a topic on a broker.
Usage Example:
do {
let producer = try KafkaProducer()
producer.connect(brokers: "localhost:9092")
producer.send(producerRecord: KafkaProducerRecord(topic: "test", value: "Hello World")) { result in
switch result {
case .success(let message):
print("Message at offset \(message.offset) successfully sent")
case .failure(let error):
print("Error producing: \(error)")
}
}
} catch {
print("Error creating producer: \(error)")
}
-
Create a new
KafkaProducer
for sending messages to Kafka.Declaration
Swift
public init(config: KafkaConfig = KafkaConfig(), pollInterval: TimeInterval = 1) throws
Parameters
config
The
KafkaConfig
that will configure your Kafka producer.pollInterval
The time in seconds that the producer will poll for message callbacks.
-
Send a
KafkaProducerRecord
to the broker. The result of sending the message will be returned in the messageCallback.Declaration
Swift
public func send(producerRecord: KafkaProducerRecord, messageCallback: ((Result<KafkaConsumerRecord, KafkaError>) -> Void)? = nil)
Parameters
producerRecord
The
KafkaProducerRecord
that will be sent to Kafka.messageCallback
The callback that will be called with the result of trying to send a message.