Top Description Methods
io.vertx.core

public Interface Context

Known Direct Subinterfaces
io.vertx.core.impl.ContextInternal
Annotations
@VertxGen
Imports
io.vertx.codegen.annotations.Fluent, .GenIgnore, .Nullable, .VertxGen, io.vertx.core.impl.VertxThread, io.vertx.core.impl.launcher.VertxCommandLauncher, io.vertx.core.json.JsonObject, java.util.List, java.util.concurrent.Callable

The execution context of a io.vertx.core.Handler execution.

When Vert.x provides an event to a handler or calls the start or stop methods of a io.vertx.core.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 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.

Author
Tim Fox

Method Summary

Modifier and TypeMethod and Description
public @Nullable JsonObject

Returns:

the configuration of the deployment or null if not a Verticle deployment
config
()
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 deployment
deploymentID
()

If 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 fluently
exceptionHandler
(@Nullable Handler<Throwable>
the exception handler
handler
)

Set an exception handler called when the context runs an action throwing an uncaught throwable.

public @Nullable Handler<Throwable>

Returns:

the current exception handler of this context
exceptionHandler
()

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<@Nullable T>

Returns:

a future completed when the blocking code is complete
executeBlocking
(Callable<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
)

Safely execute some blocking code.

public default <T> void
executeBlocking(Callable<T> blockingCodeHandler, Handler<AsyncResult<@Nullable T>> resultHandler)

Like executeBlocking(Callable) but using a callback.

public default <T> void
executeBlocking(Callable<T> blockingCodeHandler, boolean ordered, Handler<AsyncResult<@Nullable T>> resultHandler)

Like executeBlocking(Callable, boolean) but using a callback.

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 executeBlocking(Handler, boolean, Handler) with order = true.

public <T> Future<@Nullable T>
executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered)
Deprecated instead use executeBlocking(Callable, boolean)

Same as executeBlocking(Handler, boolean, Handler) but with an handler called when the operation completes

public default <T> Future<@Nullable T>
executeBlocking(Handler<Promise<T>> blockingCodeHandler)
Deprecated instead use executeBlocking(Callable)

Same as executeBlocking(Handler, Handler) but with an handler called when the operation completes

public default <
the type of the result
T
>
Future<@Nullable T>

Returns:

a future completed when the blocking code is complete
executeBlocking
(Callable<T>
handler representing the blocking code to run
blockingCodeHandler
)

Invoke executeBlocking(Callable, boolean) with order = true.

public <
the type of the data
T
>
T

Returns:

the data
get
(Object
the key of the data
key
)

Get some data from the context.

public int

Returns:

the number of instances of the verticle that were deployed in the deployment (if any) related to this context
getInstanceCount
()

public <
the type of the data
T
>
T

Returns:

the data
getLocal
(Object
the key of the data
key
)

Get some local data from the context.

public boolean

Returns:

true if false otherwise
isEventLoopContext
()

Is the current context an event loop context?

public static boolean

Returns:

true if current thread is an event thread, false otherwise
isOnEventLoopThread
()

Is the current thread an event thread?

public static boolean

Returns:

true if current thread is a Vert.x thread, false otherwise
isOnVertxThread
()

Is the current thread a Vert.x thread?

public static boolean

Returns:

true if current thread is a worker thread, false otherwise
isOnWorkerThread
()

Is the current thread a worker thread?

public boolean

Returns:

true if the current context is a worker context, false otherwise
isWorkerContext
()

Is the current context a worker context?

public Vertx

Returns:

The Vertx instance that created the context
owner
()

public default List<String>
processArgs()

The process args

public void
put(Object
the key of the data
key
,
Object
the data
value
)

Put some data in the context.

public void
putLocal(Object
the key of the data
key
,
Object
the data
value
)

Put some local data in the context.

public boolean

Returns:

true if removed successfully, false otherwise
remove
(Object
the key to remove
key
)

Remove some data from the context.

public boolean

Returns:

true if removed successfully, false otherwise
removeLocal
(Object
the 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

Returns:

the context threading model
threadingModel
()

Method Detail

configback to summary
public @Nullable JsonObject config()

References Deprecated

JsonObject is deprecated or references (maybe indirectly) at least one deprecated element.

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.

Returns:@Nullable JsonObject

the configuration of the deployment or null if not a Verticle deployment

deploymentIDback to summary
public String deploymentID()

If the context is associated with a Verticle deployment, this returns the deployment ID of that deployment.

Returns:String

the deployment ID of the deployment or null if not a Verticle deployment

exceptionHandlerback 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, Vertx#currentContext() will return this context.

Parameters
handler:@Nullable Handler<Throwable>

the exception handler

Returns:Context

a reference to this, so the API can be used fluently

Annotations
@Fluent
exceptionHandlerback to summary
public @Nullable Handler<Throwable> exceptionHandler()
Returns:@Nullable Handler<Throwable>

the current exception handler of this context

Annotations
@GenIgnore
executeBlockingback to summary
public <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<@Nullable T>> resultHandler)

Deprecated

instead use executeBlocking(Callable, boolean)

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 into blockingCodeHandler. When the blocking code successfully completes, the handler should call the Promise#complete or Promise#complete(Object) method, or the Promise#fail 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 Context#runOnContext(Handler)

Parameters
<T>
the type of the result
blockingCodeHandler:Handler<Promise<T>>

