Package org.directmemory.supervisor

Source Code of org.directmemory.supervisor.TimedSupervisor

package org.directmemory.supervisor;


import java.util.Date;

import org.directmemory.CacheStore;
import org.javasimon.SimonManager;
import org.javasimon.Split;
import org.javasimon.Stopwatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TimedSupervisor implements Supervisor {

  private static Logger logger=LoggerFactory.getLogger(TimedSupervisor.class);

  public long batchInterval = 0;
  Date lastCheck = new Date();
     
  private abstract class ThreadUsingCache extends Thread {
    public ThreadUsingCache(CacheStore cache) {
      super();
      this.cache = cache;
    }

    public CacheStore cache;
  }
 
  public TimedSupervisor (long batchInterval) {
    this.batchInterval = batchInterval;
  }
 
  /* (non-Javadoc)
   * @see org.directmemory.supervisor.Supervisor#checkLimits(org.directmemory.CacheStore)
   */
  public void disposeOverflow(CacheStore cache) {
    Stopwatch stopWatch = SimonManager.getStopwatch("supervisor.timed.checkLimits");
    Split split = stopWatch.start();
    long passed = new Date().getTime() - lastCheck.getTime();
    if (passed >= batchInterval) {
      lastCheck = new Date();
      new ThreadUsingCache(cache) {
        public void run() {
          logger.debug("checking memory limits");
          cache.disposeHeapOverflow();
          cache.disposeOffHeapOverflow();
          logger.debug("checking expired entries");
          cache.disposeExpired();
        }
      }.start();
    }
    split.stop();
  }
 
}
TOP

Related Classes of org.directmemory.supervisor.TimedSupervisor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.