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 intDefault IO Buffer size, 4kb -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes and destroys this Resource.voidRestores the entire state of thisResourcefrom the suppliedInputStream.default voidRestores the entire state of thisResourcefrom the suppliedInputStream.Gets theAttributesassociated with thisResourcegetId()Returns the immutable and globally-unique ID of this resource.org.slf4j.LoggerGets theLoggerused by thisResource.getMethodDispatcher(String name) Returns an instance ofMethodDispatcher, which is used to invoke methods against thisResource.getTasks()default booleanReturns true if the resource is set to enable verbose mode.voidResumes the supplied task with theTaskIdand the list of results.default voidresumeFromNetwork(TaskId taskId, Object result) Resumes a suspended task, accepting the task id.default voidresumeFromScheduler(TaskId taskId, double elapsedTime) Resumes a suspended task, accepting the task id.default voidresumeWithError(TaskId taskId, Throwable throwable) Resumes a suspended task, accepting the task id.voidDumps the entire contents of thisResourceto the suppliedOutputStreamwhere it can be reconstituted later using thedeserialize(InputStream)method.default voidDumps the entire contents of thisResourceto the suppliedWritableByteChannelwhere it can be reconstituted later using thedeserialize(InputStream)method.default voidsetVerbose(boolean verbose) Sets the verbose mode.voidunload()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 theAttributesassociated with thisResource- Returns:
- this instance's
Attributes
-
getMethodDispatcher
Returns an instance ofMethodDispatcher, which is used to invoke methods against thisResource. The reurnedMethodDispatcherwill 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 theTaskIdand the list of results.- Parameters:
taskId- theTaskIdof 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- theTaskIdif 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- theTaskIdif the running taskelapsedTime- the amount of time elapsed since the task was last activated
-
serialize
Dumps the entire contents of thisResourceto the suppliedOutputStreamwhere it can be reconstituted later using thedeserialize(InputStream)method.- Parameters:
os- theOutputStreamused to receive the serializedResource- Throws:
IOException- if something failed during serialization
-
deserialize
Restores the entire state of thisResourcefrom the suppliedInputStream. This assumes theInputStreamwas produced by a call toserialize(OutputStream).- Parameters:
is- theInputStreamfrom which to read the serialized resource- Throws:
IOException- if something failed during deserialization
-
serialize
Dumps the entire contents of thisResourceto the suppliedWritableByteChannelwhere it can be reconstituted later using thedeserialize(InputStream)method.- Parameters:
wbc- theOutputStreamused to receive the serializedResource- Throws:
IOException- if something failed during serialization
-
deserialize
Restores the entire state of thisResourcefrom the suppliedInputStream. This assumes theInputStreamwas produced by a call toserialize(OutputStream).- Parameters:
ibc- theInputStreamfrom 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 aResourceDestroyedExceptionimmediately. 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 theResourcemy be reconstituted later to continue performing work.- Specified by:
closein 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 newResourcewould be made and the contents of this deserialized back into this one. -
getLogger
org.slf4j.Logger getLogger()Gets theLoggerused by thisResource.- Returns:
- the logger
-