Java Code Examples for io.vertx.core.Vertx#close()

The following examples show how to use io.vertx.core.Vertx#close() . 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: UnixDomainSocketTest.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
@Test
public void connectWithVertxInstance(TestContext context) {
  assumeTrue(options.isUsingDomainSocket());
  Vertx vertx = Vertx.vertx(new VertxOptions().setPreferNativeTransport(true));
  try {
    client = PgPool.pool(vertx, new PgConnectOptions(options), new PoolOptions());
    Async async = context.async();
    client.getConnection(context.asyncAssertSuccess(pgConnection -> {
      async.complete();
      pgConnection.close();
    }));
    async.await();
  } finally {
    vertx.close();
  }
}
 
Example 2
Source File: DeploymentTest.java    From vertx-lang-groovy with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeployVerticleGroovyScriptNoStop() throws Exception {
  Vertx vertx = Vertx.vertx();
  try {
    BlockingQueue<AsyncResult<String>> deployed = new ArrayBlockingQueue<>(1);
    vertx.deployVerticle("io/vertx/lang/groovy//NoStopVerticleScript.groovy", deployed::add);
    AsyncResult<String> deployment = deployed.poll(10, TimeUnit.SECONDS);
    String deploymentId = assertResult(deployment);
    BlockingQueue<AsyncResult<Void>> undeployed = new ArrayBlockingQueue<>(1);
    vertx.undeploy(deploymentId, undeployed::add);
    AsyncResult<?> undeployment = undeployed.poll(10, TimeUnit.SECONDS);
    assertResult(undeployment);
  } finally {
    vertx.close();
  }
}
 
Example 3
Source File: TestVertxUI.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Test (expected = DL4JException.class)
public void testUIStartPortAlreadyBound() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    //Create HttpServer that binds the same port
    int port = VertxUIServer.DEFAULT_UI_PORT;
    Vertx vertx = Vertx.vertx();
    vertx.createHttpServer()
            .requestHandler(event -> {})
            .listen(port, result -> latch.countDown());
    latch.await();

    try {
        //DL4JException signals that the port cannot be bound, UI server cannot start
        UIServer.getInstance();
    } finally {
        vertx.close();
    }
}
 
Example 4
Source File: VertxUtils.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
public static void blockCloseVertx(Vertx vertx) {
  CountDownLatch latch = new CountDownLatch(1);
  vertx.close(ar -> {
    if (ar.succeeded()) {
      LOGGER.info("Success to close vertx {}.", vertx);
    } else {
      LOGGER.info("Failed to close vertx {}.", vertx);
    }

    latch.countDown();
  });

  try {
    latch.await();
  } catch (InterruptedException e) {
    LOGGER.info("Failed to wait close vertx {}.", vertx);
  }
}
 
Example 5
Source File: Http2TestCase.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttp2EnabledSsl() throws ExecutionException, InterruptedException {
    Assumptions.assumeTrue(JdkSSLEngineOptions.isAlpnAvailable()); //don't run on JDK8
    Vertx vertx = Vertx.vertx();
    try {
        WebClientOptions options = new WebClientOptions()
                .setUseAlpn(true)
                .setProtocolVersion(HttpVersion.HTTP_2)
                .setSsl(true)
                .setKeyStoreOptions(
                        new JksOptions().setPath("src/test/resources/client-keystore.jks").setPassword("password"))
                .setTrustStoreOptions(
                        new JksOptions().setPath("src/test/resources/client-truststore.jks").setPassword("password"));

        WebClient client = WebClient.create(vertx, options);
        int port = sslUrl.getPort();

        runTest(client, port);

    } finally {
        vertx.close();
    }
}
 
Example 6
Source File: MetricsServiceImplTest.java    From vertx-micrometer-metrics with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldGetJvmMetricsInSnapshot(TestContext ctx) {
  MetricsOptions metricsOptions = new MicrometerMetricsOptions()
    .setJvmMetricsEnabled(true)
    .setMicrometerRegistry(new SimpleMeterRegistry())
    .setRegistryName(registryName)
    .setEnabled(true);
  VertxOptions vertxOptions = new VertxOptions().setMetricsOptions(metricsOptions);
  Vertx vertx = Vertx.vertx(vertxOptions)
    .exceptionHandler(ctx.exceptionHandler());

  JsonObject snapshot = MetricsService.create(vertx).getMetricsSnapshot("jvm");

  assertFalse(snapshot.isEmpty());

  vertx.close(ctx.asyncAssertSuccess());
}
 
