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());
}
}
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();
}
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();
}
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();
}
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();
}
Aggregations