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 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.
original
non Mutiny-ified interface using Vert.x codegen.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionio.vertx.core.json.JsonObject
config()
boolean
exceptionHandler
(Consumer<Throwable> handler) <T> io.smallrye.mutiny.Uni<T>
executeBlocking
(io.smallrye.mutiny.Uni<T> blockingCodeHandler) InvokeexecuteBlocking(io.smallrye.mutiny.Uni<T>, boolean)
with order = true.<T> io.smallrye.mutiny.Uni<T>
executeBlocking
(io.smallrye.mutiny.Uni<T> blockingCodeHandler, boolean ordered) Safely execute some blocking code.<T> io.smallrye.mutiny.Uni<T>
executeBlocking
(Callable<T> blockingCodeHandler) LikeexecuteBlocking(io.smallrye.mutiny.Uni<T>, boolean)
but using a callback.<T> io.smallrye.mutiny.Uni<T>
executeBlocking
(Callable<T> blockingCodeHandler, boolean ordered) LikeexecuteBlocking(io.smallrye.mutiny.Uni<T>, boolean)
but using a callback.<T> T
executeBlockingAndAwait
(io.smallrye.mutiny.Uni<T> blockingCodeHandler) Deprecated.<T> T
executeBlockingAndAwait
(io.smallrye.mutiny.Uni<T> blockingCodeHandler, boolean ordered) Deprecated.instead useexecuteBlocking(Callable, boolean)
<T> T
executeBlockingAndAwait
(Callable<T> blockingCodeHandler) Blocking variant ofexecuteBlocking(Callable)
.<T> T
executeBlockingAndAwait
(Callable<T> blockingCodeHandler, boolean ordered) Blocking variant ofexecuteBlocking(Callable,boolean)
.<T> void
executeBlockingAndForget
(io.smallrye.mutiny.Uni<T> blockingCodeHandler) Deprecated.instead useexecuteBlocking(Callable)
<T> void
executeBlockingAndForget
(io.smallrye.mutiny.Uni<T> blockingCodeHandler, boolean ordered) Deprecated.instead useexecuteBlocking(Callable, boolean)
<T> void
executeBlockingAndForget
(Callable<T> blockingCodeHandler) Variant ofexecuteBlocking(Callable)
that ignores the result of the operation.<T> void
executeBlockingAndForget
(Callable<T> blockingCodeHandler, boolean ordered) Variant ofexecuteBlocking(Callable,boolean)
that ignores the result of the operation.<T> T
io.vertx.core.Context
int
<T> T
int
hashCode()
boolean
static boolean
static boolean
static boolean
boolean
static Context
newInstance
(io.vertx.core.Context arg) owner()
void
void
boolean
boolean
removeLocal
(Object key) void
runOnContext
(Runnable action) io.vertx.core.ThreadingModel
toString()
-
Field Details
-
__TYPE_ARG
-
-
Constructor Details
-
Context
public Context(io.vertx.core.Context delegate) -
Context
-
-
Method Details
-
getDelegate
public io.vertx.core.Context getDelegate()- Specified by:
getDelegate
in interfaceMutinyDelegate
- Returns:
- the delegate used by this Mutiny object of generated type
-
toString
-
equals
-
hashCode
public int hashCode() -
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
- Parameters:
action
- the action to run
-
executeBlocking
@CheckReturnValue 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.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 intoblockingCodeHandler
. When the blocking code successfully completes, the handler should call thePromise.complete(T)
orPromise.complete(T)
method, or thePromise.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 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
uni
firing the result of the operation when completed, or a failure if the operation failed.
-
executeBlockingAndAwait
@Deprecated public <T> T executeBlockingAndAwait(io.smallrye.mutiny.Uni<T> blockingCodeHandler, boolean ordered) Deprecated.instead useexecuteBlocking(Callable, boolean)
Blocking variant ofexecuteBlocking(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 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 T instance produced by the operation.
-
executeBlockingAndForget
@Deprecated public <T> void executeBlockingAndForget(io.smallrye.mutiny.Uni<T> blockingCodeHandler, boolean ordered) Deprecated.instead useexecuteBlocking(Callable, boolean)
Variant ofexecuteBlocking(Consumer,boolean)
that ignores the result of the operation.This method subscribes on the result of
executeBlocking(Consumer,boolean)
, but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation fromexecuteBlocking(Consumer,boolean)
but you don't need to compose it with other operations.- 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
-
executeBlocking
@CheckReturnValue public <T> io.smallrye.mutiny.Uni<T> executeBlocking(io.smallrye.mutiny.Uni<T> blockingCodeHandler) InvokeexecuteBlocking(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
Deprecated.instead useexecuteBlocking(Callable)
Blocking variant ofexecuteBlocking(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
Deprecated.instead useexecuteBlocking(Callable)
Variant ofexecuteBlocking(Consumer)
that ignores the result of the operation.This method subscribes on the result of
executeBlocking(Consumer)
, but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation fromexecuteBlocking(Consumer)
but you don't need to compose it with other operations.- Parameters:
blockingCodeHandler
- handler representing the blocking code to run
-
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
- 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
-
threadingModel
public io.vertx.core.ThreadingModel threadingModel()- Returns:
- the context threading model
-
get
- Parameters:
key
- the key of the data- Returns:
- the data
-
put
- Parameters:
key
- the key of the datavalue
- the data
-
remove
- Parameters:
key
- the key to remove- Returns:
- true if removed successfully, false otherwise
-
getLocal
- Parameters:
key
- the key of the data- Returns:
- the data
-
putLocal
- Parameters:
key
- the key of the datavalue
- the data
-
removeLocal
- 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
- Parameters:
handler
- the exception handler- Returns:
-
executeBlocking
@CheckReturnValue public <T> io.smallrye.mutiny.Uni<T> executeBlocking(Callable<T> blockingCodeHandler) LikeexecuteBlocking(io.smallrye.mutiny.Uni<T>, boolean)
but using a callback.Unlike the bare Vert.x variant, this method returns a
Uni
. Don't forget to subscribe on it to trigger the operation.- Parameters:
blockingCodeHandler
-- Returns:
- the
uni
firing the result of the operation when completed, or a failure if the operation failed.
-
executeBlockingAndAwait
Blocking variant ofexecuteBlocking(Callable)
.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
-- Returns:
- the T instance produced by the operation.
-
executeBlockingAndForget
Variant ofexecuteBlocking(Callable)
that ignores the result of the operation.This method subscribes on the result of
executeBlocking(Callable)
, but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation fromexecuteBlocking(Callable)
but you don't need to compose it with other operations.- Parameters:
blockingCodeHandler
-
-
executeBlocking
@CheckReturnValue public <T> io.smallrye.mutiny.Uni<T> executeBlocking(Callable<T> blockingCodeHandler, boolean ordered) LikeexecuteBlocking(io.smallrye.mutiny.Uni<T>, boolean)
but using a callback.Unlike the bare Vert.x variant, this method returns a
Uni
. Don't forget to subscribe on it to trigger the operation.- Parameters:
blockingCodeHandler
-ordered
-- Returns:
- the
uni
firing the result of the operation when completed, or a failure if the operation failed.
-
executeBlockingAndAwait
Blocking variant ofexecuteBlocking(Callable,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
-ordered
-- Returns:
- the T instance produced by the operation.
-
executeBlockingAndForget
Variant ofexecuteBlocking(Callable,boolean)
that ignores the result of the operation.This method subscribes on the result of
executeBlocking(Callable,boolean)
, but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation fromexecuteBlocking(Callable,boolean)
but you don't need to compose it with other operations.- Parameters:
blockingCodeHandler
-ordered
-
-
newInstance
-
executeBlocking(Callable)