Package org.vertx.java.core.impl

Examples of org.vertx.java.core.impl.DefaultContext


  This is important e.g. if a user is using a 3rd party library which returns a result on it's own thread,
  we don't then want a new context being created if the user calls runOnContext
   */
  @Override
  public void runOnContext(final Handler<Void> task) {
    DefaultContext context = getContext();
    if (context == null) {
      throw new IllegalStateException("Not on a Vert.x context");
    }
    context.runOnContext(task);
  }
View Full Code Here


* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class PathAdjuster {

  public static Path adjust(final VertxInternal vertx, Path path) {
    DefaultContext context = vertx.getContext();
    if (context != null) {
      Path adjustment = context.getPathAdjustment();
      if (adjustment != null) {
        return adjustment.resolve(path);
      }
    }
    return path;
View Full Code Here

      FlowControlStateEvent evt = (FlowControlStateEvent) obj;
      final Channel ch = ctx.channel();
      final C conn = connectionMap.get(ch);
      if (conn != null) {
        conn.setWritable(evt.isWritable());
        DefaultContext context = getContext(conn);
        if (context.isOnCorrectWorker(ch.eventLoop())) {
          try {
            vertx.setContext(context);
            conn.handleInterestedOpsChanged();
          } catch (Throwable t) {
            context.reportException(t);
          }
        } else {
          context.execute(new Runnable() {
            public void run() {
              conn.handleInterestedOpsChanged();
            }
          });
        }
View Full Code Here

  @Override
  public void exceptionCaught(ChannelHandlerContext chctx, final Throwable t) throws Exception {
    final Channel ch = chctx.channel();
    final C sock = connectionMap.remove(ch);
    if (sock != null) {
      DefaultContext context = getContext(sock);
      if (context.isOnCorrectWorker(ch.eventLoop())) {
        try {
          vertx.setContext(context);
          sock.handleException(t);
        } catch (Throwable tt) {
          context.reportException(tt);
        }
        try {
          ch.close();
        } catch (Throwable ignore) {
        }
      } else {
        context.execute(new Runnable() {
          public void run() {
          sock.handleException(t);
          try {
            ch.close();
          } catch (Throwable ignore) {
View Full Code Here

  @Override
  public void channelInactive(ChannelHandlerContext chctx) throws Exception {
    final Channel ch = chctx.channel();
    final C sock = connectionMap.remove(ch);
    if (sock != null) {
      DefaultContext context = getContext(sock);
      if (context.isOnCorrectWorker(ch.eventLoop())) {
        try {
          vertx.setContext(context);
          sock.handleClosed();
        } catch (Throwable t) {
          context.reportException(t);
        }
      } else {
        context.execute(new Runnable() {
          public void run() {
            sock.handleClosed();
          }
        });
      }
View Full Code Here

  public void messageReceived(final ChannelHandlerContext chctx, final Object msg) throws Exception {
    final Channel ch = chctx.channel();

    final C connection = connectionMap.get(ch);
    if (connection != null) {
      DefaultContext context = getContext(connection);
      // We need to do this since it's possible the server is being used from a worker context
      if (context.isOnCorrectWorker(ch.eventLoop())) {
        try {
          vertx.setContext(context);
          doMessageReceived(connection, chctx, msg);
        } catch (Throwable t) {
          context.reportException(t);
        }
      } else {
        BufUtil.retain(msg);
        context.execute(new Runnable() {
          public void run() {
            try {
              doMessageReceived(connection, chctx, msg);
            } catch (Exception e) {
              ch.pipeline().fireExceptionCaught(e);
View Full Code Here

  public void inboundBufferUpdated(ChannelHandlerContext chctx) {
    final ByteBuf in = chctx.inboundByteBuffer();
    final DefaultNetSocket sock = connectionMap.get(chctx.channel());
    if (sock != null) {
      Channel ch = chctx.channel();
      DefaultContext context = getContext(sock);
      // We need to do this since it's possible the server is being used from a worker context
      if (context.isOnCorrectWorker(ch.eventLoop())) {
        try {
          vertx.setContext(context);
          sock.handleDataReceived(new Buffer(in.slice()));
        } catch (Throwable t) {
          context.reportException(t);
        }
      } else {
        final ByteBuf buf = in.readBytes(in.readableBytes());
        context.execute(new Runnable() {
          public void run() {
            sock.handleDataReceived(new Buffer(buf));
          }
        });
      }
View Full Code Here

  }

  public EventBus unregisterHandler(String address, Handler<? extends Message> handler,
                                    Handler<AsyncResult<Void>> completionHandler) {
    checkStarted();
    DefaultContext context = vertx.getOrAssignContext();
    Handlers handlers = handlerMap.get(address);
    if (handlers != null) {
      synchronized (handlers) {
        int size = handlers.list.size();
        // Requires a list traversal. This is tricky to optimise since we can't use a set since
        // we need fast ordered traversal for the round robin
        for (int i = 0; i < size; i++) {
          HandlerHolder holder = handlers.list.get(i);
          if (holder.handler == handler) {
            handlers.list.remove(i);
            holder.removed = true;
            if (handlers.list.isEmpty()) {
              handlerMap.remove(address);
              if (subs != null && !holder.localOnly) {
                removeSub(address, serverID, completionHandler);
              } else if (completionHandler != null) {
                callCompletionHandler(completionHandler);
              }
            } else if (completionHandler != null) {
              callCompletionHandler(completionHandler);
            }
            context.removeCloseHook(new HandlerEntry(address, handler));
            return this;
          }
        }
      }
    }
View Full Code Here

    sendOrPub(null, message, replyHandler);
  }

  private void sendOrPub(ServerID replyDest, final BaseMessage message, final Handler replyHandler) {
    checkStarted();
    DefaultContext context = vertx.getOrAssignContext();
    try {
      message.sender = serverID;
      if (replyHandler != null) {
        message.replyAddress = prefix + String.valueOf(seq.incrementAndGet());
        registerHandler(message.replyAddress, replyHandler, null, true, true);
View Full Code Here

                               boolean replyHandler, boolean localOnly) {
    checkStarted();
    if (address == null) {
      throw new NullPointerException("address");
    }
    DefaultContext context = vertx.getContext();
    boolean hasContext = context != null;
    if (!hasContext) {
      context = vertx.createEventLoopContext();
    }
    Handlers handlers = handlerMap.get(address);
    if (handlers == null) {
      handlers = new Handlers();
      Handlers prevHandlers = handlerMap.putIfAbsent(address, handlers);
      if (prevHandlers != null) {
        handlers = prevHandlers;
      }
      if (completionHandler == null) {
        completionHandler = new Handler<AsyncResult<Void>>() {
          public void handle(AsyncResult<Void> event) {
            if (event.failed()) {
              log.error("Failed to remove entry", event.cause());
            }
          }
        };
      }
      handlers.list.add(new HandlerHolder(handler, replyHandler, localOnly, context));
      if (subs != null && !replyHandler && !localOnly) {
        // Propagate the information
        subs.put(address, serverID, completionHandler);
      } else {
        callCompletionHandler(completionHandler);
      }
    } else {
      handlers.list.add(new HandlerHolder(handler, replyHandler, localOnly, context));
      if (completionHandler != null) {
        callCompletionHandler(completionHandler);
      }
    }
    if (hasContext) {
      HandlerEntry entry = new HandlerEntry(address, handler);
      context.addCloseHook(entry);
    }
  }
View Full Code Here

TOP

Related Classes of org.vertx.java.core.impl.DefaultContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.