Class Cleaner

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public final class Cleaner
    extends java.lang.Object
    implements java.lang.AutoCloseable
    A context controlling lifecycle of native proxies. When a proxy of a native object is created, it must register a cleaner of the native object in a context. The context performs the cleaning actions in a reversed order of their registration when it is closed. Once closed, the context must no longer be used to register new clean actions.

    The context might have a description of its origin so that it can be identified for a particular context.

    All method arguments are non-null by default.

    This class is not thread-safe.

    See Also:
    Discussion of various approaches to the management of native peers
    • Constructor Summary

      Constructors 
      Constructor Description
      Cleaner()
      Creates a new cleaner with no (an empty) description.
      Cleaner​(java.lang.String description)
      Creates a new cleaner.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(CleanAction<?> cleanAction)
      Registers a new clean action with this context.
      void close()
      Performs all the clean operations that has been registered in this context in a reversed order of the registration order.
      java.lang.String getDescription()
      Returns a description of this cleaner.
      int getNumRegisteredActions()
      Returns the number of the registered clean actions.
      boolean isClosed()
      Returns true if this cleaner is closed.
      java.lang.String toString()
      Returns a string representation of this object, including its hash code so that this instance can be easily identified in the logs.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Cleaner

        public Cleaner()
        Creates a new cleaner with no (an empty) description.
      • Cleaner

        public Cleaner​(java.lang.String description)
        Creates a new cleaner.
        Parameters:
        description - a description of this context, which describes its origin and is included in toString()
    • Method Detail

      • isClosed

        public boolean isClosed()
        Returns true if this cleaner is closed.
      • add

        public void add​(CleanAction<?> cleanAction)
        Registers a new clean action with this context. If the context is already closed, the clean action will be executed immediately.
        Parameters:
        cleanAction - a clean action to register; must not be null
        Throws:
        java.lang.IllegalStateException - if it’s attempted to add a clean action to a closed context
      • close

        public void close()
                   throws CloseFailuresException
        Performs all the clean operations that has been registered in this context in a reversed order of the registration order.

        If any clean operation throws an exception in its CleanAction.clean(), the context logs the exception and attempts to perform the remaining operations.

        The implementation is idempotent — subsequent invocations have no effect.

        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        CloseFailuresException - if any clean action failed. The exception includes all thrown exceptions as suppressed
      • getDescription

        public java.lang.String getDescription()
        Returns a description of this cleaner. May be empty.
      • getNumRegisteredActions

        public int getNumRegisteredActions()
        Returns the number of the registered clean actions.
      • toString

        public java.lang.String toString()
        Returns a string representation of this object, including its hash code so that this instance can be easily identified in the logs.
        Overrides:
        toString in class java.lang.Object