Class Cleanup<K,​V>

  • All Implemented Interfaces:
    Runnable
    Direct Known Subclasses:
    LockingCleanup

    public class Cleanup<K,​V>
    extends MyThread
    General utility for applying retention policies to maps (which covers all caching as well as stores). This may be used either directly by calling age() or by running this as a thread.

    Use

    Create an instance of this either
    You must then set some RetentionPolicy so that the cleanup knows when to expunge entries (there are several possible policies and you can have as many as you want, though it is up to you to ensure your policies make sense.) Start this in its own thread and it will quietly clean up ever so often. To stop the thread, call the MyThread.setStopThread(boolean) to true and the thread will halt as soon as it wakes up. YOu can also kill the thread directly but generally that can do bad things if the thread is in the middle of, say, updating you store. Most web servers will wait until all threads exit of their own accord before finishing a shutdown, incidentally.

    Created by Jeff Gaynor
    on 7/12/11 at 11:39 AM

    • Method Detail

      • isFailOnError

        public boolean isFailOnError()
        If this set true, then any failures cause the cleanup to stop.

        If false, then this will attempt to remove all elements. Failures are logged in the getFailures() list.
        Returns:
      • setFailOnError

        public void setFailOnError​(boolean failOnError)
      • isEnabledLocking

        public boolean isEnabledLocking()
      • setEnabledLocking

        public void setEnabledLocking​(boolean enabledLocking)
      • info

        public void info​(List<V> removed)
        Log the results of this. Override this if you need more detailed output. It takes the list of removed entries. Actual writing to the log is done with the call MyThread.info(String).
        Parameters:
        removed -
      • getFailures

        public List<String> getFailures()
        List of failures. Each entry is for the form
             id: message
         
        Returns:
      • getStore

        public Store getStore()
      • setStore

        public void setStore​(Store store)
      • age

        public List<V> age()
      • oldAge

        protected List<V> oldAge()
        Clean out old entries by aging the elements, i.e., apply the retention policies. Returns a list of the entries removed
      • getSortedKeys

        public Set<K> getSortedKeys()
        Return all the keys for the map, sorted into a some order, usually by timestamp. The default assumes that the keys are comparable and orders them. Override as needed.
        Returns:
      • getMap

        public Map<K,​V> getMap()
      • setMap

        public void setMap​(Map<K,​V> map)
      • addRetentionPolicy

        public void addRetentionPolicy​(RetentionPolicy retentionPolicy)
      • removeRetentionPolicy

        public void removeRetentionPolicy​(RetentionPolicy retentionPolicy)
      • run

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