All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vertx.core.impl.EventLoopContext Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */

package io.vertx.core.impl;

import io.netty.channel.EventLoop;
import io.vertx.core.Handler;

import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionException;

/**
 * @author Tim Fox
 */
public class EventLoopContext extends ContextBase {

  EventLoopContext(VertxInternal vertx,
                   EventLoop eventLoop,
                   WorkerPool internalBlockingPool,
                   WorkerPool workerPool,
                   Deployment deployment,
                   CloseFuture closeFuture,
                   ClassLoader tccl) {
    super(vertx, eventLoop, internalBlockingPool, workerPool, deployment, closeFuture, tccl);
  }

  @Override
  public Executor executor() {
    return nettyEventLoop();
  }

  @Override
  protected void runOnContext(ContextInternal ctx, Handler action) {
    try {
      nettyEventLoop().execute(() -> ctx.dispatch(action));
    } catch (RejectedExecutionException ignore) {
      // Pool is already shut down
    }
  }

  @Override
  protected  void emit(ContextInternal ctx, T argument, Handler 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));
    }
  }

  /**
   * 
    *
  • When the current thread is event-loop thread of this context the implementation will execute the {@code task} directly
  • *
  • Otherwise the task will be scheduled on the event-loop thread for execution
  • *
*/ @Override protected void execute(ContextInternal ctx, T argument, Handler task) { EventLoop eventLoop = nettyEventLoop(); if (eventLoop.inEventLoop()) { task.handle(argument); } else { eventLoop.execute(() -> task.handle(argument)); } } @Override protected void execute(ContextInternal ctx, Runnable task) { EventLoop eventLoop = nettyEventLoop(); if (eventLoop.inEventLoop()) { task.run(); } else { eventLoop.execute(task); } } @Override public boolean isEventLoopContext() { return true; } @Override public boolean isWorkerContext() { return false; } @Override public boolean inThread() { return nettyEventLoop().inEventLoop(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy