package io.vertx.core.impl;
import io.vertx.core.*;
import io.vertx.core.spi.metrics.Metrics;
import io.vertx.core.spi.metrics.MetricsProvider;
import io.vertx.core.spi.metrics.PoolMetrics;
class WorkerExecutorImpl implements MetricsProvider, WorkerExecutorInternal {
private final Context ctx;
private final VertxImpl.SharedWorkerPool pool;
private boolean closed;
public WorkerExecutorImpl(Context ctx, VertxImpl.SharedWorkerPool pool) {
this.ctx = ctx;
this.pool = pool;
}
@Override
public Metrics getMetrics() {
return pool.metrics();
}
@Override
public boolean isMetricsEnabled() {
PoolMetrics metrics = pool.metrics();
return metrics != null;
}
@Override
public Vertx vertx() {
return ctx.owner();
}
@Override
public WorkerPool getPool() {
return pool;
}
public synchronized <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<T>> asyncResultHandler) {
if (closed) {
throw new IllegalStateException("Worker executor closed");
}
ContextImpl context = (ContextImpl) ctx.owner().getOrCreateContext();
context.executeBlocking(blockingCodeHandler, asyncResultHandler, pool.executor(), ordered ? context.orderedTasks : null, pool.metrics());
}
@Override
public void close() {
synchronized (this) {
if (!closed) {
closed = true;
} else {
return;
}
}
ctx.removeCloseHook(this);
pool.release();
}
@Override
public void close(Handler<AsyncResult<Void>> completionHandler) {
close();
completionHandler.handle(Future.succeededFuture());
}
}