Class Context
- All Implemented Interfaces:
MutinyDelegate
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 multithreaded 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.
- Author:
- Tim Fox
- See Also:
-
Context
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionio.vertx.core.json.JsonObjectconfig()If the context is associated with a Verticle deployment, this returns the configuration that was specified when the verticle was deployed.If the context is associated with a Verticle deployment, this returns the deployment ID of that deployment.booleanexceptionHandler(Consumer<Throwable> handler) Set an exception handler called when the context runs an action throwing an uncaught throwable.<T> io.smallrye.mutiny.Uni<T>executeBlocking(Callable<T> blockingCodeHandler) InvokeexecuteBlocking(Callable, boolean)with order = true.<T> io.smallrye.mutiny.Uni<T>executeBlocking(Callable<T> blockingCodeHandler, boolean ordered) Safely execute some blocking code.<T> TexecuteBlockingAndAwait(Callable<T> blockingCodeHandler) InvokeexecuteBlocking(Callable, boolean)with order = true.<T> TexecuteBlockingAndAwait(Callable<T> blockingCodeHandler, boolean ordered) Safely execute some blocking code.<T> ContextexecuteBlockingAndForget(Callable<T> blockingCodeHandler) InvokeexecuteBlocking(Callable, boolean)with order = true.<T> ContextexecuteBlockingAndForget(Callable<T> blockingCodeHandler, boolean ordered) Safely execute some blocking code.<T> TGet some data from the context.io.vertx.core.ContextGet the delegate instance.int<T> TgetLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key) Get local data associated withkeyusing the concurrent access mode.<T> TgetLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key, io.vertx.core.spi.context.storage.AccessMode accessMode) Get local data associated withkeyusing the specified access mode.inthashCode()booleanIs the current context an event loop context?static booleanIs the current thread an event thread?static booleanIs the current thread a Vert.x thread?static booleanIs the current thread a worker thread?booleanIs the current context a worker context?static ContextnewInstance(io.vertx.core.Context delegate) Creates a new instance of theContext.owner()Deprecated.As of version 5, Vert.x is no longer tightly coupled to the CLIvoidPut some data in the context.<T> voidputLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key, io.vertx.core.spi.context.storage.AccessMode accessMode, T value) Associate local data withkeyusing the specified access mode.<T> voidputLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key, T value) Associate local data withkeyusing the concurrent access mode.booleanRemove some data from the context.<T> voidremoveLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key) Remove local data associated withkeyusing the concurrent access mode.<T> voidremoveLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key, io.vertx.core.spi.context.storage.AccessMode accessMode) Remove local data associated withkeyusing the specified access mode.voidrunOnContext(Runnable action) Run the specified action asynchronously on the same context, some time after the current execution has completed.io.vertx.core.ThreadingModeltoString()
-
Field Details
-
__TYPE_ARG
-
-
Constructor Details
-
Context
public Context(io.vertx.core.Context delegate) Create a new instance ofContextdelegating to the given (non-null) instance ofContext. -
Context
-
-
Method Details
-
getDelegate
public io.vertx.core.Context getDelegate()Get the delegate instance.This method returns the instance on which this shim is delegating the calls. And so, give you access to the bare API.
- Specified by:
getDelegatein interfaceMutinyDelegate- Returns:
- the delegate instance
-
executeBlocking
@CheckReturnValue public <T> io.smallrye.mutiny.Uni<T> executeBlocking(Callable<T> blockingCodeHandler, boolean ordered) Safely execute some blocking code.Executes the blocking code in the handler
blockingCodeHandlerusing a thread from the worker pool.The returned uni 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
Context#runOnContext(Handler)Unlike the bare Vert.x variant, this method returns a
Uni. The uni emits the result of the operation as item. If the operation fails, the uni emits the failure.Don't forget to subscribe on it to trigger the operation.
- Type Parameters:
T- the type of the result- Parameters:
blockingCodeHandler- 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- Returns:
- A
Unirepresenting the asynchronous result of this operation. - See Also:
-
Context.executeBlocking(Callable, boolean)
-
executeBlockingAndAwait
Safely execute some blocking code.Executes the blocking code in the handler
blockingCodeHandlerusing a thread from the worker pool.The returned underlying uni 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
Context#runOnContext(Handler)Unlike the bare Vert.x variant, this method returns a
Context. This method awaits indefinitely 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 aRuntimeException).- Type Parameters:
T- the type of the result- Parameters:
blockingCodeHandler- 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- Returns:
- The operation result
- See Also:
-
Context.executeBlocking(Callable, boolean)
-
executeBlockingAndForget
Safely execute some blocking code.Executes the blocking code in the handler
blockingCodeHandlerusing a thread from the worker pool.The returned underlying uni 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
Context#runOnContext(Handler)Unlike the bare Vert.x variant, this method ignores the
Contextresult or any failure.- Type Parameters:
T- the type of the result- Parameters:
blockingCodeHandler- 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- Returns:
- The current instance to chain operations if needed.
- See Also:
-
Context.executeBlocking(Callable, boolean)
-
executeBlocking
@CheckReturnValue public <T> io.smallrye.mutiny.Uni<T> executeBlocking(Callable<T> blockingCodeHandler) InvokeexecuteBlocking(Callable, boolean)with order = true.Unlike the bare Vert.x variant, this method returns a
Uni. The uni emits the result of the operation as item. If the operation fails, the uni emits the failure.Don't forget to subscribe on it to trigger the operation.
- Type Parameters:
T- the type of the result- Parameters:
blockingCodeHandler- handler representing the blocking code to run- Returns:
- A
Unirepresenting the asynchronous result of this operation. - See Also:
-
Context.executeBlocking(Callable)
-
executeBlockingAndAwait
InvokeexecuteBlocking(Callable, boolean)with order = true.Unlike the bare Vert.x variant, this method returns a
Context. This method awaits indefinitely 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 aRuntimeException).- Type Parameters:
T- the type of the result- Parameters:
blockingCodeHandler- handler representing the blocking code to run- Returns:
- The operation result
- See Also:
-
Context.executeBlocking(Callable)
-
executeBlockingAndForget
InvokeexecuteBlocking(Callable, boolean)with order = true.Unlike the bare Vert.x variant, this method ignores the
Contextresult or any failure.- Type Parameters:
T- the type of the result- Parameters:
blockingCodeHandler- handler representing the blocking code to run- Returns:
- The current instance to chain operations if needed.
- See Also:
-
Context.executeBlocking(Callable)
-
isOnWorkerThread
public static boolean isOnWorkerThread()Is the current thread a worker thread?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 butisWorkerContext()will return false.- Returns:
- true if current thread is a worker thread, false otherwise
-
isOnEventLoopThread
public static boolean isOnEventLoopThread()Is the current thread an event thread?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 butisEventLoopContext()will return true.- Returns:
- true if current thread is an event thread, false otherwise
-
isOnVertxThread
public 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
-
runOnContext
Run the specified action asynchronously on the same context, some time after the current execution has completed.- Parameters:
action- the action to run
-
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
-
config
public io.vertx.core.json.JsonObject 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
-
processArgs
Deprecated.As of version 5, Vert.x is no longer tightly coupled to the CLI- Returns:
- an empty list
-
isEventLoopContext
public boolean isEventLoopContext()Is the current context an event loop context?NOTE! when running blocking code using
Vertx.executeBlocking(Callable)from a standard (not worker) verticle, the context will still an event loop context and thisisEventLoopContext()will return true.- Returns:
trueif the current context is an event-loop context,falseotherwise
-
isWorkerContext
public boolean isWorkerContext()Is the current context a worker context?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 {@link this#isWorkerContext()} will return false.- Returns:
trueif the current context is a worker context,falseotherwise
-
threadingModel
public io.vertx.core.ThreadingModel threadingModel()- Returns:
- the context threading model
-
get
Get some data from the context.- Type Parameters:
T- the type of the data- Parameters:
key- the key of the data- Returns:
- the data
-
put
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 datavalue- the data
-
remove
Remove some data from the context.- Parameters:
key- the key to remove- Returns:
- true if removed successfully, false otherwise
-
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
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.- Parameters:
handler- the exception handler. Can benull.- Returns:
- a reference to this, so the API can be used fluently
-
getLocal
public <T> T getLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key) Get local data associated withkeyusing the concurrent access mode.- Type Parameters:
T- the type of the data- Parameters:
key- the key of the data- Returns:
- the local data
-
putLocal
public <T> void putLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key, T value) Associate local data with
keyusing the concurrent access mode.- Parameters:
key- the key of the datavalue- the data
-
removeLocal
public <T> void removeLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key) Remove local data associated with
keyusing the concurrent access mode.- Parameters:
key- the key to be removed
-
getLocal
public <T> T getLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key, io.vertx.core.spi.context.storage.AccessMode accessMode) Get local data associated with
keyusing the specified access mode.- Type Parameters:
T- the type of the data- Parameters:
key- the key of the dataaccessMode- the access mode- Returns:
- the local data
-
putLocal
public <T> void putLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key, io.vertx.core.spi.context.storage.AccessMode accessMode, T value) Associate local data with
keyusing the specified access mode.- Parameters:
key- the key of the dataaccessMode- the access modevalue- the data
-
removeLocal
public <T> void removeLocal(io.vertx.core.spi.context.storage.ContextLocal<T> key, io.vertx.core.spi.context.storage.AccessMode accessMode) Remove local data associated with
keyusing the specified access mode.- Parameters:
key- the key to be removedaccessMode- the access mode
-
newInstance
Creates a new instance of theContext. -
hashCode
public int hashCode() -
equals
-
toString
-