package io.vertx.core.impl;
import io.netty.channel.EventLoop;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.spi.tracing.VertxTracer;
import java.util.concurrent.RejectedExecutionException;
public class EventLoopContext extends ContextImpl {
EventLoopContext(VertxInternal vertx,
VertxTracer<?, ?> tracer,
EventLoop eventLoop,
WorkerPool internalBlockingPool,
WorkerPool workerPool,
Deployment deployment,
CloseHooks closeHooks,
ClassLoader tccl) {
super(vertx, tracer, eventLoop, internalBlockingPool, workerPool, deployment, closeHooks, tccl);
}
@Override
void runOnContext(AbstractContext ctx, Handler<Void> action) {
try {
nettyEventLoop().execute(() -> ctx.dispatch(action));
} catch (RejectedExecutionException ignore) {
}
}
@Override
<T> void emit(AbstractContext ctx, T argument, Handler<T> task) {
EventLoop eventLoop = nettyEventLoop();
if (eventLoop.inEventLoop()) {
ContextInternal prev = ctx.beginDispatch();
try {
task.handle(argument);
} catch (Throwable t) {
reportException(t);
} finally {
ctx.endDispatch(prev);
}
} else {
eventLoop.execute(() -> emit(ctx, argument, task));
}
}
@Override
<T> void execute(AbstractContext ctx, T argument, Handler<T> task) {
EventLoop eventLoop = nettyEventLoop();
if (eventLoop.inEventLoop()) {
task.handle(argument);
} else {
eventLoop.execute(() -> task.handle(argument));
}
}
@Override
<T> void execute(AbstractContext ctx, Runnable task) {
EventLoop eventLoop = nettyEventLoop();
if (eventLoop.inEventLoop()) {
task.run();
} else {
eventLoop.execute(task);
}
}
@Override
public boolean isEventLoopContext() {
return true;
}
@Override
boolean inThread() {
return nettyEventLoop().inEventLoop();
}
}