Java Code Examples for io.vertx.core.Context#isOnEventLoopThread()

The following examples show how to use io.vertx.core.Context#isOnEventLoopThread() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VertxCoreRecorder.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public IOThreadDetector detector() {
    return new IOThreadDetector() {
        @Override
        public boolean isInIOThread() {
            return Context.isOnEventLoopThread();
        }
    };
}
 
Example 2
Source File: HystrixTest.java    From vertx-circuit-breaker with Apache License 2.0 5 votes vote down vote up
private void checkSetter(AtomicReference<String> ref, String value) {
  if (Context.isOnEventLoopThread()) {
    ref.set(value);
  } else {
    ref.set("Not on the event loop");
  }
}
 
Example 3
Source File: HonoConnectionImpl.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public final void shutdown() {
    // we don't want to block any event loop thread (even if it's different from the one of the 'context' variable)
    // therefore the latch used for blocking is not used in that case
    final CountDownLatch latch = Context.isOnEventLoopThread() ? null : new CountDownLatch(1);

    shutdown(done -> {
        if (!done.succeeded()) {
            log.warn("could not close connection to server", done.cause());
        }
        if (latch != null) {
            latch.countDown();
        }
    });
    if (latch != null) {
        try {
            // use a timeout slightly higher than the one used in closeConnection()
            final int timeout = getCloseConnectionTimeout() + 20;
            if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
                log.warn("shutdown of client timed out after {}ms", timeout);
            }
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
 
Example 4
Source File: HonoConnectionImpl.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public final void disconnect() {
    // we don't want to block any event loop thread (even if it's different from the one of the 'context' variable)
    // therefore the latch used for blocking is not used in that case
    final CountDownLatch latch = Context.isOnEventLoopThread() ? null : new CountDownLatch(1);

    disconnect(disconnectResult -> {
        if (disconnectResult.succeeded()) {
            log.info("successfully disconnected from the server [{}:{}]", connectionFactory.getHost(), connectionFactory.getPort());
        } else {
            log.warn("could not disconnect from the server [{}:{}]", connectionFactory.getHost(), connectionFactory.getPort());
        }
        if (latch != null) {
            latch.countDown();
        }
    });
    if (latch != null) {
        try {
            // use a timeout slightly higher than the one used in closeConnection()
            final int timeout = getCloseConnectionTimeout() + 20;
            if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
                log.warn("Disconnecting from server [{}:{}, role: {}] timed out after {}ms",
                        connectionFactory.getHost(),
                        connectionFactory.getPort(),
                        connectionFactory.getServerRole(),
                        timeout);
            }
        } catch (final InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}
 
Example 5
Source File: VertxInputStream.java    From quarkus with Apache License 2.0 4 votes vote down vote up
protected ByteBuf readBlocking() throws IOException {
    long expire = System.currentTimeMillis() + timeout;
    synchronized (request.connection()) {
        while (input1 == null && !eof && readException == null) {
            long rem = expire - System.currentTimeMillis();
            if (rem <= 0) {
                //everything is broken, if read has timed out we can assume that the underling connection
                //is wrecked, so just close it
                request.connection().close();
                IOException throwable = new IOException("Read timed out");
                readException = throwable;
                throw throwable;
            }

            try {
                if (Context.isOnEventLoopThread()) {
                    throw new IOException("Attempting a blocking read on io thread");
                }
                waiting = true;
                request.connection().wait(rem);
            } catch (InterruptedException e) {
                throw new InterruptedIOException(e.getMessage());
            } finally {
                waiting = false;
            }
        }
        if (readException != null) {
            throw new IOException(readException);
        }
        Buffer ret = input1;
        input1 = null;
        if (inputOverflow != null) {
            input1 = inputOverflow.poll();
            if (input1 == null) {
                request.fetch(1);
            }
        } else if (!eof) {
            request.fetch(1);
        }
        return ret == null ? null : ret.getByteBuf();
    }
}
 
Example 6
Source File: VertxHttpRecorder.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public Handler<RoutingContext> createBodyHandler(HttpConfiguration httpConfiguration) {
    BodyHandler bodyHandler = BodyHandler.create();
    Optional<MemorySize> maxBodySize = httpConfiguration.limits.maxBodySize;
    if (maxBodySize.isPresent()) {
        bodyHandler.setBodyLimit(maxBodySize.get().asLongValue());
    }
    final BodyConfig bodyConfig = httpConfiguration.body;
    bodyHandler.setHandleFileUploads(bodyConfig.handleFileUploads);
    bodyHandler.setUploadsDirectory(bodyConfig.uploadsDirectory);
    bodyHandler.setDeleteUploadedFilesOnEnd(bodyConfig.deleteUploadedFilesOnEnd);
    bodyHandler.setMergeFormAttributes(bodyConfig.mergeFormAttributes);
    bodyHandler.setPreallocateBodyBuffer(bodyConfig.preallocateBodyBuffer);
    return new Handler<RoutingContext>() {
        @Override
        public void handle(RoutingContext event) {
            if (!Context.isOnEventLoopThread()) {
                ((ConnectionBase) event.request().connection()).channel().eventLoop().execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //this can happen if blocking authentication is involved for get requests
                            if (!event.request().isEnded()) {
                                event.request().resume();
                                if (CAN_HAVE_BODY.contains(event.request().method())) {
                                    bodyHandler.handle(event);
                                } else {
                                    event.next();
                                }
                            } else {
                                event.next();
                            }
                        } catch (Throwable t) {
                            event.fail(t);
                        }
                    }
                });
            } else {
                event.request().resume();
                if (CAN_HAVE_BODY.contains(event.request().method())) {
                    bodyHandler.handle(event);
                } else {
                    event.next();
                }
            }
        }
    };
}