Class DisposeManager


  • public class DisposeManager
    extends Object
    A central place to register commands to execute when the application is shutdown, which is when it calls dispose(). This is useful to register clean-up tasks, to ensure resources and background jobs are not left over on shutdown.

    What shutdown means exactly depends on the application itself and how it integrates in its environment. It can be when the servlet context is destroyed or when the plug-in is stopped, if the application is a plug-in in its host product. Shutdown is when nothing in the application is to continue executing in any way.

    Client application may use add(Runnable) to register a command to execute on shutdown. Client application must call dispose() when it is shutdown.

    Since:
    3.11.0
    • Method Detail

      • getInstance

        public static DisposeManager getInstance()
        Returns the dispose manager instance.
        Returns:
        the dispose manager instance
      • add

        public void add​(Runnable command)
        Registers a command to execute when the application is shutdown, which is when dispose() is called. The command is expected to perform its own error handling by catching exceptions as appropriate.

        The given Runnable instance will stay in memory as long as the application is alive. Callers should be cautious and make sure not to hold too much data in memory along this command. Best practice is to register a reference to a method, like below:

         DisposeManager.getInstance().add(myObjectToDispose::dispose);
         

        Also take in consideration that the command executes when application terminates normally, not in case of a crash.

        Parameters:
        command - a command to execute on application shutdown
        Throws:
        NullPointerException - if command is null
      • remove

        public void remove​(Runnable command)
        Unregisters a command that was previously added.
        Parameters:
        command - a command to no longer execute on application shutdown
        Throws:
        NullPointerException - if command is null
      • dispose

        public void dispose()
        Indicates the application is shutdown and all registered commands are to execute.

        What shutdown means exactly depends on the application itself and how it integrates in its environment. It can be when the servlet context is destroyed or when the plug-in is stopped, if the application is a plug-in in its host product. Shutdown is when nothing in the application is to continue executing in any way.