io.smallrye.reactive.messaging.kafka.KafkaConnector Maven / Gradle / Ivy
package io.smallrye.reactive.messaging.kafka;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.BeforeDestroyed;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.reactive.messaging.Message;
import org.eclipse.microprofile.reactive.messaging.spi.Connector;
import org.eclipse.microprofile.reactive.messaging.spi.IncomingConnectorFactory;
import org.eclipse.microprofile.reactive.messaging.spi.OutgoingConnectorFactory;
import org.eclipse.microprofile.reactive.streams.operators.PublisherBuilder;
import org.eclipse.microprofile.reactive.streams.operators.SubscriberBuilder;
import io.vertx.reactivex.core.Vertx;
@ApplicationScoped
@Connector(KafkaConnector.CONNECTOR_NAME)
public class KafkaConnector implements IncomingConnectorFactory, OutgoingConnectorFactory {
static final String CONNECTOR_NAME = "smallrye-kafka";
@Inject
private Instance instanceOfVertx;
@Inject
@ConfigProperty(name = "kafka.bootstrap.servers", defaultValue = "localhost:9092")
private String servers;
private List sources = new CopyOnWriteArrayList<>();
private List sinks = new CopyOnWriteArrayList<>();
private boolean internalVertxInstance = false;
private Vertx vertx;
public void terminate(@Observes @BeforeDestroyed(ApplicationScoped.class) Object event) {
sources.forEach(KafkaSource::closeQuietly);
sinks.forEach(KafkaSink::closeQuietly);
if (internalVertxInstance) {
vertx.close();
}
}
@PostConstruct
void init() {
if (instanceOfVertx.isUnsatisfied()) {
internalVertxInstance = true;
this.vertx = Vertx.vertx();
} else {
this.vertx = instanceOfVertx.get();
}
}
@Override
public PublisherBuilder extends Message>> getPublisherBuilder(Config config) {
String s = servers;
KafkaSource