Java Code Examples for io.vertx.servicediscovery.ServiceDiscovery#getRecord()

The following examples show how to use io.vertx.servicediscovery.ServiceDiscovery#getRecord() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HTTPEndpointExamples.java    From vertx-service-discovery with Apache License 2.0 6 votes vote down vote up
public void example2_webclient(ServiceDiscovery discovery) {
  // Get the record
  discovery.getRecord(new JsonObject().put("name", "some-http-service"), ar -> {
    if (ar.succeeded() && ar.result() != null) {
      // Retrieve the service reference
      ServiceReference reference = discovery.getReference(ar.result());
      // Retrieve the service object
      WebClient client = reference.getAs(WebClient.class);

      // You need to path the complete path
      client.get("/api/persons").send(
        response -> {

          // ...

          // Dont' forget to release the service
          reference.release();

        });
    }
  });
}
 
Example 2
Source File: EventBusService.java    From vertx-service-discovery with Apache License 2.0 6 votes vote down vote up
/**
 * Lookup for a service record and if found, retrieve it and return the service object (used to consume the service).
 * This is a convenient method to avoid explicit lookup and then retrieval of the service. A filter based on the
 * request interface is used.
 *
 * @param discovery     the service discovery instance
 * @param itf           the service interface
 * @param conf          the configuration for message delivery
 * @param resultHandler the result handler
 * @param <T>           the service interface
 * @return {@code null}
 */
@GenIgnore // Java only
static <T> T getProxy(ServiceDiscovery discovery, Class<T> itf, JsonObject conf, Handler<AsyncResult<T>> resultHandler) {
  JsonObject filter = new JsonObject().put("service.interface", itf.getName());
  discovery.getRecord(filter, ar -> {
    if (ar.failed()) {
      resultHandler.handle(Future.failedFuture(ar.cause()));
    } else {
      if (ar.result() == null) {
        resultHandler.handle(Future.failedFuture("Cannot find service matching with " + filter));
      } else {
        ServiceReference service = discovery.getReferenceWithConfiguration(ar.result(), conf);
        resultHandler.handle(Future.succeededFuture(service.get()));
      }
    }
  });
  return null;
}
 
Example 3
Source File: EventBusService.java    From vertx-service-discovery with Apache License 2.0 6 votes vote down vote up
/**
 * Lookup for a service record and if found, retrieve it and return the service object (used to consume the service).
 * This is a convenient method to avoid explicit lookup and then retrieval of the service. A filter based on the
 * request interface is used.
 *
 * @param discovery     the service discovery instance
 * @param itf           the service interface
 * @param resultHandler the result handler
 * @param <T>           the service interface
 * @return {@code null}
 */
@GenIgnore // Java only
static <T> T getProxy(ServiceDiscovery discovery, Class<T> itf, Handler<AsyncResult<T>>
  resultHandler) {
  JsonObject filter = new JsonObject().put("service.interface", itf.getName());
  discovery.getRecord(filter, ar -> {
    if (ar.failed()) {
      resultHandler.handle(Future.failedFuture(ar.cause()));
    } else {
      if (ar.result() == null) {
        resultHandler.handle(Future.failedFuture("Cannot find service matching with " + filter));
      } else {
        ServiceReference service = discovery.getReference(ar.result());
        resultHandler.handle(Future.succeededFuture(service.get()));
      }
    }
  });
  return null;
}
 
Example 4
Source File: GraphQLClient.java    From vertx-graphql-service-discovery with Apache License 2.0 6 votes vote down vote up
/**
 * Lookup a GraphQL service record using the provided filter and if found, retrieve the service proxy of type
 * {@link Queryable} used for querying.
 *
 * @param discovery     the service discovery instance
 * @param filter        the filter to select the schema
 * @param resultHandler the result handler
 */
