Package: io.vertx.groovy.core

## [Groovy] Class Context

- io.vertx.groovy.core.Context

- The execution context of a io.vertx.groovy.core.Handler execution.

  When Vert.x provides an event to a handler or calls the start or stop methods of a io.vertx.groovy.core.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 Context.put and Context.get on the context so it can be shared easily amongst different handlers of, for example, a verticle instance.

  This class also provides Context.runOnContext which allows an action to be executed asynchronously using the same context.

- - ### Constructor Summary

    Constructors
    Constructor and description
    `Context (java.lang.Object delegate)`

  - ### Methods Summary

    Methods
    Type | Name and description
    --- | ---
    `java.util.Map<java.lang.String, java.lang.Object>` | `config()` If the context is associated with a Verticle deployment, this returns the configuration that was specified when the verticle was deployed.
    `java.lang.String` | `deploymentID()` If the context is associated with a Verticle deployment, this returns the deployment ID of that deployment.
    `Context` | `exceptionHandler(io.vertx.core.Handler<java.lang.Throwable> handler)` Set an exception handler called when the context runs an action throwing an uncaught throwable.
    `void` | `executeBlocking(io.vertx.core.Handler<Future<T>> blockingCodeHandler, boolean ordered, io.vertx.core.Handler<io.vertx.core.AsyncResult<T>> resultHandler)` Safely execute some blocking code.
    `void` | `executeBlocking(io.vertx.core.Handler<Future<T>> blockingCodeHandler, io.vertx.core.Handler<io.vertx.core.AsyncResult<T>> resultHandler)` Invoke Context.executeBlocking with order = true.
    `T` | `get(java.lang.String key)` Get some data from the context.
    `java.lang.Object` | `getDelegate()`
    `int` | `getInstanceCount()` @return the number of instances of the verticle that were deployed in the deployment (if any) related to this context
    `boolean` | `isEventLoopContext()` Is the current context an event loop context?
    `boolean` | `isMultiThreadedWorkerContext()` Is the current context a multi-threaded worker context?
    `static boolean` | `isOnEventLoopThread()` Is the current thread an event thread?
    `static boolean` | `isOnVertxThread()` Is the current thread a Vert.x thread?
    `static boolean` | `isOnWorkerThread()` Is the current thread a worker thread?
    `boolean` | `isWorkerContext()` Is the current context a worker context?
    `Vertx` | `owner()` @return The Vertx instance that created the context
    `java.util.List<java.lang.String>` | `processArgs()` The process args @return
    `void` | `put(java.lang.String key, java.lang.Object value)` Put some data in the context.
    `boolean` | `remove(java.lang.String key)` Remove some data from the context.
    `void` | `runOnContext(io.vertx.core.Handler<java.lang.Void> action)` Run the specified action asynchronously on the same context, some time after the current execution has completed.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class java.lang.Object` | `java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()`

- - ### Constructor Detail

    - #### Context(java.lang.Object delegate)

  - ### Method Detail

    - #### java.util.Map<java.lang.String, java.lang.Object> config()

      If the context is associated with a Verticle deployment, this returns the configuration that was specified when the verticle was deployed.

      Returns:
        the configuration of the deployment or null if not a Verticle deployment

    - #### java.lang.String deploymentID()

      If the context is associated with a Verticle deployment, this returns the deployment ID of that deployment.

      Returns:
        the deployment ID of the deployment or null if not a Verticle deployment

    - #### Context exceptionHandler(io.vertx.core.Handler<java.lang.Throwable> handler)

      Set an exception handler called when the context runs an action throwing an uncaught throwable.

      When this handler is called, Vertx.currentContext will return this context.
      Returns:
        a reference to this, so the API can be used fluently
      Parameters:
        `handler` - the exception handler

    - #### void executeBlocking(io.vertx.core.Handler<Future<T>> blockingCodeHandler, boolean ordered, io.vertx.core.Handler<io.vertx.core.AsyncResult<T>> resultHandler)

      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 Future.complete or Future.complete method, or the Future.fail method if it failed.

      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
        `resultHandler` - handler that will be called when the blocking code is complete

    - #### void executeBlocking(io.vertx.core.Handler<Future<T>> blockingCodeHandler, io.vertx.core.Handler<io.vertx.core.AsyncResult<T>> resultHandler)

      Invoke Context.executeBlocking with order = true.

      Parameters:
        `blockingCodeHandler` - handler representing the blocking code to run
        `resultHandler` - handler that will be called when the blocking code is complete

    - #### T get(java.lang.String key)

      Get some data from the context.

      Returns:
        the data
      Parameters:
        `key` - the key of the data

    - #### java.lang.Object getDelegate()

    - #### int getInstanceCount()

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

    - #### boolean isEventLoopContext()

      Is the current context an event loop context?

      NOTE! when running blocking code using Vertx.executeBlocking from a standard (not worker) verticle, the context will still an event loop context and this will return true.

      Returns:
        true if false otherwise

    - #### boolean isMultiThreadedWorkerContext()

      Is the current context a multi-threaded worker context?

      Returns:
        true if the current context is a multi-threaded worker context, false otherwise

    - #### static boolean isOnEventLoopThread()

      Is the current thread an event thread?

      NOTE! This is not always the same as calling Context.isEventLoopContext. If you are running blocking code from an event loop context, then this will return false but Context.isEventLoopContext will return true.

      Returns:
        true if current thread is a worker thread, false otherwise

    - #### static boolean isOnVertxThread()

      Is the current thread a Vert.x thread? That's either a worker thread or an event loop thread

      Returns:
        true if current thread is a Vert.x thread, false otherwise

    - #### static boolean isOnWorkerThread()

      Is the current thread a worker thread?

      NOTE! This is not always the same as calling Context.isWorkerContext. If you are running blocking code from an event loop context, then this will return true but Context.isWorkerContext will return false.

      Returns:
        true if current thread is a worker thread, false otherwise

    - #### boolean isWorkerContext()

      Is the current context a worker context?

      NOTE! when running blocking code using Vertx.executeBlocking from a standard (not worker) verticle, the context will still an event loop context and this will return false.

      Returns:
        true if the current context is a worker context, false otherwise

    - #### Vertx owner()

      Returns:
        The Vertx instance that created the context

    - #### java.util.List<java.lang.String> processArgs()

      The process args

      Returns:

    - #### void put(java.lang.String key, java.lang.Object value)

      Put some data in the context.

      This can be used to share data between different handlers that share a context

      Parameters:
        `key` - the key of the data
        `value` - the data

    - #### boolean remove(java.lang.String key)

      Remove some data from the context.

      Returns:
        true if removed successfully, false otherwise
      Parameters:
        `key` - the key to remove

    - #### void runOnContext(io.vertx.core.Handler<java.lang.Void> action)

      Run the specified action asynchronously on the same context, some time after the current execution has completed.

      Parameters:
        `action` - the action to run