public synchronized void removeHandler(T handler, ContextInternal context) { EventLoop worker = context.nettyEventLoop(); Handlers<T> handlers = handlerMap.get(worker); if (!handlers.removeHandler(new HandlerHolder<>(context, handler))) { throw new IllegalStateException("Can't find handler"); } if (handlers.isEmpty()) { handlerMap.remove(worker); } if (handlerMap.isEmpty()) { hasHandlers = false; } //Available workers does it's own reference counting -since workers can be shared across different Handlers availableWorkers.removeWorker(worker); }
@Override public void get(final K k, Handler<AsyncResult<ChoosableIterable<V>>> asyncResultHandler) { ContextInternal ctx = vertx.getOrCreateContext(); ctx.executeBlocking(fut -> { ChoosableIterable<V> it = map.get(k); if (it == null) { it = new ChoosableSet<>(0); } fut.complete(it); }, taskQueue, asyncResultHandler); }
@Override public void onSettingsAckRead(ChannelHandlerContext ctx) { Handler<Void> handler; synchronized (this) { handler = updateSettingsHandlers.poll(); } if (handler != null) { // No need to run on a particular context it shall be done by the handler instead context.executeFromIO(handler); } }
public void resolveHostname(String hostname, Handler<AsyncResult<InetAddress>> resultHandler) { ContextInternal callback = (ContextInternal) vertx.getOrCreateContext(); io.netty.resolver.AddressResolver<InetSocketAddress> resolver = resolverGroup.getResolver(callback.nettyEventLoop()); io.netty.util.concurrent.Future<InetSocketAddress> fut = resolver.resolve(InetSocketAddress.createUnresolved(hostname, 0)); fut.addListener(a -> { callback.runOnContext(v -> { if (a.isSuccess()) { InetSocketAddress address = fut.getNow(); resultHandler.handle(Future.succeededFuture(address.getAddress())); } else { resultHandler.handle(Future.failedFuture(a.cause())); } }); }); }
void run() { inProgressMap.put(msg.id(), this); timerID = vertx.setTimer(options.getQueryTimeout(), id -> { timerID = -1; actualCtx.runOnContext(v -> { fail(new VertxException("DNS query timeout for " + name)); }); }); channel.writeAndFlush(msg).addListener((ChannelFutureListener) future -> { if (!future.isSuccess()) { actualCtx.executeFromIO(future.cause(), this::fail); } }); }
@Test public void testReportExceptionToContext() { ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext(); RuntimeException expected = new RuntimeException(); AtomicReference<Throwable> err = new AtomicReference<>(); ctx.exceptionHandler(err::set); ctx.reportException(expected); assertSame(expected, err.get()); } }
sslHelper.validate(vertx); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(context.nettyEventLoop()); bootstrap.channelFactory(vertx.transport().channelFactory(remoteAddress.path() != null)); context.executeFromIO(v -> { log.debug("Failed to create connection. Will retry in " + options.getReconnectInterval() + " milliseconds");
private void executeCloseDone(final ContextInternal closeContext, final Handler<AsyncResult<Void>> done, final Exception e) { if (done != null) { Future<Void> fut = e != null ? Future.failedFuture(e) : Future.succeededFuture(); closeContext.runOnContext((v) -> done.handle(fut)); } }
private Connection createConnection(ChannelHandlerContext chctx) { return new Connection(context.owner(), chctx, context); }
Http1xServerConnection conn = new Http1xServerConnection(holder2.context.owner(), sslHelper, options, metrics); if (metrics != null) { holder2.context.executeFromIO(v -> conn.metric(metrics.connected(conn.remoteAddress(), conn.remoteName())));
@Override public synchronized void close(Handler<AsyncResult<Void>> completionHandler) { if (creatingContext != null) { creatingContext.removeCloseHook(this); context.runOnContext(v -> { done.handle(Future.succeededFuture()); });
public NetServerImpl(VertxInternal vertx, NetServerOptions options) { this.vertx = vertx; this.options = new NetServerOptions(options); this.sslHelper = new SSLHelper(options, options.getKeyCertOptions(), options.getTrustOptions()); this.creatingContext = vertx.getContext(); this.logEnabled = options.getLogActivity(); if (creatingContext != null) { if (creatingContext.isMultiThreadedWorkerContext()) { throw new IllegalStateException("Cannot use NetServer in a multi-threaded worker verticle"); } creatingContext.addCloseHook(this); } }
public NetServerImpl(VertxInternal vertx, NetServerOptions options) { this.vertx = vertx; this.options = new NetServerOptions(options); this.sslHelper = new SSLHelper(options, options.getKeyCertOptions(), options.getTrustOptions()); this.creatingContext = vertx.getContext(); this.logEnabled = options.getLogActivity(); if (creatingContext != null) { creatingContext.addCloseHook(this); } }
public VertxServer start(Handler<AsyncResult<Void>> completionHandler) { if (id.port > 0) { actual = map.computeIfAbsent(id, id -> new ActualServer(context.owner(), id, options, builder)); } else { actual = new ActualServer(context.owner(), id, options, builder); } actual.start(context, ar1 -> { if (ar1.succeeded()) { hook = ar2 -> shutdown(); context.addCloseHook(hook); } completionHandler.handle(ar1); }); return this; }
public void close() { if (!closed) { for (NetSocketImpl sock : socketMap.values()) { sock.close(); } if (creatingContext != null) { creatingContext.removeCloseHook(closeHook); } closed = true; if (metrics != null) { metrics.close(); } } }
VertxHttp2Stream(C conn, Http2Stream stream, boolean writable) { this.conn = conn; this.vertx = conn.vertx(); this.handlerContext = conn.handlerContext; this.stream = stream; this.context = conn.getContext(); this.writable = writable; this.pending = new InboundBuffer<>(context, 5); this.priority = HttpUtils.DEFAULT_STREAM_PRIORITY; pending.drainHandler(v -> { int numBytes = pendingBytes; pendingBytes = 0; conn.handler.consume(stream, numBytes); }); pending.handler(this::handleData); pending.exceptionHandler(context.exceptionHandler()); pending.emptyHandler(v -> { if (trailers != null) { handleEnd(trailers); } }); pending.resume(); }
@Test public void testWorkerExecuteFromIo() throws Exception { AtomicReference<ContextInternal> workerContext = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); vertx.deployVerticle(new AbstractVerticle() { @Override public void start() throws Exception { workerContext.set((ContextInternal) context); latch.countDown(); } }, new DeploymentOptions().setWorker(true)); awaitLatch(latch); workerContext.get().nettyEventLoop().execute(() -> { assertNull(Vertx.currentContext()); workerContext.get().nettyEventLoop().execute(() -> { workerContext.get().executeFromIO(v -> { assertSame(workerContext.get(), Vertx.currentContext()); assertTrue(Context.isOnWorkerThread()); testComplete(); }); }); }); await(); }
AtomicReference<Thread> contextThread = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); context.runOnContext(v -> { contextThread.set(Thread.currentThread()); latch.countDown(); awaitLatch(latch); ServerBootstrap bs = new ServerBootstrap(); bs.group(context.nettyEventLoop()); bs.channelFactory(((VertxInternal)vertx).transport().serverChannelFactory(false)) ; bs.option(ChannelOption.SO_BACKLOG, 100);
private void executeCloseDone(ContextInternal closeContext, Handler<AsyncResult<Void>> done, Exception e) { if (done != null) { Future<Void> fut = e == null ? Future.succeededFuture() : Future.failedFuture(e); closeContext.runOnContext(v -> done.handle(fut)); } }
private void handleConnect(SocketAddress remoteAddress, SocketAddress peerAddress, String serverName, Handler<AsyncResult<Channel>> channelHandler) { VertxInternal vertx = context.owner(); bootstrap.resolver(vertx.nettyAddressResolverGroup()); bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) { initSSL(peerAddress, serverName, ch, channelHandler); } }); ChannelFuture fut = bootstrap.connect(vertx.transport().convert(remoteAddress, false)); fut.addListener(res -> { if (res.isSuccess()) { connected(fut.channel(), channelHandler); } else { channelHandler.handle(io.vertx.core.Future.failedFuture(res.cause())); } }); }