public class VertxHelperBase extends HelperBase
This class also provides utilities for submitting jobs to be executed in an associated verticle. This is useful for jobs that trigger future callbacks, because the verticle will ensure that all callbacks are called in the same thread.
Moreover, this class provides utilities for ensuring that a requested job
has been completed, including all expected callbacks, before the next job
is initiated. This is useful for ensuring that responses to requests
occur in the same order as the requests themselves. To use this, when you
make a request (e.g. using submit(Runnable)
, the subclass can
call the protected method _setBusy(boolean)
with argument true,
and subsequent requests will simply be queued until the subclass calls
_setBusy(boolean)
with argument false.
This class also provides utilities for a subclass to issue asynchronous
requests in order, and when callbacks occur, to execute those callbacks
in the same order as the submitted requests. To use this, the subclass
should assign consecutive increasing integers, starting with zero, to
each request, and then wrap responses to that request in a Runnable
and pass that Runnable to the protected method
_issueOrDeferResponse(long, boolean, Runnable)
.
That method will execute the Runnable only when all previous
requests (ones with earlier sequence numbers) have had a response
executed that indicated (with the second argument) that the request
has been fully handled.
This facility must be used with care: the subclass must ensure that
every request results in at least one call to
_issueOrDeferResponse(long, boolean, Runnable)
with the second argument being true; failing to do so will result in
all future responses being queued and never executing.
Using a timeout, for example, can ensure this.
Note also that the specified Runnable will execute in the director
thread (via a timeout mechanism with timeout equal to zero), not
in the Verticle. Thus, subclasses that use should not directly invoke
Vert.x functions that need to be run in the verticle in the Runnable
that they pass. They should instead use submit(Runnable)
.
Modifier and Type | Class and Description |
---|---|
class |
VertxHelperBase.AccessorVerticle
Verticle to handle requests.
|
HelperBase.DATA_TYPE
Modifier and Type | Field and Description |
---|---|
protected VertxHelperBase.AccessorVerticle |
_verticle
Verticle supporting this helper.
|
protected static io.vertx.core.Vertx |
_vertx
Global (unclustered) instance of Vert.x core.
|
_actor, _currentObj
Modifier | Constructor and Description |
---|---|
protected |
VertxHelperBase(java.lang.Object actor,
jdk.nashorn.api.scripting.ScriptObjectMirror helping)
Construct a helper for the specified JavaScript actor and
create a verticle that can execute submitted jobs atomically.
|
protected |
VertxHelperBase(java.lang.Object actor,
jdk.nashorn.api.scripting.ScriptObjectMirror helping,
VertxHelperBase helper)
Construct a helper for the specified JavaScript actor and
create a verticle that can execute submitted jobs atomically.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_issueOrDeferResponse(long requestNumber,
boolean done,
java.lang.Runnable response)
Execute the specified response in the same order as the request
that triggered the response.
|
protected void |
_processPendingJob()
If the verticle is not busy, process the next pending job.
|
protected void |
_setBusy(boolean busy)
Specify whether this helper is currently processing a previous
request.
|
static void |
closeVertx()
Stop the global (unclustered instance of Vert.x.
|
static VertxHelperBase |
getHelper(java.lang.Object actor)
Return an instance of this helper for the specified actor, if one
has been created and not garbage collected.
|
static java.util.Set<java.lang.String> |
getImageTypes()
Return a set of informal image type names that can be sent.
|
void |
reset()
Reset this helper.
|
void |
submit(java.lang.Runnable job)
Submit a job to be executed by the associated verticle.
|
static java.lang.String[] |
supportedReceiveTypes()
Return an array of the types supported by the current host for
receiveType arguments, which are the types that can be extracted from buffers.
|
static java.lang.String[] |
supportedSendTypes()
Return an array of the types supported by the current host for
sendType arguments, which are the types that can be written to buffers.
|
void |
undeploy()
Undeploy the associated verticle.
|
_appendNumericToBuffer, _appendToBuffer, _error, _error, _error, _error, _extractFromBuffer, _issueResponse, _removeDuplicates, _sizeOfType, _toJavaBytes, _toJSArray, getHelping
protected VertxHelperBase.AccessorVerticle _verticle
protected static io.vertx.core.Vertx _vertx
protected VertxHelperBase(java.lang.Object actor, jdk.nashorn.api.scripting.ScriptObjectMirror helping)
actor
- The JavaScript actor that this is helping, or
a RestrictedJavaScriptInterface proxy for that actor.helping
- The JavaScript object that this is helping.protected VertxHelperBase(java.lang.Object actor, jdk.nashorn.api.scripting.ScriptObjectMirror helping, VertxHelperBase helper)
actor
- The JavaScript actor that this is helping, or
a RestrictedJavaScriptInterface proxy for that actor.helping
- The JavaScript object that this is helping.helper
- The helper providing the verticle and event
handler, or null to create a new verticle and event handler.public static void closeVertx()
public static VertxHelperBase getHelper(java.lang.Object actor)
submit(Runnable)
method.actor
- Either a JavaScript actor or a RestrictedJavaScriptInterface.public void reset()
public void submit(java.lang.Runnable job)
job
- The job to execute.public static java.util.Set<java.lang.String> getImageTypes()
public static java.lang.String[] supportedReceiveTypes()
public static java.lang.String[] supportedSendTypes()
public void undeploy()
protected void _issueOrDeferResponse(long requestNumber, boolean done, java.lang.Runnable response)
requestNumber
- The number of the request.done
- True to indicate that this request is complete.response
- The response to execute.protected void _processPendingJob()
_setBusy(boolean)
protected void _setBusy(boolean busy)
submit(Runnable)
will defer execution of the
job until this is later called with argument false. If you call
this with argument true, please be sure you later call it with argument
false. The purpose of this method is to ensure that if a job
involves some callbacks, that those callbacks are processed
before the next job is executed. Normally, you would call this
with argument true when requesting a service, and call it with
argument false when the service has been completely provided.
If you do not call this at all, then jobs and callbacks may
be arbitrarily interleaved (though they will all execute in the
same thread).busy
- True to defer jobs, false to stop deferring.