View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.cache;
17  
18  import java.util.NoSuchElementException;
19  import java.io.Serializable;
20  
21  /**
22   * tk.
23   * @version $Id: StaleObjectEvictor.java 155435 2005-02-26 13:17:27Z dirkv $
24   * @author Rodney Waldhoff
25   */
26  public class StaleObjectEvictor implements Runnable, Serializable {
27    public transient static final int DEFAULT_OBJECTS_BETWEEN_NAPS;
28    static {
29      int objsTweenNaps = 10;
30      try {
31        objsTweenNaps = Integer.parseInt(System.getProperty("org.apache.commons.cache.StaleObjectEvictor.objs-between-naps","10"));
32      } catch(Exception e) {
33        objsTweenNaps = 10;
34      }
35      DEFAULT_OBJECTS_BETWEEN_NAPS = objsTweenNaps;
36    }
37  
38    public transient static final long DEFAULT_SLEEP_TIME_MILLIS;
39    static {
40      long sleepTime = 10000;
41      try {
42        sleepTime = Long.parseLong(System.getProperty("org.apache.commons.cache.StaleObjectEvictor.sleep-time-millis","10000"));
43      } catch(Exception e) {
44        sleepTime = 10000;
45      }
46      DEFAULT_SLEEP_TIME_MILLIS = sleepTime;
47    }
48  
49    protected int _objsBetweenNaps = DEFAULT_OBJECTS_BETWEEN_NAPS;
50    protected long _sleepTimeMillis = DEFAULT_SLEEP_TIME_MILLIS;
51    protected CachedObjectIterator _iterator;
52    protected boolean _cancelled = false;
53  
54    public StaleObjectEvictor(CachedObjectIterator it) {
55      this(it,DEFAULT_OBJECTS_BETWEEN_NAPS,DEFAULT_SLEEP_TIME_MILLIS);
56    }
57  
58    public StaleObjectEvictor(CachedObjectIterator it, int objsBetweenNaps) {
59      this(it,objsBetweenNaps,DEFAULT_SLEEP_TIME_MILLIS);
60    }
61  
62    public StaleObjectEvictor(CachedObjectIterator it, long sleepTimeMillis) {
63      this(it,DEFAULT_OBJECTS_BETWEEN_NAPS,sleepTimeMillis);
64    }
65  
66    public StaleObjectEvictor(CachedObjectIterator it, int objsBetweenNaps, long sleepTimeMillis) {
67      _iterator = it;
68      _objsBetweenNaps = objsBetweenNaps;
69      _sleepTimeMillis = sleepTimeMillis;
70    }
71  
72    public void cancel() {
73      _cancelled = true;
74    }
75  
76    public void run() {
77      while(!_cancelled) {
78        for(int i=0;i<_objsBetweenNaps;i++) {
79          CachedObjectInfo info = null;
80          try {
81            info = _iterator.getNext();
82          } catch(NoSuchElementException e) {
83            info = null;
84          }
85          if(null != info) {
86            Long expiry = info.getExpirationTs();
87            if(null != expiry) {
88              if(System.currentTimeMillis() >= expiry.longValue()) {
89              try {
90                  _iterator.remove();
91                } catch(Exception e) {
92                  // ignored
93                }
94              }
95            }
96          } else {
97            break;
98          }
99        }
100       try {
101         Thread.sleep(_sleepTimeMillis);
102       } catch(InterruptedException e) {
103         // ignored
104       }
105     }
106   }
107 
108   /*
109   public void passivate() {
110     _cancelled = true;
111   }
112 
113   public void activate() {
114     _cancelled = false;
115   }
116   */
117 
118 
119 }