static void getSchemaProxy(ServiceDiscovery discovery, JsonObject filter,
                           Handler<AsyncResult<Queryable>> resultHandler) {
    Objects.requireNonNull(discovery, "Service discovery cannot be null");
    Objects.requireNonNull(resultHandler, "Schema proxy result handler cannot be null");

    discovery.getRecord(filter, rh -> {
        if (rh.failed()) {
            resultHandler.handle(Future.failedFuture(rh.cause()));
        } else {
            if (rh.result() == null) {
                resultHandler.handle(Future.failedFuture("Failed to find schema proxy using filter " + filter));
            } else {
                Record record = rh.result();
                if (!SERVICE_TYPE.equals(record.getType())) {
                    resultHandler.handle(Future.failedFuture("Record '" + record.getName() +
                            "' is of wrong type '" + record.getType() + "'. Expected: " + SERVICE_TYPE));
                } else {
                    getSchemaProxy(discovery, rh.result(), resultHandler);
                }
            }
        }
    });
}
 
Example 5
Source File: EventBusService.java    From vertx-service-discovery with Apache License 2.0 6 votes vote down vote up
/**
 * Lookup for a service record and if found, retrieve it and return the service object (used to consume the service).
 * This is a convenient method to avoid explicit lookup and then retrieval of the service. This method requires to
 * have the {@code clientClass} set with the expected set of client. This is important for usages not using Java so
 * you can pass the expected type.
 *
 * @param discovery     the service discovery
 * @param filter        the filter
 * @param clientClass   the client class
 * @param conf          the configuration for message delivery
 * @param resultHandler the result handler
 * @param <T>           the type of the client class
 * @return {@code null} - do not use
 */
static <T> T getServiceProxy(ServiceDiscovery discovery,
                             Function<Record, Boolean> filter,
                             Class<T> clientClass,
                             JsonObject conf,
                             Handler<AsyncResult<T>> resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed()) {
      resultHandler.handle(Future.failedFuture(ar.cause()));
    } else {
      if (ar.result() == null) {
        resultHandler.handle(Future.failedFuture("Cannot find service matching with " + filter));
      } else {
        ServiceReference service = discovery.getReferenceWithConfiguration(ar.result(), conf);
        resultHandler.handle(Future.succeededFuture(service.getAs(clientClass)));
      }
    }
  });
  return null;
}
 
Example 6
Source File: JDBCDataSourceExamples.java    From vertx-service-discovery with Apache License 2.0 6 votes vote down vote up
public void example2(ServiceDiscovery discovery) {
  // Get the record
  discovery.getRecord(
      new JsonObject().put("name", "some-data-source-service"),
      ar -> {
        if (ar.succeeded() && ar.result() != null) {
          // Retrieve the service reference
          ServiceReference reference = discovery.getReferenceWithConfiguration(
              ar.result(), // The record
              new JsonObject().put("username", "clement").put("password", "*****")); // Some additional metadata

          // Retrieve the service object
          JDBCClient client = reference.getAs(JDBCClient.class);

          // ...

          // when done
          reference.release();
        }
      });
}
 
Example 7
Source File: MessageSourceExamples.java    From vertx-service-discovery with Apache License 2.0 6 votes vote down vote up
public void example2(ServiceDiscovery discovery) {
  // Get the record
  discovery.getRecord(new JsonObject().put("name", "some-message-source-service"), ar -> {
    if (ar.succeeded() && ar.result() != null) {
      // Retrieve the service reference
      ServiceReference reference = discovery.getReference(ar.result());
      // Retrieve the service object
      MessageConsumer<JsonObject> consumer = reference.getAs(MessageConsumer.class);

      // Attach a message handler on it
      consumer.handler(message -> {
        // message handler
        JsonObject payload = message.body();
      });
    }
  });
}
 
Example 8
Source File: RedisDataSource.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a Redis data source and provides the configured {@link io.vertx.redis.client.Redis}.
 * The async result is marked as failed is there are no matching services, or if the lookup fails.
 *
 * @param discovery             The service discovery instance
 * @param filter                The filter, optional
 * @param consumerConfiguration The additional consumer configuration
 * @param resultHandler         The result handler
 */
