Package dev.getelements.elements.rt
Interface Resource
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
TransactionalResource
A
Resource
is a logical unit of work, which is represented by an instance of this type. It essentially
contains a set of handler methods, which can be invoked using the getMethodDispatcher(String)
method as well
as keep running tasks, as represented by TaskId
.
This interface is meant to be implemented directly or, for clustered configurations, a proxy to a remote
Resource
. Therefore, it is necessary to restrict the methods and return values on this interface to objects
and types which can be serialized (ie plain-old-data), or can be used as proxies themselves.
Created by patricktwohig on 8/8/15.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Default IO Buffer size, 4kb -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes and destroys this Resource.void
Restores the entire state of thisResource
from the suppliedInputStream
.default void
Restores the entire state of thisResource
from the suppliedInputStream
.Gets theAttributes
associated with thisResource
getId()
Returns the immutable and globally-unique ID of this resource.org.slf4j.Logger
Gets theLogger
used by thisResource
.getMethodDispatcher
(String name) Returns an instance ofMethodDispatcher
, which is used to invoke methods against thisResource
.getTasks()
default boolean
Returns true if the resource is set to enable verbose mode.void
Resumes the supplied task with theTaskId
and the list of results.default void
resumeFromNetwork
(TaskId taskId, Object result) Resumes a suspended task, accepting the task id.default void
resumeFromScheduler
(TaskId taskId, double elapsedTime) Resumes a suspended task, accepting the task id.default void
resumeWithError
(TaskId taskId, Throwable throwable) Resumes a suspended task, accepting the task id.void
Dumps the entire contents of thisResource
to the suppliedOutputStream
where it can be reconstituted later using thedeserialize(InputStream)
method.default void
Dumps the entire contents of thisResource
to the suppliedWritableByteChannel
where it can be reconstituted later using thedeserialize(InputStream)
method.default void
setVerbose
(boolean verbose) Sets the verbose mode.void
unload()
Closes and destroys this Resource.
-
Field Details
-
DEFAULT_IO_BUFFER_SIZE
static final int DEFAULT_IO_BUFFER_SIZEDefault IO Buffer size, 4kb- See Also:
-
-
Method Details
-
getId
ResourceId getId()Returns the immutable and globally-unique ID of this resource. Though a resource may exist at any path, this is the resource's ID. All resources are assigned a unique ID upon creation. The ID must remain unique for the life of the resource.- Returns:
- the resource's ID
-
getAttributes
MutableAttributes getAttributes()Gets theAttributes
associated with thisResource
- Returns:
- this instance's
Attributes
-
getMethodDispatcher
Returns an instance ofMethodDispatcher
, which is used to invoke methods against thisResource
. The reurnedMethodDispatcher
will defer actually invoking the method until the final call in the chainResultAcceptor.dispatch(Consumer, Consumer)
.- Parameters:
name
- the name of the method- Returns:
- the
MethodDispatcher
, never null - Throws:
MethodNotFoundException
- if the method cannot be found
-
resume
Resumes the supplied task with theTaskId
and the list of results.- Parameters:
taskId
- theTaskId
of the associated taskresults
- zero or more results supplied to the resumed task
-
resumeFromNetwork
Resumes a suspended task, accepting the task id. The task will be resumed as soon as possible. This is used when the network resumes the call. This is assumed that the resume was successful. The response to the, invocation whatever it may be, was successful.- Parameters:
taskId
- theTaskId
if the running taskresult
- the resulting object from the call
-
resumeWithError
Resumes a suspended task, accepting the task id. The task will be resumed as soon as possible. This is used when suspended coroutine encountered an error. The coroutine will be resume with the error that caused the underlying failure.- Parameters:
taskId
-throwable
-
-
resumeFromScheduler
Resumes a suspended task, accepting the task id. The task will be resumed as soon as possible. This method must succeed at the scheduling process, or else throw an exception. Note that this does not guarantee successful execution of the task, but rather successful scheduling.- Parameters:
taskId
- theTaskId
if the running taskelapsedTime
- the amount of time elapsed since the task was last activated
-
serialize
Dumps the entire contents of thisResource
to the suppliedOutputStream
where it can be reconstituted later using thedeserialize(InputStream)
method.- Parameters:
os
- theOutputStream
used to receive the serializedResource
- Throws:
IOException
- if something failed during serialization
-
deserialize
Restores the entire state of thisResource
from the suppliedInputStream
. This assumes theInputStream
was produced by a call toserialize(OutputStream)
.- Parameters:
is
- theInputStream
from which to read the serialized resource- Throws:
IOException
- if something failed during deserialization
-
serialize
Dumps the entire contents of thisResource
to the suppliedWritableByteChannel
where it can be reconstituted later using thedeserialize(InputStream)
method.- Parameters:
wbc
- theOutputStream
used to receive the serializedResource
- Throws:
IOException
- if something failed during serialization
-
deserialize
Restores the entire state of thisResource
from the suppliedInputStream
. This assumes theInputStream
was produced by a call toserialize(OutputStream)
.- Parameters:
ibc
- theInputStream
from which to read the serialized resource- Throws:
IOException
- if something failed during deserialization
-
setVerbose
default void setVerbose(boolean verbose) Sets the verbose mode. This will enable enhanced logging for debug purposes.- Parameters:
verbose
- true if verbose, false otherwise
-
isVerbose
default boolean isVerbose()Returns true if the resource is set to enable verbose mode.- Returns:
- true if verbose mode is enabled.
-
getTasks
- Returns:
- the running tasks as a
Set
-
close
void close()Closes and destroys this Resource. A resource, once destroyed, cannot be used again. Any tasks pending on the resource will be completed with aResourceDestroyedException
immediately. This is simlar toclose()
in that it frees up memory associated with thisResource
. However, its key difference is that it also propagates exceptions which indicate that it has reached a final state. In contrast tounload()
which indicates that theResource
my be reconstituted later to continue performing work.- Specified by:
close
in interfaceAutoCloseable
-
unload
void unload()Closes and destroys this Resource. A resource, once destroyed, cannot be used again. Any tasks pending on ths resource will not be destroyed or unregistered. It is possible that later a newResource
would be made and the contents of this deserialized back into this one. -
getLogger
org.slf4j.Logger getLogger()Gets theLogger
used by thisResource
.- Returns:
- the logger
-