Example 7
Source File: Http2TestCase.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttp2EnabledPlain() throws ExecutionException, InterruptedException {
    Vertx vertx = Vertx.vertx();
    try {
        WebClientOptions options = new WebClientOptions()
                .setProtocolVersion(HttpVersion.HTTP_2)
                .setHttp2ClearTextUpgrade(true);
        WebClient client = WebClient.create(vertx, options);
        runTest(client, url.getPort());

    } finally {
        vertx.close();
    }
}
 
Example 8
Source File: PortfolioVerticleTest.java    From vertx-kubernetes-workshop with Apache License 2.0 5 votes vote down vote up
@Test
public void testServiceAccess() {
  Vertx vertx = Vertx.vertx();
  vertx.deployVerticle(PortfolioVerticle.class.getName());

  PortfolioService proxy = ProxyHelper.createProxy(PortfolioService.class, vertx, PortfolioService.ADDRESS);

  assertThat(proxy).isNotNull();
  AtomicReference<Portfolio> reference = new AtomicReference<>();
  proxy.getPortfolio(ar -> reference.set(ar.result()));

  await().untilAtomic(reference, not(nullValue()));

  vertx.close();
}
 
Example 9
Source File: VertxNetUtils.java    From Lealone-Plugins with Apache License 2.0 5 votes vote down vote up
public static void closeVertx(Vertx v) {
    synchronized (VertxNetUtils.class) {
        v.close();
        if (v == vertx) {
            vertx = null;
        }
    }
}
 
Example 10
Source File: VertxExtensionTest.java    From vertx-junit5 with Apache License 2.0 5 votes vote down vote up
@AfterEach
void cleanup(Vertx vertx, VertxTestContext testContext) {
  assertThat(testContext).isNotSameAs(previousTestContext);
  previousTestContext = testContext;
  assertThat(vertx.deploymentIDs()).isNotEmpty().hasSize(1);
  vertx.close(testContext.succeeding(v -> testContext.completeNow()));
}
 
Example 11
Source File: UseEventLoopTest.java    From vertx-unit with Apache License 2.0 5 votes vote down vote up
@org.junit.Test
public void testWitVertx() {
  Vertx vertx = Vertx.vertx();
  List<Context> contexts = Collections.synchronizedList(new ArrayList<>());
  TestSuiteImpl suite = (TestSuiteImpl) TestSuite.create("my_suite").test("my_test", context -> {
    contexts.add(Vertx.currentContext());
  });
  TestReporter reporter = new TestReporter();
  suite.runner().setReporter(reporter).setUseEventLoop(true).setVertx(vertx).run();
  reporter.await();
  assertEquals(0, reporter.exceptions.size());
  assertEquals(1, reporter.results.size());
  assertTrue(reporter.results.get(0).succeeded());
  assertEquals(1, contexts.size());
  assertNotNull(contexts.get(0));
  reporter = new TestReporter();
  suite.runner().setReporter(reporter).setUseEventLoop(false).setVertx(vertx).run();
  reporter.await();
  assertEquals(0, reporter.exceptions.size());
  assertEquals(1, reporter.results.size());
  assertTrue(reporter.results.get(0).succeeded());
  assertEquals(2, contexts.size());
  assertNull(contexts.get(1));
  reporter = new TestReporter();
  suite.runner().setReporter(reporter).setUseEventLoop(null).setVertx(vertx).run();
  reporter.await();
  assertEquals(0, reporter.exceptions.size());
  assertEquals(1, reporter.results.size());
  assertTrue(reporter.results.get(0).succeeded());
  assertEquals(3, contexts.size());
  assertNotNull(contexts.get(2));
  vertx.close();
}
 
Example 12
Source File: WebLifeCycleProcessor.java    From festival with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeStop(ApplicationContext applicationContext) throws Exception {
    Vertx vertx = applicationContext.getBean(Vertx.class);
    CountDownLatch latch = new CountDownLatch(1);
    vertx.close(res -> {
        if (res.succeeded()) {
            if (log.isInfoEnabled()) {
                log.info("closed vertx!");
            }
        }
        latch.countDown();
    });
    latch.await();
}
 
