You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@locus.apache.org on 2000/05/27 01:18:22 UTC

cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util Reaper.java

costin      00/05/26 16:18:22

  Modified:    src/share/org/apache/tomcat/util Reaper.java
  Log:
  "Revived" the Reaper, it can be used to schedule periodic actions.
  This is usefull to avoid the current one thread per context, will
  save resources and allow better control.
  
  Revision  Changes    Path
  1.2       +46 -26    jakarta-tomcat/src/share/org/apache/tomcat/util/Reaper.java
  
  Index: Reaper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/Reaper.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Reaper.java	2000/05/12 02:32:01	1.1
  +++ Reaper.java	2000/05/26 23:18:22	1.2
  @@ -69,47 +69,67 @@
    * data.
    * 
    * @author James Duncan Davidson [duncan@eng.sun.com]
  + * @author Costin Manolache
    */
  -
   public class Reaper extends Thread {
   
  -    private static Reaper reaper;
  -    
  -    static {
  -	reaper = new Reaper();
  -    }
  -    
  -    static Reaper getReaper() {
  -	return reaper;
  +    public Reaper() {
  +	this.setDaemon(true);
  +	this.setName("TomcatReaper");
       }
   
  -    private int interval = 1000 * 60; //ms    
  -    //XXX    private ServerSessionManager serverSessionMgr;
  +    private int interval = 1000 * 60; //ms
       
  -    private Reaper() {
  -	this.setDaemon(true);
  -    }
  +    // XXX TODO Allow per/callback interval, find next, etc
  +    // Right now the "interval" is used for all callbacks
  +    // and it represent a sleep between runs.
       
  -//XXX     void setServerSessionManager(ServerSessionManager serverSessionMgr) {
  -// 	this.serverSessionMgr = serverSessionMgr;
  -//     }
  +    ThreadPoolRunnable cbacks[]=new ThreadPoolRunnable[30]; // XXX max
  +    Object tdata[]=new Object[30]; // XXX max
  +    int count=0;
  +
  +    /** Adding and removing callbacks is synchronized
  +     */
  +    Object lock=new Object();
  +    static boolean running=true;
       
  -    public void run() {
  +    public int addCallback( ThreadPoolRunnable c, int interval ) {
  +	synchronized( lock ) {
  +	    cbacks[count]=c;
  +	    count++;
  +	    return count-1;
  +	}
  +    }
   
  -	// XXX
  -	// eventually, to be nice, this should know when everything
  -	// goes down so that it's not continuing to tick in the background
  +    public void removeCallback( int idx ) {
  +	synchronized( lock ) {
  +	    count--;
  +	    cbacks[idx]=cbacks[count];
  +	    cbacks[count]=null;
  +	}
  +    }
   
  -	while (true) {
  +    public void stopReaper() {
  +	running=false;
  +	this.notify();
  +    }
  +    
  +    public void run() {
  +	while (running) {
   	    try {
   		this.sleep(interval);
   	    } catch (InterruptedException ie) {
   		// sometimes will happen
   	    }
  -
  -	    //XXX     if (serverSessionMgr != null) {
  -	    // 		serverSessionMgr.reap();
  -	    //     }
  +	    
  +	    for( int i=0; i< count; i++ ) {
  +		ThreadPoolRunnable callB=cbacks[i];
  +		// it may be null if a callback is removed.
  +		//  I think the code is correct
  +		if( callB!= null ) {
  +		    callB.runIt( cbacks[i] );
  +		}
  +	    }
   	}
       }
   }