
io.vertx.rxjava.servicediscovery.ServiceDiscovery Maven / Gradle / Ivy
/*
* Copyright 2014 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.vertx.rxjava.servicediscovery;
import rx.Observable;
import rx.Single;
import io.vertx.rx.java.RxHelper;
import io.vertx.rx.java.WriteStreamSubscriber;
import io.vertx.rx.java.SingleOnSubscribeAdapter;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.Iterator;
import java.util.function.Function;
import java.util.stream.Collectors;
import io.vertx.core.Handler;
import io.vertx.core.AsyncResult;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.lang.rx.RxGen;
import io.vertx.lang.rx.TypeArg;
import io.vertx.lang.rx.MappingIterator;
/**
* Service Discovery main entry point.
*
* The service discovery is an infrastructure that let you publish and find `services`. A `service` is a discoverable
* functionality. It can be qualified by its type, metadata, and location. So a `service` can be a database, a
* service proxy, a HTTP endpoint. It does not have to be a vert.x entity, but can be anything. Each service is
* described by a {@link io.vertx.servicediscovery.Record}.
*
* The service discovery implements the interactions defined in the service-oriented computing. And to some extend,
* also provides the dynamic service-oriented computing interaction. So, application can react to arrival and
* departure of services.
*
* A service provider can:
*
* * publish a service record
* * un-publish a published record
* * update the status of a published service (down, out of service...)
*
* A service consumer can:
*
* * lookup for services
* * bind to a selected service (it gets a {@link io.vertx.rxjava.servicediscovery.ServiceReference}) and use it
* * release the service once the consumer is done with it
* * listen for arrival, departure and modification of services.
*
* Consumer would 1) lookup for service record matching their need, 2) retrieve the {@link io.vertx.rxjava.servicediscovery.ServiceReference} that give access
* to the service, 3) get a service object to access the service, 4) release the service object once done.
*
* A state above, the central piece of information shared by the providers and consumers are {@link io.vertx.servicediscovery.Record}.
*
* Providers and consumers must create their own {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery} instance. These instances are collaborating
* in background (distributed structure) to keep the set of services in sync.
*
*
* NOTE: This class has been automatically generated from the {@link io.vertx.servicediscovery.ServiceDiscovery original} non RX-ified interface using Vert.x codegen.
*/
@RxGen(io.vertx.servicediscovery.ServiceDiscovery.class)
public class ServiceDiscovery {
@Override
public String toString() {
return delegate.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ServiceDiscovery that = (ServiceDiscovery) o;
return delegate.equals(that.delegate);
}
@Override
public int hashCode() {
return delegate.hashCode();
}
public static final TypeArg __TYPE_ARG = new TypeArg<>( obj -> new ServiceDiscovery((io.vertx.servicediscovery.ServiceDiscovery) obj),
ServiceDiscovery::getDelegate
);
private final io.vertx.servicediscovery.ServiceDiscovery delegate;
public ServiceDiscovery(io.vertx.servicediscovery.ServiceDiscovery delegate) {
this.delegate = delegate;
}
public ServiceDiscovery(Object delegate) {
this.delegate = (io.vertx.servicediscovery.ServiceDiscovery)delegate;
}
public io.vertx.servicediscovery.ServiceDiscovery getDelegate() {
return delegate;
}
private static final TypeArg TYPE_ARG_0 = new TypeArg(o1 -> io.vertx.rxjava.servicediscovery.ServiceReference.newInstance((io.vertx.servicediscovery.ServiceReference)o1), o1 -> o1.getDelegate());
/**
* Creates an instance of {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery}.
* @param vertx the vert.x instance
* @param options the discovery options
* @return the created service discovery instance.
*/
public static io.vertx.rxjava.servicediscovery.ServiceDiscovery create(io.vertx.rxjava.core.Vertx vertx, io.vertx.servicediscovery.ServiceDiscoveryOptions options) {
io.vertx.rxjava.servicediscovery.ServiceDiscovery ret = io.vertx.rxjava.servicediscovery.ServiceDiscovery.newInstance((io.vertx.servicediscovery.ServiceDiscovery)io.vertx.servicediscovery.ServiceDiscovery.create(vertx.getDelegate(), options));
return ret;
}
/**
* Creates a new instance of {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery} using the default configuration.
* @param vertx the vert.x instance
* @return the created instance
*/
public static io.vertx.rxjava.servicediscovery.ServiceDiscovery create(io.vertx.rxjava.core.Vertx vertx) {
io.vertx.rxjava.servicediscovery.ServiceDiscovery ret = io.vertx.rxjava.servicediscovery.ServiceDiscovery.newInstance((io.vertx.servicediscovery.ServiceDiscovery)io.vertx.servicediscovery.ServiceDiscovery.create(vertx.getDelegate()));
return ret;
}
/**
* Creates an instance of {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery}.
* @param vertx the vert.x instance
* @param options the discovery options
* @param completionHandler completion handler called when the service discovery has been initialized. This includes the initialization of the service importer registered from the SPI.
* @return the created instance, should not be used to retrieve services before the invocation of the completion handler.
*/
@Deprecated()
public static io.vertx.rxjava.servicediscovery.ServiceDiscovery create(io.vertx.rxjava.core.Vertx vertx, io.vertx.servicediscovery.ServiceDiscoveryOptions options, Handler completionHandler) {
io.vertx.rxjava.servicediscovery.ServiceDiscovery ret = io.vertx.rxjava.servicediscovery.ServiceDiscovery.newInstance((io.vertx.servicediscovery.ServiceDiscovery)io.vertx.servicediscovery.ServiceDiscovery.create(vertx.getDelegate(), options, new Handler() {
public void handle(io.vertx.servicediscovery.ServiceDiscovery event) {
completionHandler.handle(io.vertx.rxjava.servicediscovery.ServiceDiscovery.newInstance((io.vertx.servicediscovery.ServiceDiscovery)event));
}
}));
return ret;
}
/**
* Creates a new instance of {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery} using the default configuration.
* @param vertx the vert.x instance
* @param completionHandler completion handler called when the service discovery has been initialized. This includes the initialization of the service importer registered from the SPI.
* @return the created instance, should not be used to retrieve services before the invocation of the completion handler.
*/
@Deprecated()
public static io.vertx.rxjava.servicediscovery.ServiceDiscovery create(io.vertx.rxjava.core.Vertx vertx, Handler completionHandler) {
io.vertx.rxjava.servicediscovery.ServiceDiscovery ret = io.vertx.rxjava.servicediscovery.ServiceDiscovery.newInstance((io.vertx.servicediscovery.ServiceDiscovery)io.vertx.servicediscovery.ServiceDiscovery.create(vertx.getDelegate(), new Handler() {
public void handle(io.vertx.servicediscovery.ServiceDiscovery event) {
completionHandler.handle(io.vertx.rxjava.servicediscovery.ServiceDiscovery.newInstance((io.vertx.servicediscovery.ServiceDiscovery)event));
}
}));
return ret;
}
/**
* Gets a service reference from the given record.
* @param record the chosen record
* @return the service reference, that allows retrieving the service object. Once called the service reference is cached, and need to be released.
*/
public io.vertx.rxjava.servicediscovery.ServiceReference getReference(io.vertx.servicediscovery.Record record) {
io.vertx.rxjava.servicediscovery.ServiceReference ret = io.vertx.rxjava.servicediscovery.ServiceReference.newInstance((io.vertx.servicediscovery.ServiceReference)delegate.getReference(record));
return ret;
}
/**
* Gets a service reference from the given record, the reference is configured with the given json object.
* @param record the chosen record
* @param configuration the configuration
* @return the service reference, that allows retrieving the service object. Once called the service reference is cached, and need to be released.
*/
public io.vertx.rxjava.servicediscovery.ServiceReference getReferenceWithConfiguration(io.vertx.servicediscovery.Record record, JsonObject configuration) {
io.vertx.rxjava.servicediscovery.ServiceReference ret = io.vertx.rxjava.servicediscovery.ServiceReference.newInstance((io.vertx.servicediscovery.ServiceReference)delegate.getReferenceWithConfiguration(record, configuration));
return ret;
}
/**
* Releases the service reference.
* @param reference the reference to release, must not be null
* @return whether or not the reference has been released.
*/
public boolean release(io.vertx.rxjava.servicediscovery.ServiceReference reference) {
boolean ret = delegate.release(reference.getDelegate());
return ret;
}
/**
* Registers a discovery service importer. Importers let you integrate other discovery technologies in this service
* discovery.
* @param importer the service importer
* @param configuration the optional configuration
* @return the current {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery}
*/
public io.vertx.rxjava.servicediscovery.ServiceDiscovery registerServiceImporter(io.vertx.rxjava.servicediscovery.spi.ServiceImporter importer, JsonObject configuration) {
delegate.registerServiceImporter(importer.getDelegate(), configuration);
return this;
}
/**
* Registers a discovery service importer. Importers let you integrate other discovery technologies in this service
* discovery.
* @param importer the service importer
* @param configuration the optional configuration
* @param completionHandler handler call when the importer has finished its initialization and initial imports
* @return the current {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery}
*/
public io.vertx.rxjava.servicediscovery.ServiceDiscovery registerServiceImporter(io.vertx.rxjava.servicediscovery.spi.ServiceImporter importer, JsonObject configuration, Handler> completionHandler) {
delegate.registerServiceImporter(importer.getDelegate(), configuration, completionHandler);
return this;
}
/**
* Registers a discovery service importer. Importers let you integrate other discovery technologies in this service
* discovery.
* @param importer the service importer
* @param configuration the optional configuration
* @return the current {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery}
*/
public Single rxRegisterServiceImporter(io.vertx.rxjava.servicediscovery.spi.ServiceImporter importer, JsonObject configuration) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
registerServiceImporter(importer, configuration, fut);
}));
}
/**
* Registers a discovery bridge. Exporters let you integrate other discovery technologies in this service
* discovery.
* @param exporter the service exporter
* @param configuration the optional configuration
* @return the current {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery}
*/
public io.vertx.rxjava.servicediscovery.ServiceDiscovery registerServiceExporter(io.vertx.rxjava.servicediscovery.spi.ServiceExporter exporter, JsonObject configuration) {
delegate.registerServiceExporter(exporter.getDelegate(), configuration);
return this;
}
/**
* Registers a discovery bridge. Exporters let you integrate other discovery technologies in this service
* discovery.
* @param exporter the service exporter
* @param configuration the optional configuration
* @param completionHandler handler notified when the exporter has been correctly initialized.
* @return the current {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery}
*/
public io.vertx.rxjava.servicediscovery.ServiceDiscovery registerServiceExporter(io.vertx.rxjava.servicediscovery.spi.ServiceExporter exporter, JsonObject configuration, Handler> completionHandler) {
delegate.registerServiceExporter(exporter.getDelegate(), configuration, completionHandler);
return this;
}
/**
* Registers a discovery bridge. Exporters let you integrate other discovery technologies in this service
* discovery.
* @param exporter the service exporter
* @param configuration the optional configuration
* @return the current {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery}
*/
public Single rxRegisterServiceExporter(io.vertx.rxjava.servicediscovery.spi.ServiceExporter exporter, JsonObject configuration) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
registerServiceExporter(exporter, configuration, fut);
}));
}
/**
* Closes the service discovery
*/
public void close() {
delegate.close();
}
/**
* Publishes a record.
* @param record the record
* @param resultHandler handler called when the operation has completed (successfully or not). In case of success, the passed record has a registration id required to modify and un-register the service.
*/
public void publish(io.vertx.servicediscovery.Record record, Handler> resultHandler) {
delegate.publish(record, resultHandler);
}
/**
* Publishes a record.
* @param record the record
* @return
*/
public Single rxPublish(io.vertx.servicediscovery.Record record) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
publish(record, fut);
}));
}
/**
* Un-publishes a record.
* @param id the registration id
* @param resultHandler handler called when the operation has completed (successfully or not).
*/
public void unpublish(String id, Handler> resultHandler) {
delegate.unpublish(id, resultHandler);
}
/**
* Un-publishes a record.
* @param id the registration id
* @return
*/
public Single rxUnpublish(String id) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
unpublish(id, fut);
}));
}
/**
* Lookups for a single record.
*
* Filters are expressed using a Json object. Each entry of the given filter will be checked against the record.
* All entry must match exactly the record. The entry can use the special "*" value to denotes a requirement on the
* key, but not on the value.
*
* Let's take some example:
*
* { "name" = "a" } => matches records with name set fo "a"
* { "color" = "*" } => matches records with "color" set
* { "color" = "red" } => only matches records with "color" set to "red"
* { "color" = "red", "name" = "a"} => only matches records with name set to "a", and color set to "red"
*
*
* If the filter is not set (null
or empty), it accepts all records.
*
* This method returns the first matching record.
* @param filter the filter.
* @param resultHandler handler called when the lookup has been completed. When there are no matching record, the operation succeeds, but the async result has no result (null
).
*/
public void getRecord(JsonObject filter, Handler> resultHandler) {
delegate.getRecord(filter, resultHandler);
}
/**
* Lookups for a single record.
*
* Filters are expressed using a Json object. Each entry of the given filter will be checked against the record.
* All entry must match exactly the record. The entry can use the special "*" value to denotes a requirement on the
* key, but not on the value.
*
* Let's take some example:
*
* { "name" = "a" } => matches records with name set fo "a"
* { "color" = "*" } => matches records with "color" set
* { "color" = "red" } => only matches records with "color" set to "red"
* { "color" = "red", "name" = "a"} => only matches records with name set to "a", and color set to "red"
*
*
* If the filter is not set (null
or empty), it accepts all records.
*
* This method returns the first matching record.
* @param filter the filter.
* @return
*/
public Single rxGetRecord(JsonObject filter) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
getRecord(filter, fut);
}));
}
/**
* Lookups for a single record.
*
* The filter is a taking a {@link io.vertx.servicediscovery.Record} as argument and returning a boolean. You should see it
* as an accept
method of a filter. This method return a record passing the filter.
*
* This method only looks for records with a UP
status.
* @param filter the filter, must not be null
. To return all records, use a function accepting all records
* @param resultHandler the result handler called when the lookup has been completed. When there are no matching record, the operation succeed, but the async result has no result.
*/
public void getRecord(Function filter, Handler> resultHandler) {
delegate.getRecord(filter, resultHandler);
}
/**
* Lookups for a single record.
*
* The filter is a taking a {@link io.vertx.servicediscovery.Record} as argument and returning a boolean. You should see it
* as an accept
method of a filter. This method return a record passing the filter.
*
* This method only looks for records with a UP
status.
* @param filter the filter, must not be null
. To return all records, use a function accepting all records
* @return
*/
public Single rxGetRecord(Function filter) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
getRecord(filter, fut);
}));
}
/**
* Lookups for a single record.
*
* The filter is a taking a {@link io.vertx.servicediscovery.Record} as argument and returning a boolean. You should see it
* as an accept
method of a filter. This method return a record passing the filter.
*
* Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}, this method may accept records with a OUT OF SERVICE
* status, if the includeOutOfService
parameter is set to true
.
* @param filter the filter, must not be null
. To return all records, use a function accepting all records
* @param includeOutOfService whether or not the filter accepts OUT OF SERVICE
records
* @param resultHandler the result handler called when the lookup has been completed. When there are no matching record, the operation succeed, but the async result has no result.
*/
public void getRecord(Function filter, boolean includeOutOfService, Handler> resultHandler) {
delegate.getRecord(filter, includeOutOfService, resultHandler);
}
/**
* Lookups for a single record.
*
* The filter is a taking a {@link io.vertx.servicediscovery.Record} as argument and returning a boolean. You should see it
* as an accept
method of a filter. This method return a record passing the filter.
*
* Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}, this method may accept records with a OUT OF SERVICE
* status, if the includeOutOfService
parameter is set to true
.
* @param filter the filter, must not be null
. To return all records, use a function accepting all records
* @param includeOutOfService whether or not the filter accepts OUT OF SERVICE
records
* @return
*/
public Single rxGetRecord(Function filter, boolean includeOutOfService) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
getRecord(filter, includeOutOfService, fut);
}));
}
/**
* Lookups for a set of records. Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}, this method returns all matching
* records.
* @param filter the filter - see {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}
* @param resultHandler handler called when the lookup has been completed. When there are no matching record, the operation succeed, but the async result has an empty list as result.
*/
public void getRecords(JsonObject filter, Handler>> resultHandler) {
delegate.getRecords(filter, resultHandler);
}
/**
* Lookups for a set of records. Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}, this method returns all matching
* records.
* @param filter the filter - see {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}
* @return
*/
public Single> rxGetRecords(JsonObject filter) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
getRecords(filter, fut);
}));
}
/**
* Lookups for a set of records. Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}, this method returns all matching
* records.
*
* The filter is a taking a {@link io.vertx.servicediscovery.Record} as argument and returning a boolean. You should see it
* as an accept
method of a filter. This method return a record passing the filter.
*
* This method only looks for records with a UP
status.
* @param filter the filter, must not be null
. To return all records, use a function accepting all records
* @param resultHandler handler called when the lookup has been completed. When there are no matching record, the operation succeed, but the async result has an empty list as result.
*/
public void getRecords(Function filter, Handler>> resultHandler) {
delegate.getRecords(filter, resultHandler);
}
/**
* Lookups for a set of records. Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}, this method returns all matching
* records.
*
* The filter is a taking a {@link io.vertx.servicediscovery.Record} as argument and returning a boolean. You should see it
* as an accept
method of a filter. This method return a record passing the filter.
*
* This method only looks for records with a UP
status.
* @param filter the filter, must not be null
. To return all records, use a function accepting all records
* @return
*/
public Single> rxGetRecords(Function filter) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
getRecords(filter, fut);
}));
}
/**
* Lookups for a set of records. Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}, this method returns all matching
* records.
*
* The filter is a taking a {@link io.vertx.servicediscovery.Record} as argument and returning a boolean. You should see it
* as an accept
method of a filter. This method return a record passing the filter.
*
* Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecords}, this method may accept records with a OUT OF SERVICE
* status, if the includeOutOfService
parameter is set to true
.
* @param filter the filter, must not be null
. To return all records, use a function accepting all records
* @param includeOutOfService whether or not the filter accepts OUT OF SERVICE
records
* @param resultHandler handler called when the lookup has been completed. When there are no matching record, the operation succeed, but the async result has an empty list as result.
*/
public void getRecords(Function filter, boolean includeOutOfService, Handler>> resultHandler) {
delegate.getRecords(filter, includeOutOfService, resultHandler);
}
/**
* Lookups for a set of records. Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}, this method returns all matching
* records.
*
* The filter is a taking a {@link io.vertx.servicediscovery.Record} as argument and returning a boolean. You should see it
* as an accept
method of a filter. This method return a record passing the filter.
*
* Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecords}, this method may accept records with a OUT OF SERVICE
* status, if the includeOutOfService
parameter is set to true
.
* @param filter the filter, must not be null
. To return all records, use a function accepting all records
* @param includeOutOfService whether or not the filter accepts OUT OF SERVICE
records
* @return
*/
public Single> rxGetRecords(Function filter, boolean includeOutOfService) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
getRecords(filter, includeOutOfService, fut);
}));
}
/**
* Updates the given record. The record must has been published, and has it's registration id set.
* @param record the updated record
* @param resultHandler handler called when the lookup has been completed.
*/
public void update(io.vertx.servicediscovery.Record record, Handler> resultHandler) {
delegate.update(record, resultHandler);
}
/**
* Updates the given record. The record must has been published, and has it's registration id set.
* @param record the updated record
* @return
*/
public Single rxUpdate(io.vertx.servicediscovery.Record record) {
return Single.create(new io.vertx.rx.java.SingleOnSubscribeAdapter<>(fut -> {
update(record, fut);
}));
}
/**
* @return the set of service references retrieved by this service discovery.
*/
public Set bindings() {
Set ret = delegate.bindings().stream().map(elt -> io.vertx.rxjava.servicediscovery.ServiceReference.newInstance((io.vertx.servicediscovery.ServiceReference)elt)).collect(java.util.stream.Collectors.toSet());
return ret;
}
/**
* @return the discovery options. Modifying the returned object would not update the discovery service configuration. This object should be considered as read-only.
*/
public io.vertx.servicediscovery.ServiceDiscoveryOptions options() {
io.vertx.servicediscovery.ServiceDiscoveryOptions ret = delegate.options();
return ret;
}
/**
* Release the service object retrieved using get
methods from the service type interface.
* It searches for the reference associated with the given object and release it.
* @param discovery the service discovery
* @param svcObject the service object
*/
public static void releaseServiceObject(io.vertx.rxjava.servicediscovery.ServiceDiscovery discovery, java.lang.Object svcObject) {
io.vertx.servicediscovery.ServiceDiscovery.releaseServiceObject(discovery.getDelegate(), svcObject);
}
public static ServiceDiscovery newInstance(io.vertx.servicediscovery.ServiceDiscovery arg) {
return arg != null ? new ServiceDiscovery(arg) : null;
}
}