Example 13
Source File: GuicedInjectionTest.java    From rest.vertx with Apache License 2.0 4 votes vote down vote up
@AfterEach
void lastChecks(Vertx vertx) {
    vertx.close(vertxTestContext.succeeding());
    vertx.close();
}
 
Example 14
Source File: VertxTest.java    From rest.vertx with Apache License 2.0 4 votes vote down vote up
@AfterEach
void lastChecks(Vertx vertx) {
    vertx.close(vertxTestContext.succeeding());
}
 
Example 15
Source File: EthSigner.java    From ethsigner with Apache License 2.0 4 votes vote down vote up
public void run() {

    final Duration downstreamHttpRequestTimeout = config.getDownstreamHttpRequestTimeout();
    if (downstreamHttpRequestTimeout.toMillis() <= 0) {
      LOG.error("Http request timeout must be greater than 0.");
      return;
    }

    if (config.getHttpListenHost().equals(config.getDownstreamHttpHost())
        && config.getHttpListenPort().equals(config.getDownstreamHttpPort())) {
      LOG.error("Http host and port must be different to the downstream host and port.");
      return;
    }

    final JsonDecoder jsonDecoder = createJsonDecoder();

    final HttpServerOptions serverOptions =
        new HttpServerOptions()
            .setPort(config.getHttpListenPort())
            .setHost(config.getHttpListenHost())
            .setReuseAddress(true)
            .setReusePort(true);

    final Vertx vertx = Vertx.vertx();
    try {
      final Runner runner =
          new Runner(
              config.getChainId().id(),
              transactionSignerProvider,
              webClientOptionsFactory.createWebClientOptions(config),
              applyConfigTlsSettingsTo(serverOptions),
              downstreamHttpRequestTimeout,
              new DownstreamPathCalculator(config.getDownstreamHttpPath()),
              jsonDecoder,
              config.getDataPath(),
              vertx,
              config.getCorsAllowedOrigins());

      runner.start();
    } catch (final Throwable t) {
      vertx.close();
      throw new InitializationException("Failed to create http service.", t);
    }
  }
 
Example 16
Source File: MetricRegistryTest.java    From vertx-dropwizard-metrics with Apache License 2.0 4 votes vote down vote up
@Test
public void testVertxWithoutSetMetricRegistryOption() {
  Vertx vertx = Vertx.vertx(getOptionsWithoutSetMetricRegistry());
  assertNotNull(vertx);
  vertx.close();
}
 
Example 17
Source File: MetricRegistryTest.java    From vertx-dropwizard-metrics with Apache License 2.0 4 votes vote down vote up
@Test
public void testVertxWithSetMetricRegistryOption() {
  Vertx vertx = Vertx.vertx(getOptionsWithSetMetricRegistry());
  assertNotNull(vertx);
  vertx.close();
}
 
Example 18
Source File: RestLauncherTest.java    From raml-module-builder with Apache License 2.0 4 votes vote down vote up
@Override
public void afterStartingVertx(Vertx vertx) {
  vertx.close();
}
 
Example 19
Source File: AmqpBridgeReceiver.java    From strimzi-kafka-bridge with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {

        Vertx vertx = Vertx.vertx();

        AmqpBridgeReceiver receiver = new AmqpBridgeReceiver();

        // multiple receivers on same connection, same session but different links
        AmqpBridgeReceiver.ExampleOne ex1 = receiver.new ExampleOne();
        ex1.run(vertx);

        vertx.close();
    }
 
Example 20
Source File: ApimanLauncher.java    From apiman with Apache License 2.0 2 votes vote down vote up
/**
 * A deployment failure has been encountered. You can override this method to customize the behavior.
 * By default it closes the `vertx` instance.
 *
 * @param vertx             the vert.x instance
 * @param mainVerticle      the verticle
 * @param deploymentOptions the verticle deployment options
 * @param cause             the cause of the failure
 */
@Override
public void handleDeployFailed(Vertx vertx, String mainVerticle, DeploymentOptions deploymentOptions, Throwable cause) {
  // Default behaviour is to close Vert.x if the deploy failed
  vertx.close();
}