static void getRedisClient(ServiceDiscovery discovery, JsonObject filter, JsonObject consumerConfiguration,
                           Handler<AsyncResult<Redis>> resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(
        discovery.<Redis>getReferenceWithConfiguration(ar.result(), consumerConfiguration).get()));
    }
  });
}
 
Example 9
Source File: EventBusServiceExamples.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
public void example2(ServiceDiscovery discovery) {
  // Get the record
  discovery.getRecord(new JsonObject().put("name", "some-eventbus-service"), ar -> {
    if (ar.succeeded() && ar.result() != null) {
      // Retrieve the service reference
      ServiceReference reference = discovery.getReference(ar.result());
      // Retrieve the service object
      MyService service = reference.getAs(MyService.class);

      // Dont' forget to release the service
      reference.release();
    }
  });
}
 
Example 10
Source File: HttpEndpoint.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a HTTP endpoint and provides the configured {@link HttpClient}. The async result
 * is marked as failed is there are no matching services, or if the lookup fails.
 *
 * @param discovery     The service discovery instance
 * @param filter        The filter, optional
 * @param resultHandler The result handler
 */
static void getClient(ServiceDiscovery discovery, JsonObject filter, Handler<AsyncResult<HttpClient>>
  resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(discovery.<HttpClient>getReference(ar.result()).get()));
    }
  });
}
 
Example 11
Source File: EventBusServiceJavaExamples.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
public void example2(ServiceDiscovery discovery) {
  // Get the record
  discovery.getRecord(new JsonObject().put("name", "some-eventbus-service"), ar -> {
    if (ar.succeeded() && ar.result() != null) {
      // Retrieve the service reference
      ServiceReference reference = discovery.getReference(ar.result());
      // Retrieve the service object
      MyService service = reference.getAs(MyService.class);

      // Dont' forget to release the service
      reference.release();
    }
  });
}
 
Example 12
Source File: HttpEndpoint.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a HTTP endpoint and provides the configured {@link HttpClient}. The async result
 * is marked as failed is there are no matching services, or if the lookup fails. This method accepts a
 * configuration for the HTTP client.
 *
 * @param discovery     The service discovery instance
 * @param filter        The filter
 * @param conf          the configuration of the client
 * @param resultHandler The result handler
 */
static void getClient(ServiceDiscovery discovery, Function<Record, Boolean> filter, JsonObject conf,
                      Handler<AsyncResult<HttpClient>> resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(
        discovery.<HttpClient>getReferenceWithConfiguration(ar.result(), conf).get()));
    }
  });
}
 
Example 13
Source File: JDBCDataSource.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a JDBC datasource source and provides the configured {@link io.vertx.ext.jdbc.JDBCClient}. The
 * async result is marked as failed is there are no matching services, or if the lookup fails.
 *
 * @param discovery             The service discovery instance
 * @param filter                The filter, must not be {@code null}
 * @param consumerConfiguration the consumer configuration
 * @param resultHandler         the result handler
 */
static void getJDBCClient(ServiceDiscovery discovery, Function<Record, Boolean> filter, JsonObject consumerConfiguration,
                          Handler<AsyncResult<JDBCClient>> resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(
        discovery.<JDBCClient>getReferenceWithConfiguration(ar.result(), consumerConfiguration).get()));
    }
  });
}
 
Example 14
Source File: MessageSource.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a message source and provides the configured {@link MessageConsumer}. The
 * async result is marked as failed is there are no matching services, or if the lookup fails.
 *
 * @param discovery     The service discovery instance
 * @param filter        The filter, optional
 * @param resultHandler The result handler
 * @param <T>           The class of the message
 */
static <T> void getConsumer(ServiceDiscovery discovery, JsonObject filter,
                            Handler<AsyncResult<MessageConsumer<T>>>
                                resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(discovery.<MessageConsumer<T>>getReference(ar.result()).get()));
    }
  });
}
 
