package examples;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.json.JsonObject;
import io.vertx.servicediscovery.ServiceDiscovery;
import io.vertx.servicediscovery.Record;
import io.vertx.servicediscovery.ServiceReference;
import io.vertx.servicediscovery.types.MessageSource;
public class MessageSourceExamples {
public void example1(ServiceDiscovery discovery) {
Record record = MessageSource.createRecord(
"some-message-source-service",
"some-address"
);
discovery.publish(record, ar -> {
});
record = MessageSource.createRecord(
"some-other-message-source-service",
"some-address",
"examples.MyData"
);
}
public void example2(ServiceDiscovery discovery) {
discovery.getRecord(new JsonObject().put("name", "some-message-source-service"), ar -> {
if (ar.succeeded() && ar.result() != null) {
ServiceReference reference = discovery.getReference(ar.result());
MessageConsumer<JsonObject> consumer = reference.getAs(MessageConsumer.class);
consumer.handler(message -> {
JsonObject payload = message.body();
});
}
});
}
public void example3(ServiceDiscovery discovery) {
MessageSource.<JsonObject>getConsumer(discovery, new JsonObject().put("name", "some-message-source-service"), ar -> {
if (ar.succeeded()) {
MessageConsumer<JsonObject> consumer = ar.result();
consumer.handler(message -> {
JsonObject payload = message.body();
});
}
});
}
}