Search in sources :

Example 1 with Closeable

use of io.vertx.core.Closeable in project vert.x by eclipse.

the class CloseHooks method run.

/**
   * Run the close hooks.
   *
   * @param completionHandler called when all hooks have beene executed
   */
void run(Handler<AsyncResult<Void>> completionHandler) {
    Set<Closeable> copy = null;
    synchronized (this) {
        if (closeHooksRun) {
            // Sanity check
            throw new IllegalStateException("Close hooks already run");
        }
        closeHooksRun = true;
        if (closeHooks != null && !closeHooks.isEmpty()) {
            // Must copy before looping as can be removed during loop otherwise
            copy = new HashSet<>(closeHooks);
        }
    }
    if (copy != null && !copy.isEmpty()) {
        int num = copy.size();
        if (num != 0) {
            AtomicInteger count = new AtomicInteger();
            AtomicBoolean failed = new AtomicBoolean();
            for (Closeable hook : copy) {
                Future<Void> a = Future.future();
                a.setHandler(ar -> {
                    if (ar.failed()) {
                        if (failed.compareAndSet(false, true)) {
                            completionHandler.handle(Future.failedFuture(ar.cause()));
                        }
                    } else {
                        if (count.incrementAndGet() == num) {
                            completionHandler.handle(Future.succeededFuture());
                        }
                    }
                });
                try {
                    hook.close(a);
                } catch (Throwable t) {
                    log.warn("Failed to run close hooks", t);
                    a.tryFail(t);
                }
            }
        } else {
            completionHandler.handle(Future.succeededFuture());
        }
    } else {
        completionHandler.handle(Future.succeededFuture());
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Closeable(io.vertx.core.Closeable)

Example 2 with Closeable

use of io.vertx.core.Closeable in project vert.x by eclipse.

the class VertxTest method testCloseHookFailure1.

@Test
public void testCloseHookFailure1() throws Exception {
    AtomicInteger closedCount = new AtomicInteger();
    class Hook implements Closeable {

        @Override
        public void close(Handler<AsyncResult<Void>> completionHandler) {
            if (closedCount.incrementAndGet() == 1) {
                throw new RuntimeException();
            } else {
                completionHandler.handle(Future.succeededFuture());
            }
        }
    }
    VertxInternal vertx = (VertxInternal) Vertx.vertx();
    vertx.addCloseHook(new Hook());
    vertx.addCloseHook(new Hook());
    // Now undeploy
    vertx.close(ar -> {
        assertTrue(ar.succeeded());
        assertEquals(2, closedCount.get());
        testComplete();
    });
    await();
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Closeable(io.vertx.core.Closeable) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 3 with Closeable

use of io.vertx.core.Closeable in project vert.x by eclipse.

the class CloseFuture method close.

/**
 * Run all close hooks, after completion of all hooks, the future is closed.
 *
 * @return the future completed after completion of all close hooks
 */
public Future<Void> close() {
    boolean close;
    List<Closeable> toClose;
    synchronized (this) {
        close = !closed;
        toClose = hooks != null ? new ArrayList<>(hooks.keySet()) : null;
        closed = true;
        hooks = null;
    }
    if (close) {
        // We want an immutable version of the list holding strong references to avoid racing against finalization
        if (toClose != null && !toClose.isEmpty()) {
            int num = toClose.size();
            AtomicInteger count = new AtomicInteger();
            for (Closeable hook : toClose) {
                Promise<Void> closePromise = Promise.promise();
                closePromise.future().onComplete(ar -> {
                    if (count.incrementAndGet() == num) {
                        promise.complete();
                    }
                });
                try {
                    hook.close(closePromise);
                } catch (Throwable t) {
                    if (log != null) {
                        log.warn("Failed to run close hook", t);
                    }
                    closePromise.tryFail(t);
                }
            }
        } else {
            promise.complete();
        }
    }
    return promise.future();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Closeable(io.vertx.core.Closeable) ArrayList(java.util.ArrayList)

Example 4 with Closeable

use of io.vertx.core.Closeable in project vert.x by eclipse.

the class VertxTest method testCloseHooksCalled.

@Test
public void testCloseHooksCalled() throws Exception {
    AtomicInteger closedCount = new AtomicInteger();
    Closeable myCloseable1 = completionHandler -> {
        closedCount.incrementAndGet();
        completionHandler.handle(Future.succeededFuture());
    };
    Closeable myCloseable2 = completionHandler -> {
        closedCount.incrementAndGet();
        completionHandler.handle(Future.succeededFuture());
    };
    VertxInternal vertx = (VertxInternal) Vertx.vertx();
    vertx.addCloseHook(myCloseable1);
    vertx.addCloseHook(myCloseable2);
    // Now undeploy
    vertx.close(ar -> {
        assertTrue(ar.succeeded());
        assertEquals(2, closedCount.get());
        testComplete();
    });
    await();
}
Also used : Closeable(io.vertx.core.Closeable) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Vertx(io.vertx.core.Vertx) Test(org.junit.Test) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) Future(io.vertx.core.Future) VertxInternal(io.vertx.core.impl.VertxInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Closeable(io.vertx.core.Closeable) Test(org.junit.Test)

Example 5 with Closeable

use of io.vertx.core.Closeable in project vert.x by eclipse.

the class VertxTest method testCloseHookFailure2.

@Test
public void testCloseHookFailure2() throws Exception {
    AtomicInteger closedCount = new AtomicInteger();
    class Hook implements Closeable {

        @Override
        public void close(Handler<AsyncResult<Void>> completionHandler) {
            if (closedCount.incrementAndGet() == 1) {
                completionHandler.handle(Future.succeededFuture());
                throw new RuntimeException();
            } else {
                completionHandler.handle(Future.succeededFuture());
            }
        }
    }
    VertxInternal vertx = (VertxInternal) Vertx.vertx();
    vertx.addCloseHook(new Hook());
    vertx.addCloseHook(new Hook());
    // Now undeploy
    vertx.close(ar -> {
        assertTrue(ar.succeeded());
        assertEquals(2, closedCount.get());
        testComplete();
    });
    await();
}
Also used : VertxInternal(io.vertx.core.impl.VertxInternal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Closeable(io.vertx.core.Closeable) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Aggregations

Closeable (io.vertx.core.Closeable)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Handler (io.vertx.core.Handler)3 VertxInternal (io.vertx.core.impl.VertxInternal)3 Test (org.junit.Test)3 AsyncResult (io.vertx.core.AsyncResult)1 Future (io.vertx.core.Future)1 Vertx (io.vertx.core.Vertx)1 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1