Interface Resource

All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
TransactionalResource

public interface Resource extends AutoCloseable
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 Details

    • DEFAULT_IO_BUFFER_SIZE

      static final int DEFAULT_IO_BUFFER_SIZE
      Default 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 the Attributes associated with this Resource
      Returns:
      this instance's Attributes
    • getMethodDispatcher

      MethodDispatcher getMethodDispatcher(String name)
      Returns an instance of MethodDispatcher, which is used to invoke methods against this Resource. The reurned MethodDispatcher will defer actually invoking the method until the final call in the chain ResultAcceptor.dispatch(Consumer, Consumer).
      Parameters:
      name - the name of the method
      Returns:
      the MethodDispatcher, never null
      Throws:
      MethodNotFoundException - if the method cannot be found
    • resume

      void resume(TaskId taskId, Object... results)
      Resumes the supplied task with the TaskId and the list of results.
      Parameters:
      taskId - the TaskId of the associated task
      results - zero or more results supplied to the resumed task
    • resumeFromNetwork

      default void resumeFromNetwork(TaskId taskId, Object result)
      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 - the TaskId if the running task
      result - the resulting object from the call
    • resumeWithError

      default void resumeWithError(TaskId taskId, Throwable throwable)
      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

      default void resumeFromScheduler(TaskId taskId, double elapsedTime)
      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 - the TaskId if the running task
      elapsedTime - the amount of time elapsed since the task was last activated
    • serialize

      void serialize(OutputStream os) throws IOException
      Dumps the entire contents of this Resource to the supplied OutputStream where it can be reconstituted later using the deserialize(InputStream) method.
      Parameters:
      os - the OutputStream used to receive the serialized Resource
      Throws:
      IOException - if something failed during serialization
    • deserialize

      void deserialize(InputStream is) throws IOException
      Restores the entire state of this Resource from the supplied InputStream. This assumes the InputStream was produced by a call to serialize(OutputStream).
      Parameters:
      is - the InputStream from which to read the serialized resource
      Throws:
      IOException - if something failed during deserialization
    • serialize

      default void serialize(WritableByteChannel wbc) throws IOException
      Dumps the entire contents of this Resource to the supplied WritableByteChannel where it can be reconstituted later using the deserialize(InputStream) method.
      Parameters:
      wbc - the OutputStream used to receive the serialized Resource
      Throws:
      IOException - if something failed during serialization
    • deserialize

      default void deserialize(ReadableByteChannel ibc) throws IOException
      Restores the entire state of this Resource from the supplied InputStream. This assumes the InputStream was produced by a call to serialize(OutputStream).
      Parameters:
      ibc - the InputStream 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

      Set<TaskId> getTasks()
      Gets a Set<TaskId> representing active tasks at the current state.
      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 a ResourceDestroyedException immediately. This is simlar to close() in that it frees up memory associated with this Resource. However, its key difference is that it also propagates exceptions which indicate that it has reached a final state. In contrast to unload() which indicates that the Resource my be reconstituted later to continue performing work.
      Specified by:
      close in interface AutoCloseable
    • 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 new Resource would be made and the contents of this deserialized back into this one.
    • getLogger

      org.slf4j.Logger getLogger()
      Gets the Logger used by this Resource.
      Returns:
      the logger