handler representing the blocking code to run

ordered: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

resultHandler:Handler<AsyncResult<@Nullable T>>

handler that will be called when the blocking code is complete

Annotations
@Deprecated
executeBlockingback to summary
public <T> Future<@Nullable T> executeBlocking(Callable<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.

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)

Parameters
<T>
the type of the result
blockingCodeHandler:Callable<T>

handler representing the blocking code to run

ordered: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

Returns:Future<@Nullable T>

a future completed when the blocking code is complete

Annotations
@GenIgnore:permitted-type
executeBlockingback to summary
public default <T> void executeBlocking(Callable<T> blockingCodeHandler, Handler<AsyncResult<@Nullable T>> resultHandler)

Like executeBlocking(Callable) but using a callback.

Annotations
@GenIgnore:permitted-type
executeBlockingback to summary
public default <T> void executeBlocking(Callable<T> blockingCodeHandler, boolean ordered, Handler<AsyncResult<@Nullable T>> resultHandler)

Like executeBlocking(Callable, boolean) but using a callback.

Annotations
@GenIgnore:permitted-type
executeBlockingback to summary
public default <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, Handler<AsyncResult<@Nullable T>> resultHandler)

Deprecated

instead use executeBlocking(Callable)

Invoke executeBlocking(Handler, boolean, Handler) with order = true.

Parameters
<T>
the type of the result
blockingCodeHandler:Handler<Promise<T>>

handler representing the blocking code to run

resultHandler:Handler<AsyncResult<@Nullable T>>

handler that will be called when the blocking code is complete

Annotations
@Deprecated
executeBlockingback to summary
public <T> Future<@Nullable T> executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered)

Deprecated

instead use executeBlocking(Callable, boolean)

Same as executeBlocking(Handler, boolean, Handler) but with an handler called when the operation completes

Annotations
@Deprecated
executeBlockingback to summary
public default <T> Future<@Nullable T> executeBlocking(Handler<Promise<T>> blockingCodeHandler)

Deprecated

instead use executeBlocking(Callable)

Same as executeBlocking(Handler, Handler) but with an handler called when the operation completes

Annotations
@Deprecated
executeBlockingback to summary
public default <T> Future<@Nullable T> executeBlocking(Callable<T> blockingCodeHandler)

Invoke executeBlocking(Callable, boolean) with order = true.

Parameters
<T>
the type of the result
blockingCodeHandler:Callable<T>

handler representing the blocking code to run

Returns:Future<@Nullable T>

a future completed when the blocking code is complete

Annotations
@GenIgnore:permitted-type
getback to summary
public <T> T get(Object key)

Get some data from the context.

Parameters
<T>
the type of the data
key:Object

the key of the data

Returns:T

the data

getInstanceCountback to summary
public int getInstanceCount()
Returns:int

the number of instances of the verticle that were deployed in the deployment (if any) related to this context

getLocalback to summary
public <T> T getLocal(Object key)

Get some local data from the context.

Parameters
<T>
the type of the data
key:Object

the key of the data

Returns:T

the data

isEventLoopContextback to summary
public boolean isEventLoopContext()

Is the current context an event loop context?

Note

when running blocking code using io.vertx.core.Vertx#executeBlocking(Callable) from a standard (not worker) verticle, the context will still an event loop context and this this#isEventLoopContext() will return true.

Returns:boolean

true if false otherwise

isOnEventLoopThreadback to summary
public static boolean isOnEventLoopThread()

Is the current thread an event thread?

Note

This is not always the same as calling Context#isEventLoopContext. If you are running blocking code from an event loop context, then this will return false but Context#isEventLoopContext will return true.

Returns:boolean

true if current thread is an event thread, false otherwise

isOnVertxThreadback 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

Returns:boolean

true if current thread is a Vert.x thread, false otherwise

isOnWorkerThreadback to summary
public static boolean isOnWorkerThread()

Is the current thread a worker thread?

Note

This is not always the same as calling Context#isWorkerContext. If you are running blocking code from an event loop context, then this will return true but Context#isWorkerContext will return false.

Returns:boolean

true if current thread is a worker thread, false otherwise

isWorkerContextback to summary
public boolean isWorkerContext()

Is the current context a worker context?

Note

when running blocking code using io.vertx.core.Vertx#executeBlocking(Callable) from a standard (not worker) verticle, the context will still an event loop context and this this#isWorkerContext() will return false.

Returns:boolean

true if the current context is a worker context, false otherwise

ownerback to summary
public Vertx owner()
Returns:Vertx

The Vertx instance that created the context

processArgsback to summary
public default List<String> processArgs()

The process args

putback 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

Parameters
key:Object

the key of the data

value:Object

the data

putLocalback 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

Parameters
key:Object

the key of the data

value:Object

the data

removeback to summary
public boolean remove(Object key)

Remove some data from the context.

Parameters
key:Object

the key to remove

Returns:boolean

true if removed successfully, false otherwise

removeLocalback to summary
public boolean removeLocal(Object key)

Remove some local data from the context.

Parameters
key:Object

the key to remove

Returns:boolean

true if removed successfully, false otherwise

runOnContextback 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.

Parameters
action:Handler<Void>

the action to run

threadingModelback to summary
public ThreadingModel threadingModel()
Returns:ThreadingModel

the context threading model