Class Context


  • public class Context
    extends Object
    The execution context of a Handler execution.

    When Vert.x provides an event to a handler or calls the start or stop methods of a Verticle, the execution is associated with a Context.

    Usually a context is an *event-loop context* and is tied to a specific event loop thread. So executions for that context always occur on that exact same event loop thread.

    In the case of worker verticles and running inline blocking code a worker context will be associated with the execution which will use a thread from the worker thread pool.

    When a handler is set by a thread associated with a specific context, the Vert.x will guarantee that when that handler is executed, that execution will be associated with the same context.

    If a handler is set by a thread not associated with a context (i.e. a non Vert.x thread). Then a new context will be created for that handler.

    In other words, a context is propagated.

    This means that when a verticle is deployed, any handlers it sets will be associated with the same context - the context of the verticle.

    This means (in the case of a standard verticle) that the verticle code will always be executed with the exact same thread, so you don't have to worry about multi-threaded acccess to the verticle state and you can code your application as single threaded.

    This class also allows arbitrary data to be put(java.lang.Object, java.lang.Object) and get(java.lang.Object) on the context so it can be shared easily amongst different handlers of, for example, a verticle instance.

    This class also provides runOnContext(java.lang.Runnable) which allows an action to be executed asynchronously using the same context.

    NOTE: This class has been automatically generated from the original non Mutiny-ified interface using Vert.x codegen.

    • Constructor Detail

      • Context

        public Context​(io.vertx.core.Context delegate)
      • Context

        public Context​(Object delegate)
    • Method Detail

      • getDelegate

        public io.vertx.core.Context getDelegate()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • isOnWorkerThread

        public static boolean isOnWorkerThread()
        Returns:
        true if current thread is a worker thread, false otherwise
      • isOnEventLoopThread

        public static boolean isOnEventLoopThread()
        Returns:
        true if current thread is an event thread, false otherwise
      • isOnVertxThread

        public static boolean isOnVertxThread()
        Returns:
        true if current thread is a Vert.x thread, false otherwise
      • runOnContext

        public void runOnContext​(Runnable action)
        Parameters:
        action - the action to run
      • executeBlocking

        public <T> io.smallrye.mutiny.Uni<T> executeBlocking​(io.smallrye.mutiny.Uni<T> blockingCodeHandler,
                                                             boolean ordered)
        Safely execute some blocking code.

        Executes the blocking code in the handler blockingCodeHandler using a thread from the worker pool.

        When the code is complete the handler resultHandler will be called with the result on the original context (e.g. on the original event loop of the caller).

        A Future instance is passed into blockingCodeHandler. When the blocking code successfully completes, the handler should call the Promise.complete(T) or Promise.complete(T) method, or the Promise.fail(java.lang.Throwable) method if it failed.

        The blocking code should block for a reasonable amount of time (i.e no more than a few seconds). Long blocking operations or polling operations (i.e a thread that spin in a loop polling events in a blocking fashion) are precluded.

        When the blocking operation lasts more than the 10 seconds, a message will be printed on the console by the blocked thread checker.

        Long blocking operations should use a dedicated thread managed by the application, which can interact with verticles using the event-bus or runOnContext(java.lang.Runnable)

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        blockingCodeHandler - handler representing the blocking code to run
        ordered - if true then if executeBlocking is called several times on the same context, the executions for that context will be executed serially, not in parallel. if false then they will be no ordering guarantees
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • executeBlockingAndAwait

        public <T> T executeBlockingAndAwait​(io.smallrye.mutiny.Uni<T> blockingCodeHandler,
                                             boolean ordered)
        Blocking variant of io.vertx.mutiny.core.Context#executeBlocking(Consumer>,boolean).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        blockingCodeHandler - handler representing the blocking code to run
        ordered - if true then if executeBlocking is called several times on the same context, the executions for that context will be executed serially, not in parallel. if false then they will be no ordering guarantees
        Returns:
        the T instance produced by the operation.
      • executeBlockingAndForget

        public <T> void executeBlockingAndForget​(io.smallrye.mutiny.Uni<T> blockingCodeHandler,
                                                 boolean ordered)
        Variant of io.vertx.mutiny.core.Context#executeBlocking(Consumer>,boolean) that ignores the result of the operation.

        This method subscribes on the result of io.vertx.mutiny.core.Context#executeBlocking(Consumer>,boolean), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from io.vertx.mutiny.core.Context#executeBlocking(Consumer>,boolean) but you don't need to compose it with other operations.

        Parameters:
        blockingCodeHandler - handler representing the blocking code to run
        ordered - if true then if executeBlocking is called several times on the same context, the executions for that context will be executed serially, not in parallel. if false then they will be no ordering guarantees
      • executeBlocking

        public <T> io.smallrye.mutiny.Uni<T> executeBlocking​(io.smallrye.mutiny.Uni<T> blockingCodeHandler)
        Invoke executeBlocking(io.smallrye.mutiny.Uni<T>, boolean) with order = true.

        Unlike the bare Vert.x variant, this method returns a Uni. Don't forget to subscribe on it to trigger the operation.

        Parameters:
        blockingCodeHandler - handler representing the blocking code to run
        Returns:
        the uni firing the result of the operation when completed, or a failure if the operation failed.
      • executeBlockingAndAwait

        public <T> T executeBlockingAndAwait​(io.smallrye.mutiny.Uni<T> blockingCodeHandler)
        Blocking variant of io.vertx.mutiny.core.Context#executeBlocking(Consumer>).

        This method waits for the completion of the underlying asynchronous operation. If the operation completes successfully, the result is returned, otherwise the failure is thrown (potentially wrapped in a RuntimeException).

        Parameters:
        blockingCodeHandler - handler representing the blocking code to run
        Returns:
        the T instance produced by the operation.
      • executeBlockingAndForget

        public <T> void executeBlockingAndForget​(io.smallrye.mutiny.Uni<T> blockingCodeHandler)
        Variant of io.vertx.mutiny.core.Context#executeBlocking(Consumer>) that ignores the result of the operation.

        This method subscribes on the result of io.vertx.mutiny.core.Context#executeBlocking(Consumer>), but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation from io.vertx.mutiny.core.Context#executeBlocking(Consumer>) but you don't need to compose it with other operations.

        Parameters:
        blockingCodeHandler - handler representing the blocking code to run
      • deploymentID

        public String deploymentID()
        Returns:
        the deployment ID of the deployment or null if not a Verticle deployment
      • config

        public io.vertx.core.json.JsonObject config()
        Returns:
        the configuration of the deployment or null if not a Verticle deployment
      • processArgs

        public List<String> processArgs()
        Returns:
      • isEventLoopContext

        public boolean isEventLoopContext()
        Returns:
        true if false otherwise
      • isWorkerContext

        public boolean isWorkerContext()
        Returns:
        true if the current context is a worker context, false otherwise
      • get

        public <T> T get​(Object key)
        Parameters:
        key - the key of the data
        Returns:
        the data
      • put

        public void put​(Object key,
                        Object value)
        Parameters:
        key - the key of the data
        value - the data
      • remove

        public boolean remove​(Object key)
        Parameters:
        key - the key to remove
        Returns:
        true if removed successfully, false otherwise
      • getLocal

        public <T> T getLocal​(Object key)
        Parameters:
        key - the key of the data
        Returns:
        the data
      • putLocal

        public void putLocal​(Object key,
                             Object value)
        Parameters:
        key - the key of the data
        value - the data
      • removeLocal

        public boolean removeLocal​(Object key)
        Parameters:
        key - the key to remove
        Returns:
        true if removed successfully, false otherwise
      • owner

        public Vertx owner()
        Returns:
        The Vertx instance that created the context
      • getInstanceCount

        public int getInstanceCount()
        Returns:
        the number of instances of the verticle that were deployed in the deployment (if any) related to this context
      • exceptionHandler

        public Context exceptionHandler​(Consumer<Throwable> handler)
        Parameters:
        handler - the exception handler
        Returns:
      • newInstance

        public static Context newInstance​(io.vertx.core.Context arg)