Class Context
- java.lang.Object
-
- io.vertx.mutiny.core.Context
-
public class Context extends Object
The execution context of aHandler
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 aContext
.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)
andget(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
NOTE: This class has been automatically generated from therunOnContext(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 Modifier and Type Field Description static TypeArg<Context>
__TYPE_ARG
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description io.vertx.core.json.JsonObject
config()
String
deploymentID()
boolean
equals(Object o)
Context
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.instead useexecuteBlocking(Callable)
<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
get(Object key)
io.vertx.core.Context
getDelegate()
int
getInstanceCount()
<T> T
getLocal(Object key)
int
hashCode()
boolean
isEventLoopContext()
static boolean
isOnEventLoopThread()
static boolean
isOnVertxThread()
static boolean
isOnWorkerThread()
boolean
isWorkerContext()
static Context
newInstance(io.vertx.core.Context arg)
Vertx
owner()
List<String>
processArgs()
void
put(Object key, Object value)
void
putLocal(Object key, Object value)
boolean
remove(Object key)
boolean
removeLocal(Object key)
void
runOnContext(Runnable action)
io.vertx.core.ThreadingModel
threadingModel()
String
toString()
-
-
-
Constructor Detail
-
Context
public Context(io.vertx.core.Context delegate)
-
Context
public Context(Object delegate)
-
-
Method Detail
-
getDelegate
public io.vertx.core.Context getDelegate()
-
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
public void runOnContext(Runnable action)
- 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 ofio.vertx.mutiny.core.Context#executeBlocking(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 ofio.vertx.mutiny.core.Context#executeBlocking(Consumer,boolean)
that ignores the result of the operation.This method subscribes on the result of
io.vertx.mutiny.core.Context#executeBlocking(Consumer,boolean)
, but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation fromio.vertx.mutiny.core.Context#executeBlocking(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 public <T> T executeBlockingAndAwait(io.smallrye.mutiny.Uni<T> blockingCodeHandler)
Deprecated.instead useexecuteBlocking(Callable)
Blocking variant ofio.vertx.mutiny.core.Context#executeBlocking(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 public <T> void executeBlockingAndForget(io.smallrye.mutiny.Uni<T> blockingCodeHandler)
Deprecated.instead useexecuteBlocking(Callable)
Variant ofio.vertx.mutiny.core.Context#executeBlocking(Consumer)
that ignores the result of the operation.This method subscribes on the result of
io.vertx.mutiny.core.Context#executeBlocking(Consumer)
, but discards the outcome (item or failure). This method is useful to trigger the asynchronous operation fromio.vertx.mutiny.core.Context#executeBlocking(Consumer)
but you don't need to compose it with other operations.- Parameters:
blockingCodeHandler
- handler representing the blocking code to run
-
deploymentID
public String 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
-
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
public <T> T get(Object key)
- Parameters:
key
- the key of the data- Returns:
- the data
-
put
public void put(Object key, Object value)
- Parameters:
key
- the key of the datavalue
- the data
-
remove
public boolean remove(Object key)
- Parameters:
key
- the key to remove- Returns:
- true if removed successfully, false otherwise
-
getLocal
public <T> T getLocal(Object key)
- Parameters:
key
- the key of the data- Returns:
- the data
-
putLocal
public void putLocal(Object key, Object value)
- Parameters:
key
- the key of the datavalue
- the data
-
removeLocal
public boolean removeLocal(Object key)
- Parameters:
key
- the key to remove- Returns:
- true if removed successfully, false otherwise
-
owner
public Vertx 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
public Context exceptionHandler(Consumer<Throwable> handler)
- 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
public <T> T executeBlockingAndAwait(Callable<T> blockingCodeHandler)
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
public <T> void executeBlockingAndForget(Callable<T> blockingCodeHandler)
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
public <T> T executeBlockingAndAwait(Callable<T> blockingCodeHandler, boolean ordered)
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
public <T> void executeBlockingAndForget(Callable<T> blockingCodeHandler, boolean ordered)
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
public static Context newInstance(io.vertx.core.Context arg)
-
-