public class WorkerContext extends Object
| Modifier and Type | Field and Description |
|---|---|
protected JsonObject |
config |
protected VertxInternal |
owner |
protected VertxTracer<?,?> |
tracer |
| Modifier and Type | Method and Description |
|---|---|
void |
addCloseHook(Closeable hook)
Add a close hook.
|
ContextInternal |
beginDispatch()
Begin the execution of a task on this context.
|
ClassLoader |
classLoader() |
CloseHooks |
closeHooks() |
JsonObject |
config()
If the context is associated with a Verticle deployment, this returns the configuration that was specified when
the verticle was deployed.
|
ConcurrentMap<Object,Object> |
contextData() |
String |
deploymentID()
If the context is associated with a Verticle deployment, this returns the deployment ID of that deployment.
|
void |
dispatch(Handler<Void> handler) |
void |
dispatch(Runnable handler) |
<T> void |
dispatch(T event,
Handler<T> handler)
Dispatch an
event to the handler on this context. |
ContextInternal |
duplicate()
Returns a context sharing with this context
the same concurrency
the same exception handler
the same context data
the same deployment
the same config
the same classloader
|
void |
emit(Handler<Void> task) |
<T> void |
emit(T argument,
Handler<T> task)
Emit the given
argument event to the task and switch on this context if necessary, this also associates the
current thread with the current context so Vertx.currentContext() returns this context. |
void |
endDispatch(ContextInternal previous)
End the execution of a task on this context, see
ContextInternal.beginDispatch() |
Handler<Throwable> |
exceptionHandler() |
Context |
exceptionHandler(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. |
void |
execute(Handler<Void> task) |
void |
execute(Runnable task)
Execute the
task on this context, it will be executed according to the
context concurrency model. |
<T> void |
execute(T argument,
Handler<T> task)
Execute a
task on this context, the task will be executed according to the
context concurrency model. |
<T> Future<T> |
executeBlocking(Handler<Promise<T>> blockingCodeHandler)
Same as
Context.executeBlocking(Handler, Handler) but with an handler called when the operation completes |
<T> Future<T> |
executeBlocking(Handler<Promise<T>> blockingCodeHandler,
boolean ordered)
Same as
Context.executeBlocking(Handler, boolean, Handler) but with an handler called when the operation completes |
<T> void |
executeBlocking(Handler<Promise<T>> blockingCodeHandler,
boolean ordered,
Handler<AsyncResult<T>> resultHandler)
Safely execute some blocking code.
|
<T> void |
executeBlocking(Handler<Promise<T>> blockingCodeHandler,
Handler<AsyncResult<T>> resultHandler)
Invoke
Context.executeBlocking(Handler, boolean, Handler) with order = true. |
<T> Future<T> |
executeBlocking(Handler<Promise<T>> blockingCodeHandler,
TaskQueue queue)
Like
Context.executeBlocking(Handler, boolean) but uses the queue to order the tasks instead
of the internal queue of this context. |
<T> void |
executeBlocking(Handler<Promise<T>> blockingCodeHandler,
TaskQueue queue,
Handler<AsyncResult<T>> resultHandler)
Like
Context.executeBlocking(Handler, boolean, Handler) but uses the queue to order the tasks instead
of the internal queue of this context. |
<T> Future<T> |
executeBlockingInternal(Handler<Promise<T>> action)
Like
ContextInternal.executeBlockingInternal(Handler, Handler) but returns a Future of the asynchronous result |
<T> Future<T> |
executeBlockingInternal(Handler<Promise<T>> action,
boolean ordered)
Like
ContextInternal.executeBlockingInternal(Handler, boolean, Handler) but returns a Future of the asynchronous result |
<T> void |
executeBlockingInternal(Handler<Promise<T>> action,
boolean ordered,
Handler<AsyncResult<T>> resultHandler) |
<T> void |
executeBlockingInternal(Handler<Promise<T>> action,
Handler<AsyncResult<T>> resultHandler)
Execute an internal task on the internal blocking ordered executor.
|
<T> Future<T> |
failedFuture(String message) |
<T> Future<T> |
failedFuture(Throwable failure) |
<T> T |
get(String key)
Get some data from the context.
|
Deployment |
getDeployment() |
int |
getInstanceCount() |
<T> T |
getLocal(String key)
Get some local data from the context.
|
boolean |
isDeployment() |
boolean |
isEventLoopContext()
Is the current context an event loop context?
|
boolean |
isRunningOnContext() |
boolean |
isWorkerContext()
Is the current context a worker context?
|
ConcurrentMap<Object,Object> |
localContextData() |
io.netty.channel.EventLoop |
nettyEventLoop()
Return the Netty EventLoop used by this Context.
|
VertxInternal |
owner() |
List<String> |
processArgs()
The process args
|
<T> PromiseInternal<T> |
promise() |
<T> PromiseInternal<T> |
promise(Handler<AsyncResult<T>> handler) |
void |
put(String key,
Object value)
Put some data in the context.
|
void |
putLocal(String key,
Object value)
Put some local data in the context.
|
boolean |
remove(String key)
Remove some data from the context.
|
void |
removeCloseHook(Closeable hook)
Remove a close hook.
|
boolean |
removeLocal(String key)
Remove some local data from the context.
|
void |
reportException(Throwable t)
Report an exception to this context synchronously.
|
void |
runOnContext(Handler<Void> action)
Run the specified action asynchronously on the same context, some time after the current execution has completed.
|
long |
setPeriodic(long delay,
Handler<Long> handler)
Like
Vertx.setPeriodic(long, Handler) except the periodic timer will fire on this context. |
long |
setTimer(long delay,
Handler<Long> handler)
Like
Vertx.setTimer(long, Handler) except the timer will fire on this context. |
<T> Future<T> |
succeededFuture() |
<T> Future<T> |
succeededFuture(T result) |
VertxTracer |
tracer() |
WorkerPool |
workerPool() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcurrentisOnEventLoopThread, isOnVertxThread, isOnWorkerThreadprotected final VertxInternal owner
protected final VertxTracer<?,?> tracer
protected final JsonObject config
public boolean isEventLoopContext()
Context
NOTE! when running blocking code using Vertx.executeBlocking(Handler, Handler) from a
standard (not worker) verticle, the context will still an event loop context and this this#isEventLoopContext()
will return true.
isEventLoopContext in interface Contextpublic Deployment getDeployment()
nullpublic CloseHooks closeHooks()
public void addCloseHook(Closeable hook)
ContextInternal The hook will be called when the associated resource needs to be released. Hooks are useful
for automatically cleanup resources when a Verticle is undeployed.
hook - the close hookpublic boolean isDeployment()
true when the context is associated with a deploymentpublic void removeCloseHook(Closeable hook)
ContextInternalThis is called when the resource is released explicitly and does not need anymore a managed close.
hook - the close hookpublic String deploymentID()
Contextpublic JsonObject config()
Contextpublic io.netty.channel.EventLoop nettyEventLoop()
ContextInternalpublic VertxInternal owner()
public <T> Future<T> executeBlockingInternal(Handler<Promise<T>> action)
ContextInternalContextInternal.executeBlockingInternal(Handler, Handler) but returns a Future of the asynchronous resultpublic <T> Future<T> executeBlockingInternal(Handler<Promise<T>> action, boolean ordered)
ContextInternalContextInternal.executeBlockingInternal(Handler, boolean, Handler) but returns a Future of the asynchronous resultpublic <T> Future<T> executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered)
ContextContext.executeBlocking(Handler, boolean, Handler) but with an handler called when the operation completespublic <T> Future<T> executeBlocking(Handler<Promise<T>> blockingCodeHandler, TaskQueue queue)
ContextInternalContext.executeBlocking(Handler, boolean) but uses the queue to order the tasks instead
of the internal queue of this context.public VertxTracer tracer()
public ClassLoader classLoader()
public WorkerPool workerPool()
public ConcurrentMap<Object,Object> contextData()
ConcurrentMap used to store context dataContext.get(String),
Context.put(String, Object)public ConcurrentMap<Object,Object> localContextData()
ConcurrentMap used to store local context datapublic void reportException(Throwable t)
ContextInternalThe exception handler will be called when there is one, otherwise the exception will be logged.
t - the exception to reportpublic Context exceptionHandler(Handler<Throwable> handler)
ContextVertx.currentContext() will return this context.handler - the exception handlerpublic Handler<Throwable> exceptionHandler()
public int getInstanceCount()
public final void runOnContext(Handler<Void> action)
Contextaction - the action to runpublic void execute(Runnable task)
ContextInternaltask on this context, it will be executed according to the
context concurrency model.task - the task to executepublic final <T> void execute(T argument,
Handler<T> task)
ContextInternaltask on this context, the task will be executed according to the
context concurrency model.argument - the task argumenttask - the task to executepublic <T> void emit(T argument,
Handler<T> task)
ContextInternalargument event to the task and switch on this context if necessary, this also associates the
current thread with the current context so Vertx.currentContext() returns this context.
execute(v -> dispatch(argument, task))argument - the task argumenttask - the handler to execute with the event argumentpublic final ContextInternal duplicate()
ContextInternalThe duplicate context has its own
public final boolean isRunningOnContext()
isRunningOnContext in interface ContextInternalpublic boolean isWorkerContext()
Context
NOTE! when running blocking code using Vertx.executeBlocking(Handler, Handler) from a
standard (not worker) verticle, the context will still an event loop context and this this#isWorkerContext()
will return false.
isWorkerContext in interface Contextpublic void emit(Handler<Void> task)
emit in interface ContextInternalContextInternal.emit(Object, Handler)public final void execute(Handler<Void> task)
execute in interface ContextInternalContextInternal.execute(Object, Handler)public final void dispatch(Handler<Void> handler)
dispatch in interface ContextInternalContextInternal.dispatch(Object, Handler)public final ContextInternal beginDispatch()
ContextInternalThe task execution is monitored by the blocked thread checker.
This context is thread-local associated during the task execution.
You should not use this API directly, instead you should use ContextInternal.dispatch(Object, Handler)
beginDispatch in interface ContextInternalnull if there is nonepublic final void endDispatch(ContextInternal previous)
ContextInternalContextInternal.beginDispatch()
You should not use this API directly, instead you should use ContextInternal.dispatch(Object, Handler)
endDispatch in interface ContextInternalprevious - the previous context to restore or null if there is nonepublic long setPeriodic(long delay,
Handler<Long> handler)
ContextInternalVertx.setPeriodic(long, Handler) except the periodic timer will fire on this context.setPeriodic in interface ContextInternalpublic long setTimer(long delay,
Handler<Long> handler)
ContextInternalVertx.setTimer(long, Handler) except the timer will fire on this context.setTimer in interface ContextInternalpublic final <T> void dispatch(T event,
Handler<T> handler)
ContextInternalevent to the handler on this context.
The handler is executed directly by the caller thread which must be a VertxThread or a FastThreadLocalThread.
The handler execution is monitored by the blocked thread checker.
This context is thread-local associated during the task execution.
dispatch in interface ContextInternalevent - the event for the handlerhandler - the handler to execute with the eventpublic final void dispatch(Runnable handler)
dispatch in interface ContextInternalContextInternal.dispatch(Handler)public final List<String> processArgs()
ContextprocessArgs in interface Contextpublic final <T> void executeBlockingInternal(Handler<Promise<T>> action, Handler<AsyncResult<T>> resultHandler)
ContextInternalexecuteBlockingInternal in interface ContextInternalpublic <T> void executeBlockingInternal(Handler<Promise<T>> action, boolean ordered, Handler<AsyncResult<T>> resultHandler)
executeBlockingInternal in interface ContextInternalpublic <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, boolean ordered, Handler<AsyncResult<T>> resultHandler)
Context
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 Promise.complete(T) or Promise.complete(Object) method, or the Promise.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 Context.runOnContext(Handler)
executeBlocking in interface ContextT - the type of the resultblockingCodeHandler - 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
guaranteesresultHandler - handler that will be called when the blocking code is completepublic <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, TaskQueue queue, Handler<AsyncResult<T>> resultHandler)
ContextInternalContext.executeBlocking(Handler, boolean, Handler) but uses the queue to order the tasks instead
of the internal queue of this context.executeBlocking in interface ContextInternalpublic final <T> void executeBlocking(Handler<Promise<T>> blockingCodeHandler, Handler<AsyncResult<T>> resultHandler)
ContextContext.executeBlocking(Handler, boolean, Handler) with order = true.executeBlocking in interface ContextT - the type of the resultblockingCodeHandler - handler representing the blocking code to runresultHandler - handler that will be called when the blocking code is completepublic <T> Future<T> executeBlocking(Handler<Promise<T>> blockingCodeHandler)
ContextContext.executeBlocking(Handler, Handler) but with an handler called when the operation completesexecuteBlocking in interface Contextpublic <T> PromiseInternal<T> promise()
promise in interface ContextInternalPromise associated with this contextpublic <T> PromiseInternal<T> promise(Handler<AsyncResult<T>> handler)
promise in interface ContextInternalPromise associated with this context or the handler
if that handler is already an instance of PromiseInternalpublic <T> Future<T> succeededFuture()
succeededFuture in interface ContextInternalFuture associated with this contextpublic <T> Future<T> succeededFuture(T result)
succeededFuture in interface ContextInternalFuture of the result associated with this contextpublic <T> Future<T> failedFuture(Throwable failure)
failedFuture in interface ContextInternalFuture failed with the failure associated with this contextpublic <T> Future<T> failedFuture(String message)
failedFuture in interface ContextInternalFuture failed with the message associated with this contextpublic final <T> T get(String key)
Contextpublic final void put(String key, Object value)
ContextThis can be used to share data between different handlers that share a context
public final boolean remove(String key)
Contextpublic final <T> T getLocal(String key)
Contextpublic final void putLocal(String key, Object value)
ContextThis can be used to share data between different handlers that share a context
public final boolean removeLocal(String key)
ContextremoveLocal in interface Contextkey - the key to removeCopyright © 2021. All rights reserved.