package examples;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.mongo.MongoClient;
import io.vertx.servicediscovery.Record;
import io.vertx.servicediscovery.ServiceDiscovery;
import io.vertx.servicediscovery.ServiceReference;
import io.vertx.servicediscovery.types.MongoDataSource;
public class MongoDataSourceExamples {
public void example1(ServiceDiscovery discovery) {
Record record = MongoDataSource.createRecord(
"some-data-source-service",
new JsonObject().put("connection_string", "some mongo connection"),
new JsonObject().put("some-metadata", "some-value")
);
discovery.publish(record, ar -> {
});
}
public void example2(ServiceDiscovery discovery) {
discovery.getRecord(
new JsonObject().put("name", "some-data-source-service"),
ar -> {
if (ar.succeeded() && ar.result() != null) {
ServiceReference reference = discovery.getReferenceWithConfiguration(
ar.result(),
new JsonObject().put("username", "clement").put("password", "*****"));
MongoClient client = reference.get();
reference.release();
}
});
}
public void example3(ServiceDiscovery discovery) {
MongoDataSource.<JsonObject>getMongoClient(discovery,
new JsonObject().put("name", "some-data-source-service"),
new JsonObject().put("username", "clement").put("password", "*****"),
ar -> {
if (ar.succeeded()) {
MongoClient client = ar.result();
ServiceDiscovery.releaseServiceObject(discovery, client);
}
});
}
}