/** * Gets the service object if already retrieved. It won't try to acquire the service object if not retrieved yet. * @return the object, <code>null</code> if not yet retrieved */ public <T> T cached() { T ret = (T) delegate.cached(); return ret; }
/** * @return the service record. */ public Record record() { if (cached_0 != null) { return cached_0; } Record ret = delegate.record(); cached_0 = ret; return ret; }
/** * Gets the object to access the service. It can be a proxy, a client or whatever object. The type depends on the * service type and the server itself. This method returns the Java version and primary facet of the object, use * {@link io.vertx.rxjava.servicediscovery.ServiceReference#getAs} to retrieve the polyglot instance of the object or another facet.. * @return the object to access the service */ public <T> T get() { T ret = (T) delegate.get(); return ret; }
/** * Get the GraphQL service proxy that is associated with the provided service record. * * @param discovery the service discovery instance * @param record the service record of a published GraphQL service * @param resultHandler the result handler */ static void getSchemaProxy(ServiceDiscovery discovery, Record record, Handler<AsyncResult<Queryable>> resultHandler) { Objects.requireNonNull(discovery, "Service discovery cannot be null"); Objects.requireNonNull(record, "Record cannot be null"); Objects.requireNonNull(resultHandler, "Schema proxy result handler cannot be null"); if (!SERVICE_TYPE.equals(record.getType())) { resultHandler.handle(Future.failedFuture("Record '" + record.getName() + "' is of wrong type '" + record.getType() + "'. Expected: " + SERVICE_TYPE)); } else if (!Status.UP.equals(record.getStatus())) { resultHandler.handle(Future.failedFuture("Record status indicates service '" + record.getName() + "' is: " + record.getStatus() + ". Expected: " + Status.UP)); } else if (record.getRegistration() == null) { resultHandler.handle(Future.failedFuture("Record '" + record.getName() + "' has no service discovery registration")); } else { ServiceReference reference = discovery.getReference(record); Queryable queryable = reference.cached() == null ? reference.get() : reference.cached(); reference.release(); resultHandler.handle(Future.succeededFuture(queryable)); } }
message.reply("FAIL - reference is null"); } else { HttpClient client = reference.get(); result.put("client", client.toString()); result.put("direct", reference.get().toString()); reference.release(); result.put("bindings", getBindings(discovery)); message.reply(result); message.reply("FAIL - reference is null"); } else { WebClient client = reference.getAs(WebClient.class); result.put("client", client.toString()); result.put("direct", reference.get().toString()); reference.release(); result.put("bindings", getBindings(discovery)); message.reply(result); message.reply("FAIL - reference is null"); } else { HelloService client = reference.get(); result.put("client", client.toString()); reference.release(); result.put("bindings", getBindings(discovery)); message.reply(result); message.reply("FAIL - reference is null"); } else { JDBCClient client = reference.get();
@Test public void testPublicationAndConsumption(TestContext context) { Async async = context.async(); // Publish the service Record record = HttpEndpoint.createRecord("hello-service", "localhost", 8080, "/foo"); discovery.publish(record, rec -> { Record published = rec.result(); discovery.getRecord(new JsonObject().put("name", "hello-service"), found -> { context.assertTrue(found.succeeded()); context.assertTrue(found.result() != null); Record match = found.result(); ServiceReference reference = discovery.getReference(match); context.assertEquals(reference.record().getLocation().getString("endpoint"), "http://localhost:8080/foo"); context.assertFalse(reference.record().getLocation().getBoolean("ssl")); HttpClient client = reference.get(); HttpClient client2 = reference.get(); context.assertTrue(client == client2); client.getNow("/foo", response -> { context.assertEquals(response.statusCode(), 200); response.bodyHandler(body -> { context.assertEquals(body.toString(), "hello"); reference.release(); discovery.unpublish(published.getRegistration(), v -> { async.complete(); }); }); }); }); }); }
Record match = found.result(); ServiceReference reference = discovery.getReference(match); context.assertEquals(reference.record().getLocation().getString("endpoint"), "http://localhost:8080/foo"); context.assertFalse(reference.record().getLocation().getBoolean("ssl")); WebClient client = reference.getAs(WebClient.class); WebClient client2 = reference.cachedAs(WebClient.class); context.assertTrue(client == client2); context.assertEquals(resp.statusCode(), 200); context.assertEquals(resp.body().toString(), "hello"); reference.release();
public static <X>java.lang.Object getAs(io.vertx.servicediscovery.ServiceReference j_receiver, java.lang.Class<java.lang.Object> x) { return io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.getAs(x)); } public static <X>java.lang.Object cachedAs(io.vertx.servicediscovery.ServiceReference j_receiver, java.lang.Class<java.lang.Object> x) {
@Test public void testAutoCloseable(TestContext context) { Async async = context.async(); // Publish the service Record record = HttpEndpoint.createRecord("hello-service", "localhost", 8080, "/foo"); discovery.publish(record, rec -> { Record published = rec.result(); discovery.getRecord(new JsonObject().put("name", "hello-service"), found -> { context.assertTrue(found.succeeded()); context.assertTrue(found.result() != null); Record match = found.result(); try (ServiceReference reference = discovery.getReference(match)) { context.assertEquals(reference.record().getLocation().getString("endpoint"), "http://localhost:8080/foo"); context.assertFalse(reference.record().getLocation().getBoolean("ssl")); WebClient client = reference.getAs(WebClient.class); WebClient client2 = reference.cachedAs(WebClient.class); context.assertTrue(client == client2); } catch (Exception e) { context.fail(e); } discovery.unpublish(published.getRegistration(), v -> async.complete()); }); }); }
/** * Releases the reference. Once released, the consumer must not use the reference anymore. * This method must be idempotent and defensive, as multiple call may happen. */ public void release() { delegate.release(); }
/** * Checks whether or not the service reference has the given service object. * @param object the service object, must not be <code>null</code> * @return <code>true</code> if the service reference service object is equal to the given object, <code>false</code> otherwise. */ public boolean isHolding(Object object) { boolean ret = delegate.isHolding(object); return ret; }
public static <X>java.lang.Object cachedAs(io.vertx.servicediscovery.ServiceReference j_receiver, java.lang.Class<java.lang.Object> x) { return io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.cachedAs(x)); } public static <T>java.lang.Object cached(io.vertx.servicediscovery.ServiceReference j_receiver) {
/** * Gets the object to access the service. It can be a proxy, a client or whatever object. The type depends on the * service type and the server itself. This method returns the Java version and primary facet of the object, use * {@link io.vertx.rxjava.servicediscovery.ServiceReference#getAs} to retrieve the polyglot instance of the object or another facet.. * @return the object to access the service */ public <T> T get() { T ret = (T) delegate.get(); return ret; }
assertThat(usages.get(0).getString("id")).isNotNull().isNotEmpty(); assertThat((HelloService) reference.cached()).isNull(); assertThat((HelloService) reference.get()).isNotNull(); assertThat((HelloService) reference.cached()).isNotNull(); reference.release(); Assertions.assertThat(discovery.bindings()).isEmpty(); await().until(() -> usages.size() == 2); reference.release(); Assertions.assertThat(discovery.bindings()).isEmpty(); Thread.sleep(100);
message.reply("FAIL - reference is null"); } else { HttpClient client = reference.get(); result.put("client", client.toString()); result.put("direct", reference.get().toString()); reference.release(); result.put("bindings", getBindings(discovery)); message.reply(result); message.reply("FAIL - reference is null"); } else { WebClient client = reference.getAs(WebClient.class); result.put("client", client.toString()); result.put("direct", reference.get().toString()); reference.release(); result.put("bindings", getBindings(discovery)); message.reply(result); message.reply("FAIL - reference is null"); } else { HelloService client = reference.get(); result.put("client", client.toString()); reference.release(); result.put("bindings", getBindings(discovery)); message.reply(result); message.reply("FAIL - reference is null"); } else { JDBCClient client = reference.get();
@Test public void testPublicationAndConsumption(TestContext context) { Async async = context.async(); // Publish the service Record record = HttpEndpoint.createRecord("hello-service", "localhost", 8080, "/foo"); discovery.publish(record, rec -> { Record published = rec.result(); discovery.getRecord(new JsonObject().put("name", "hello-service"), found -> { context.assertTrue(found.succeeded()); context.assertTrue(found.result() != null); Record match = found.result(); ServiceReference reference = discovery.getReference(match); context.assertEquals(reference.record().getLocation().getString("endpoint"), "http://localhost:8080/foo"); context.assertFalse(reference.record().getLocation().getBoolean("ssl")); HttpClient client = reference.get(); HttpClient client2 = reference.get(); context.assertTrue(client == client2); client.getNow("/foo", context.asyncAssertSuccess(response -> { context.assertEquals(response.statusCode(), 200); response.bodyHandler(body -> { context.assertEquals(body.toString(), "hello"); reference.release(); discovery.unpublish(published.getRegistration(), v -> { async.complete(); }); }); })); }); }); }
Record match = found.result(); ServiceReference reference = discovery.getReference(match); context.assertEquals(reference.record().getLocation().getString("endpoint"), "http://localhost:8080/foo"); context.assertFalse(reference.record().getLocation().getBoolean("ssl")); WebClient client = reference.getAs(WebClient.class); WebClient client2 = reference.cachedAs(WebClient.class); context.assertTrue(client == client2); context.assertEquals(resp.statusCode(), 200); context.assertEquals(resp.body().toString(), "hello"); reference.release();
/** * 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))); } }); }