io.vertx.core.Handler execution.
When Vert.x provides an event to a handler or calls the start or stop methods of a io.,
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 and get on the context so it can be shared easily
amongst different handlers of, for example, a verticle instance.
This class also provides runOnContext which allows an action to be executed asynchronously using the same context.
| Modifier and Type | Method and Description |
|---|---|
| public @Nullable JsonObject | Returns: the configuration of the deployment or null if not a Verticle deployment
References Deprecated
JsonObject is deprecated or references (maybe indirectly) at least one deprecated element.
If the context is associated with a Verticle deployment, this returns the configuration that was specified when the verticle was deployed. |
| public String | Returns: the deployment ID of the deployment or null if not a Verticle deploymentIf the context is associated with a Verticle deployment, this returns the deployment ID of that deployment. |
| public Context | Returns: a reference to this, so the API can be used fluentlythe exception handler handler)Set an exception handler called when the context runs an action throwing an uncaught throwable. |
| public @Nullable Handler | |
| public < the type of the result T> void | executeBlocking(Handler<Promise<T>>
handler representing the blocking code to run blockingCodeHandler, boolean 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 ordered, Handler<AsyncResult<@Nullable T>> handler that will be called when the blocking code is complete resultHandler)
Deprecated
instead use
executeBlocking(Callable, boolean)
Safely execute some blocking code. |
| public < the type of the result T> Future | Returns: a future completed when the blocking code is completehandler representing the blocking code to run blockingCodeHandler, boolean 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 ordered)Safely execute some blocking code. |
| public default <T> void | executeBlocking(Callable<T> blockingCodeHandler, Handler<AsyncResult<@Nullable T>> resultHandler)
Like |
| public default <T> void | executeBlocking(Callable<T> blockingCodeHandler, boolean ordered, Handler<AsyncResult<@Nullable T>> resultHandler)
Like |
| public default < the type of the result T> void | executeBlocking(Handler<Promise<T>>
handler representing the blocking code to run blockingCodeHandler, Handler<AsyncResult<@Nullable T>> handler that will be called when the blocking code is complete resultHandler)
Deprecated
instead use
executeBlocking(Callable)
Invoke |
| public <T> Future | executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered)
Deprecated
instead use
executeBlocking(Callable, boolean)
Same as |
| public default <T> Future | executeBlocking(Handler<Promise<T>> blockingCodeHandler)
Deprecated
instead use
executeBlocking(Callable)
Same as |
| public default < the type of the result T> Future | Returns: a future completed when the blocking code is completehandler representing the blocking code to run blockingCodeHandler)Invoke |
| public < the type of the data T> T | |
| public int | Returns: the number of instances of the verticle that were deployed in the deployment (if any) related to this context
|
| public < the type of the data T> T | |
| public boolean | |
| public static boolean | Returns: true if current thread is an event thread, false otherwiseIs the current thread an event thread? |
| public static boolean | Returns: true if current thread is a Vert.x thread, false otherwiseIs the current thread a Vert.x thread? |
| public static boolean | Returns: true if current thread is a worker thread, false otherwiseIs the current thread a worker thread? |
| public boolean | Returns: true if the current context is a worker context, false otherwiseIs the current context a worker context? |
| public Vertx | |
| public default List | |
| public void | |
| public void | |
| public boolean | |
| public boolean | Returns: true if removed successfully, false otherwisethe key to remove key)Remove some local data from the context. |
| public void | runOnContext(Handler<Void>
the action to run action)Run the specified action asynchronously on the same context, some time after the current execution has completed. |
| public ThreadingModel |
| config | back to summary |
|---|---|
| public @Nullable JsonObject config()
References Deprecated
See corresponding docs for further information. If the context is associated with a Verticle deployment, this returns the configuration that was specified when the verticle was deployed.
| |
| deploymentID | back to summary |
|---|---|
| public String deploymentID() If the context is associated with a Verticle deployment, this returns the deployment ID of that deployment.
| |
| exceptionHandler | back to summary |
|---|---|
| public Context exceptionHandler(@Nullable Handler<Throwable> handler) Set an exception handler called when the context runs an action throwing an uncaught throwable.
When this handler is called, | |
| exceptionHandler | back to summary |
|---|---|
| public @Nullable Handler | |
| executeBlocking | back to summary |
|---|---|
| public <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<@Nullable T>> resultHandler)
Deprecated instead use Safely execute some blocking code.
Executes the blocking code in the handler The returned future will be completed with the result on the original context (i.e. on the original event loop of the caller) or failed when the handler throws an exception.
A 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
| |
| executeBlocking | back to summary |
|---|---|
| public <T> Future Safely execute some blocking code.
Executes the blocking code in the handler The returned future will be completed with the result on the original context (i.e. on the original event loop of the caller) or failed when the handler throws an exception. 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
| |
| executeBlocking | back to summary |
|---|---|
| public default <T> void executeBlocking(Callable<T> blockingCodeHandler, Handler<AsyncResult<@Nullable T>> resultHandler) Like
| |
| executeBlocking | back to summary |
|---|---|
| public default <T> void executeBlocking(Callable<T> blockingCodeHandler, boolean ordered, Handler<AsyncResult<@Nullable T>> resultHandler) Like
| |
| executeBlocking | back to summary |
|---|---|
| public default <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, Handler<AsyncResult<@Nullable T>> resultHandler)
Deprecated instead use Invoke
| |
| executeBlocking | back to summary |
|---|---|
| public <T> Future Deprecated instead use Same as
| |
| executeBlocking | back to summary |
|---|---|
| public default <T> Future Deprecated instead use Same as
| |
| executeBlocking | back to summary |
|---|---|
| public default <T> Future Invoke
| |
| get | back to summary |
|---|---|
| public <T> T get(Object key) Get some data from the context.
| |
| getInstanceCount | back to summary |
|---|---|
public int getInstanceCount()
| |
| getLocal | back to summary |
|---|---|
| public <T> T getLocal(Object key) Get some local data from the context.
| |
| isEventLoopContext | back to summary |
|---|---|
| public boolean isEventLoopContext() Is the current context an event loop context? Note when running blocking code using
| |
| isOnEventLoopThread | back to summary |
|---|---|
| public static boolean isOnEventLoopThread() Is the current thread an event thread? Note This is not always the same as calling
| |
| isOnVertxThread | back to summary |
|---|---|
| public static boolean isOnVertxThread() Is the current thread a Vert.x thread? That's either a worker thread or an event loop thread
| |
| isOnWorkerThread | back to summary |
|---|---|
| public static boolean isOnWorkerThread() Is the current thread a worker thread? Note This is not always the same as calling
| |
| isWorkerContext | back to summary |
|---|---|
| public boolean isWorkerContext() Is the current context a worker context? Note when running blocking code using
| |
| owner | back to summary |
|---|---|
public Vertx owner()
| |
| processArgs | back to summary |
|---|---|
| public default List The process args | |
| put | back to summary |
|---|---|
| public void put(Object key, Object value) Put some data in the context. This can be used to share data between different handlers that share a context | |
| putLocal | back to summary |
|---|---|
| public void putLocal(Object key, Object value) Put some local data in the context. This can be used to share data between different handlers that share a context | |
| remove | back to summary |
|---|---|
| public boolean remove(Object key) Remove some data from the context.
| |
| removeLocal | back to summary |
|---|---|
| public boolean removeLocal(Object key) Remove some local data from the context.
| |
| runOnContext | back to summary |
|---|---|
| public void runOnContext(Handler<Void> action) Run the specified action asynchronously on the same context, some time after the current execution has completed. | |
| threadingModel | back to summary |
|---|---|
public ThreadingModel threadingModel()
| |