1 2 3 /* 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 5 * 6 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. 7 * 8 * Portions Copyright Apache Software Foundation. 9 * 10 * The contents of this file are subject to the terms of either the GNU 11 * General Public License Version 2 only ("GPL") or the Common Development 12 * and Distribution License("CDDL") (collectively, the "License"). You 13 * may not use this file except in compliance with the License. You can obtain 14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html 15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific 16 * language governing permissions and limitations under the License. 17 * 18 * When distributing the software, include this License Header Notice in each 19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. 20 * Sun designates this particular file as subject to the "Classpath" exception 21 * as provided by Sun in the GPL Version 2 section of the License file that 22 * accompanied this code. If applicable, add the following below the License 23 * Header, with the fields enclosed by brackets [] replaced by your own 24 * identifying information: "Portions Copyrighted [year] 25 * [name of copyright owner]" 26 * 27 * Contributor(s): 28 * 29 * If you wish your version of this file to be governed by only the CDDL or 30 * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 * elects to include this software in this distribution under the [CDDL or GPL 32 * Version 2] license." If you don't indicate a single choice of license, a 33 * recipient has the option to distribute your version of this file under 34 * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 * its licensees as provided above. However, if you add GPL Version 2 code 36 * and therefore, elected the GPL Version 2 license, then the option applies 37 * only if the new code is made subject to such option by the copyright 38 * holder. 39 */ 40 package org.apache.tomcat.util.threads; 41 42 import org.apache.tomcat.util; 43 44 /** 45 * The reaper is a background thread with which ticks every minute 46 * and calls registered objects to allow reaping of old session 47 * data. 48 * 49 * @author James Duncan Davidson [duncan@eng.sun.com] 50 * @author Costin Manolache 51 */ 52 public class Reaper extends Thread { 53 54 private static com.sun.org.apache.commons.logging.Log log= 55 com.sun.org.apache.commons.logging.LogFactory.getLog(Reaper.class ); 56 57 private boolean daemon=false; 58 59 public Reaper() { 60 if( daemon ) 61 this.setDaemon(true); 62 this.setName("TomcatReaper"); 63 } 64 65 public Reaper(String name) { 66 if( daemon ) 67 this.setDaemon(true); 68 this.setName(name); 69 } 70 71 private long interval = 1000 * 60; //ms 72 73 // XXX TODO Allow per/callback interval, find next, etc 74 // Right now the "interval" is used for all callbacks 75 // and it represent a sleep between runs. 76 77 ThreadPoolRunnable cbacks[]=new ThreadPoolRunnable[30]; // XXX max 78 Object tdata[][]=new Object[30][]; // XXX max 79 int count=0; 80 81 /** Adding and removing callbacks is synchronized 82 */ 83 Object lock=new Object(); 84 private boolean running=true; 85 86 // XXX Should be called 'interval' not defaultInterval 87 88 public void setDefaultInterval( long t ) { 89 interval=t; 90 } 91 92 public long getDefaultIntervale() { 93 return interval; 94 } 95 96 public int addCallback( ThreadPoolRunnable c, int interval ) { 97 synchronized( lock ) { 98 cbacks[count]=c; 99 count++; 100 return count-1; 101 } 102 } 103 104 public void removeCallback( int idx ) { 105 synchronized( lock ) { 106 count--; 107 cbacks[idx]=cbacks[count]; 108 cbacks[count]=null; 109 } 110 } 111 112 public void startReaper() { 113 running=true; 114 this.start(); 115 } 116 117 public synchronized void stopReaper() { 118 running=false; 119 if (log.isDebugEnabled()) 120 log.debug("Stop reaper "); 121 this.interrupt(); // notify() doesn't stop sleep 122 } 123 124 public void run() { 125 while (running) { 126 if( !running) break; 127 try { 128 this.sleep(interval); 129 } catch (InterruptedException ie) { 130 // sometimes will happen 131 } 132 133 if( !running) break; 134 for( int i=0; i< count; i++ ) { 135 ThreadPoolRunnable callB=cbacks[i]; 136 // it may be null if a callback is removed. 137 // I think the code is correct 138 if( callB!= null ) { 139 callB.runIt( tdata[i] ); 140 } 141 if( !running) break; 142 } 143 } 144 } 145 }