Class ResourceMonitor

  • All Implemented Interfaces:
    Runnable

    public class ResourceMonitor
    extends Thread

    A common pitfall in Java is not properly closing resources. This can become especially tricky when dealing with concurrency and the new Java stream framework. Mango provides a `ResourceMonitor` which tracks `MonitoredObjects` and automatically closes (frees) them when they are no longer referenced. The `ResourceMonitor` is basically a garbage collector for resources!

    Author:
    David B. Bracewell
    • Method Detail

      • monitor

        public static Connection monitor​(@NonNull
                                         @NonNull Connection connection)
        Monitors the given SQL Connection closing it when it is no longer referenced. Note that connection is only closed and not committed.
        Parameters:
        connection - the connection
        Returns:
        the connection
      • monitor

        public static InputStream monitor​(@NonNull
                                          @NonNull InputStream stream)
        Monitors the given InputStream.
        Parameters:
        stream - the stream
        Returns:
        the input stream
      • monitor

        public static OutputStream monitor​(@NonNull
                                           @NonNull OutputStream stream)
        Monitors the given OutputStream.
        Parameters:
        stream - the stream
        Returns:
        the output stream
      • monitor

        public static Reader monitor​(@NonNull
                                     @NonNull Reader reader)
        Monitors the given Reader.
        Parameters:
        reader - the reader
        Returns:
        the reader
      • monitor

        public static Writer monitor​(@NonNull
                                     @NonNull Writer writer)
        Monitors the given Writer.
        Parameters:
        writer - the writer
        Returns:
        the writer
      • monitor

        public static <T> Stream<T> monitor​(@NonNull
                                            @NonNull Stream<T> stream)
        Monitors the given Stream.
        Type Parameters:
        T - the type parameter
        Parameters:
        stream - the stream
        Returns:
        the stream
      • monitor

        public static <T> MStream<T> monitor​(@NonNull
                                             @NonNull MStream<T> stream)
        Monitors the given MStream
        Type Parameters:
        T - the type parameter
        Parameters:
        stream - the stream
        Returns:
        the m stream
      • monitor

        public static DoubleStream monitor​(@NonNull
                                           @NonNull DoubleStream stream)
        Monitors the given DoubleStream
        Parameters:
        stream - the stream
        Returns:
        the double stream
      • monitor

        public static IntStream monitor​(@NonNull
                                        @NonNull IntStream stream)
        Monitors the given IntStream
        Parameters:
        stream - the stream
        Returns:
        the int stream
      • monitor

        public static LongStream monitor​(@NonNull
                                         @NonNull LongStream stream)
        Monitors the given LongStream
        Parameters:
        stream - the stream
        Returns:
        the long stream
      • monitor

        public static <T> MonitoredObject<T> monitor​(@NonNull
                                                     T object)
        Monitors the given Object
        Type Parameters:
        T - the type parameter
        Parameters:
        object - the object
        Returns:
        the monitored object
      • monitor

        public static <T> MonitoredObject<T> monitor​(@NonNull
                                                     T object,
                                                     @NonNull
                                                     @NonNull Consumer<T> onClose)
        Monitors the given Object using the given onClose command
        Type Parameters:
        T - the type parameter
        Parameters:
        object - the object
        onClose - the on close
        Returns:
        the monitored object
      • addResource

        protected <T> T addResource​(@NonNull
                                    @NonNull Object referent,
                                    @NonNull
                                    T resource)
      • addResource

        protected <T> T addResource​(Object referent,
                                    T resource,
                                    Consumer<T> onClose)
      • run

        public void run()
        Specified by:
        run in interface Runnable
        Overrides:
        run in class Thread