Example 15
Source File: MessageSource.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a message source and provides the configured {@link MessageConsumer}. The
 * async result is marked as failed is there are no matching services, or if the lookup fails.
 *
 * @param discovery     The service discovery instance
 * @param filter        The filter, must not be {@code null}
 * @param resultHandler The result handler
 * @param <T>           The class of the message
 */
static <T> void getConsumer(ServiceDiscovery discovery, Function<Record, Boolean> filter,
                            Handler<AsyncResult<MessageConsumer<T>>>
                              resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(discovery.<MessageConsumer<T>>getReference(ar.result()).get()));
    }
  });
}
 
Example 16
Source File: MongoDataSource.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a Mongo datasource source and provides the configured {@link io.vertx.ext.mongo.MongoClient}. The
 * async result is marked as failed is there are no matching services, or if the lookup fails.
 *
 * @param discovery     The service discovery instance
 * @param filter        The filter, optional
 * @param resultHandler The result handler
 */
static void getMongoClient(ServiceDiscovery discovery, JsonObject filter,
                           Handler<AsyncResult<MongoClient>> resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(discovery.getReference(ar.result()).get()));
    }
  });
}
 
Example 17
Source File: MongoDataSource.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a Mongo datasource source and provides the configured
 * {@link io.vertx.ext.mongo.MongoClient}. The
 * async result is marked as failed is there are no matching services, or if the lookup fails.
 *
 * @param discovery     The service discovery instance
 * @param filter        The filter
 * @param resultHandler The result handler
 */
static void getMongoClient(ServiceDiscovery discovery, Function<Record, Boolean> filter,
                           Handler<AsyncResult<MongoClient>> resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(discovery.getReference(ar.result()).get()));
    }
  });
}
 
Example 18
Source File: MongoDataSource.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a Mongo datasource source and provides the configured {@link io.vertx.ext.mongo.MongoClient}. The
 * async result is marked as failed is there are no matching services, or if the lookup fails.
 *
 * @param discovery             The service discovery instance
 * @param filter                The filter, optional
 * @param consumerConfiguration the consumer configuration
 * @param resultHandler         the result handler
 */
static void getMongoClient(ServiceDiscovery discovery, JsonObject filter, JsonObject consumerConfiguration,
                           Handler<AsyncResult<MongoClient>> resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(
        discovery.getReferenceWithConfiguration(ar.result(), consumerConfiguration).get()));
    }
  });
}
 
Example 19
Source File: HttpEndpoint.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a HTTP endpoint and provides the configured {@link WebClient}. The async result
 * is marked as failed is there are no matching services, or if the lookup fails. This method accepts a
 * configuration for the HTTP client.
 *
 * @param discovery     The service discovery instance
 * @param filter        The filter
 * @param conf          the configuration of the client
 * @param resultHandler The result handler
 */
static void getWebClient(ServiceDiscovery discovery, Function<Record, Boolean> filter, JsonObject conf,
                      Handler<AsyncResult<WebClient>> resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(
        discovery.<HttpClient>getReferenceWithConfiguration(ar.result(), conf).getAs(WebClient.class)));
    }
  });
}
 
Example 20
Source File: HttpEndpoint.java    From vertx-service-discovery with Apache License 2.0 5 votes vote down vote up
/**
 * Convenient method that looks for a HTTP endpoint and provides the configured {@linkWebClient}. The async result
 * is marked as failed is there are no matching services, or if the lookup fails.
 *
 * @param discovery     The service discovery instance
 * @param filter        The filter, optional
 * @param resultHandler The result handler
 */
static void getWebClient(ServiceDiscovery discovery, JsonObject filter, Handler<AsyncResult<WebClient>>
  resultHandler) {
  discovery.getRecord(filter, ar -> {
    if (ar.failed() || ar.result() == null) {
      resultHandler.handle(Future.failedFuture("No matching record"));
    } else {
      resultHandler.handle(Future.succeededFuture(discovery.<HttpClient>getReference(ar.result()).getAs(WebClient.class)));
    }
  });
}