kafka.entity.changelog.topic.TopicCommandRepository Maven / Gradle / Ivy
package kafka.entity.changelog.topic;
import java.io.Flushable;
import java.io.IOException;
import kafka.entity.changelog.schema.Topic;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public interface TopicCommandRepository extends Flushable {
static TopicCommandRepository create(
Producer commandProducer,
String commandTopicName) {
return new DefaultTopicCommandRepository(commandProducer, commandTopicName);
}
void put(Topic.Command command);
@Override default void flush() throws IOException {
// nothing to flush
}
class DefaultTopicCommandRepository implements TopicCommandRepository {
static final Logger LOG = LoggerFactory.getLogger(DefaultTopicCommandRepository.class);
final Producer producer;
final String commandTopicName;
DefaultTopicCommandRepository(
Producer producer,
String commandTopicName) {
this.producer = producer;
this.commandTopicName = commandTopicName;
}
public void put(Topic.Command command) {
ProducerRecord record = null;
if (command.hasTopicsFound()) record = new ProducerRecord<>(commandTopicName, null, command);
if (command.hasCreateTopic()) record = new ProducerRecord<>(commandTopicName, null, command);
producer.send(record, (metadata, exception) -> {
if (exception != null) LOG.error("Error sending topics found", exception);
});
}
@Override public void flush() throws IOException {
producer.flush();
}
}
}