public interface Context
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(io.vertx.core.Handler<java.lang.Void>)
which allows an action to be executed asynchronously using the same context.
Modifier and Type | Method and Description |
---|---|
JsonObject |
config()
If the context is associated with a Verticle deployment, this returns the configuration that was specified when
the verticle was deployed.
|
String |
deploymentID()
If the context is associated with a Verticle deployment, this returns the deployment ID of that deployment.
|
Handler<Throwable> |
exceptionHandler() |
Context |
exceptionHandler(Handler<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. |
default <T> Future<T> |
executeBlocking(Callable<T> blockingCodeHandler)
Invoke
executeBlocking(Callable, boolean) with order = true. |
<T> Future<T> |
executeBlocking(Callable<T> blockingCodeHandler,
boolean ordered)
Safely execute some blocking code.
|
default <T> Future<T> |
executeBlocking(Handler<Promise<T>> blockingCodeHandler)
Deprecated.
instead use
executeBlocking(Callable) |
<T> Future<T> |
executeBlocking(Handler<Promise<T>> blockingCodeHandler,
boolean ordered)
Deprecated.
instead use
executeBlocking(Callable, boolean) |
<T> void |
executeBlocking(Handler<Promise<T>> blockingCodeHandler,
boolean ordered,
Handler<AsyncResult<T>> resultHandler)
Deprecated.
instead use
executeBlocking(Callable, boolean) |
default <T> void |
executeBlocking(Handler<Promise<T>> blockingCodeHandler,
Handler<AsyncResult<T>> resultHandler)
Deprecated.
instead use
executeBlocking(Callable) |
<T> T |
get(Object key)
Get some data from the context.
|
int |
getInstanceCount() |
<T> T |
getLocal(Object key)
Get some local data from the context.
|
boolean |
isEventLoopContext()
Is the current context an event loop context?
|
static boolean |
isOnEventLoopThread()
Is the current thread an event thread?
|
static boolean |
isOnVertxThread()
Is the current thread a Vert.x thread? That's either a worker thread or an event loop thread
|
static boolean |
isOnWorkerThread()
Is the current thread a worker thread?
|
boolean |
isWorkerContext()
Is the current context a worker context?
|
Vertx |
owner() |
default List<String> |
processArgs()
The process args
|
void |
put(Object key,
Object value)
Put some data in the context.
|
void |
putLocal(Object key,
Object value)
Put some local data in the context.
|
boolean |
remove(Object key)
Remove some data from the context.
|
boolean |
removeLocal(Object key)
Remove some local data from the context.
|
void |
runOnContext(Handler<Void> action)
Run the specified action asynchronously on the same context, some time after the current execution has completed.
|
static boolean isOnWorkerThread()
NOTE! This is not always the same as calling isWorkerContext()
. If you are running blocking code
from an event loop context, then this will return true but isWorkerContext()
will return false.
static boolean isOnEventLoopThread()
NOTE! This is not always the same as calling isEventLoopContext()
. If you are running blocking code
from an event loop context, then this will return false but isEventLoopContext()
will return true.
static boolean isOnVertxThread()
void runOnContext(Handler<Void> action)
action
- the action to run@Deprecated <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<T>> resultHandler)
executeBlocking(Callable, boolean)
Executes the blocking code in the handler blockingCodeHandler
using a thread from the worker pool.
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 Future
instance is passed into blockingCodeHandler
. When the blocking code successfully completes,
the handler should call the Promise.complete(T)
or Promise.complete(Object)
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(Handler)
T
- the type of the resultblockingCodeHandler
- handler representing the blocking code to runresultHandler
- handler that will be called when the blocking code is completeordered
- 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<T> Future<T> executeBlocking(Callable<T> blockingCodeHandler, boolean ordered)
Executes the blocking code in the handler blockingCodeHandler
using a thread from the worker pool.
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 runOnContext(Handler)
T
- the type of the resultblockingCodeHandler
- handler representing the blocking code to runordered
- 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@Deprecated default <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, Handler<AsyncResult<T>> resultHandler)
executeBlocking(Callable)
executeBlocking(Handler, boolean, Handler)
with order = true.T
- the type of the resultblockingCodeHandler
- handler representing the blocking code to runresultHandler
- handler that will be called when the blocking code is complete@Deprecated <T> Future<T> executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered)
executeBlocking(Callable, boolean)
executeBlocking(Handler, boolean, Handler)
but with an handler
called when the operation completes@Deprecated default <T> Future<T> executeBlocking(Handler<Promise<T>> blockingCodeHandler)
executeBlocking(Callable)
executeBlocking(Handler, Handler)
but with an handler
called when the operation completesdefault <T> Future<T> executeBlocking(Callable<T> blockingCodeHandler)
executeBlocking(Callable, boolean)
with order = true.T
- the type of the resultblockingCodeHandler
- handler representing the blocking code to runString deploymentID()
JsonObject config()
boolean isEventLoopContext()
NOTE! when running blocking code using Vertx.executeBlocking(Callable)
from a
standard (not worker) verticle, the context will still an event loop context and this this#isEventLoopContext()
will return true.
boolean isWorkerContext()
NOTE! when running blocking code using Vertx.executeBlocking(Callable)
from a
standard (not worker) verticle, the context will still an event loop context and this this#isWorkerContext()
will return false.
<T> T get(Object key)
T
- the type of the datakey
- the key of the datavoid put(Object key, Object value)
This can be used to share data between different handlers that share a context
key
- the key of the datavalue
- the databoolean remove(Object key)
key
- the key to remove<T> T getLocal(Object key)
T
- the type of the datakey
- the key of the datavoid putLocal(Object key, Object value)
This can be used to share data between different handlers that share a context
key
- the key of the datavalue
- the databoolean removeLocal(Object key)
key
- the key to removeVertx owner()
int getInstanceCount()
Context exceptionHandler(Handler<Throwable> handler)
Vertx.currentContext()
will return this context.handler
- the exception handlerCopyright © 2024 Eclipse. All rights reserved.