Context

The execution context of a {@link 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\jphp\core\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 @see \io\vertx\jphp\core\Context::put and @see \io\vertx\jphp\core\Context::get on the context so it can be shared easily amongst different handlers of, for example, a verticle instance.

This class also provides @see \io\vertx\jphp\core\Context::runOnContext which allows an action to be executed asynchronously using the same context.

package

Default

Methods

__construct

__construct() 

If the context is associated with a Verticle deployment, this returns the configuration that was specified when the verticle was deployed.

config() : array

Response

array

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

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

deploymentID() : string

Response

string

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

Set an exception handler called when the context runs an action throwing an uncaught throwable.<p/>

exceptionHandler( $arg0) : $this

When this handler is called, @see \io\vertx\jphp\core\Vertx::currentContext will return this context.

Arguments

$arg0

callable

Response

$this

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

Invoke @see \io\vertx\jphp\core\Context::executeBlocking with order = true.

executeBlocking( $arg0,  $arg1,  $arg2 = null) : void

param $blockingCodeHandler [callable] handler representing the blocking code to run param $resultHandler [callable] handler that will be called when the blocking code is complete executeBlocking($blockingCodeHandler, $resultHandler)

Safely execute some blocking code.

Executes the blocking code in the handler blockingCodeHandler using a thread from the worker pool.

When the code is complete the handler resultHandler will be called with the result on the original context (e.g. on the original event loop of the caller).

A Future instance is passed into blockingCodeHandler. When the blocking code successfully completes, the handler should call the @see \io\vertx\jphp\core\Future::complete or @see \io\vertx\jphp\core\Future::complete method, or the @see \io\vertx\jphp\core\Future::fail method if it failed. param $blockingCodeHandler [callable] handler representing the blocking code to run param $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 param $resultHandler [callable] handler that will be called when the blocking code is complete executeBlocking($blockingCodeHandler, $ordered, $resultHandler)

Arguments

$arg0

callable

$arg1

callable | boolean

$arg2

callable

Get some data from the context.

get( $arg0) : mixed

Arguments

$arg0

string

Response

mixed

the data

getInstanceCount

getInstanceCount() : integer

Response

integer

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

Is the current context an event loop context? <p> NOTE! when running blocking code using @see \io\vertx\jphp\core\Vertx::executeBlocking from a standard (not worker) verticle, the context will still an event loop context and this will return true.

isEventLoopContext() : boolean

Response

boolean

true if false otherwise

Is the current context a multi-threaded worker context?

isMultiThreadedWorkerContext() : boolean

Response

boolean

true if the current context is a multi-threaded worker context, false otherwise

Is the current thread an event thread? <p> NOTE! This is not always the same as calling @see \io\vertx\jphp\core\Context::isEventLoopContext. If you are running blocking code from an event loop context, then this will return false but @see \io\vertx\jphp\core\Context::isEventLoopContext will return true.

isOnEventLoopThread() : boolean
static

Response

boolean

true if current thread is a worker thread, false otherwise

Is the current thread a Vert.x thread? That's either a worker thread or an event loop thread

isOnVertxThread() : boolean
static

Response

boolean

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

Is the current thread a worker thread? <p> NOTE! This is not always the same as calling @see \io\vertx\jphp\core\Context::isWorkerContext. If you are running blocking code from an event loop context, then this will return true but @see \io\vertx\jphp\core\Context::isWorkerContext will return false.

isOnWorkerThread() : boolean
static

Response

boolean

true if current thread is a worker thread, false otherwise

Is the current context a worker context? <p> NOTE! when running blocking code using @see \io\vertx\jphp\core\Vertx::executeBlocking from a standard (not worker) verticle, the context will still an event loop context and this will return false.

isWorkerContext() : boolean

Response

boolean

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

owner

owner() : \io\vertx\jphp\core\Vertx

Response

\io\vertx\jphp\core\Vertx

The Vertx instance that created the context

The process args

processArgs() : array

Response

array

Put some data in the context.

put( $arg0,  $arg1) : void

This can be used to share data between different handlers that share a context

Arguments

$arg0

string

$arg1

mixed

Remove some data from the context.

remove( $arg0) : boolean

Arguments

$arg0

string

Response

boolean

true if removed successfully, false otherwise

Run the specified action asynchronously on the same context, some time after the current execution has completed.

runOnContext( $arg0) : void

Arguments

$arg0

callable