Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/io/util/MemoryBoundedObjectManager.java


1   /** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * Copyright 2004 Hiram Chirino
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License"); 
7    * you may not use this file except in compliance with the License. 
8    * You may obtain a copy of the License at 
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS, 
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
15   * See the License for the specific language governing permissions and 
16   * limitations under the License. 
17   * 
18   **/
19  
20  package org.activemq.io.util;
21  import java.util.HashSet;
22  import java.util.Iterator;
23  
24  import org.activemq.capacity.BasicCapacityMonitor;
25  
26  import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
27  
28  /**
29   * Keeps track of MemoryBoundedObjects 
30   *  
31   * @version $Revision: 1.1.1.1 $
32   */
33  public class MemoryBoundedObjectManager extends BasicCapacityMonitor {
34      
35      private final SynchronizedLong totalMemoryUsedSize = new SynchronizedLong(0);
36      private final HashSet managedObjects = new HashSet();
37      boolean closed;
38      private boolean supportJMSPriority = false;
39  
40      /**
41       * @param name
42       * @param maxSize
43       */
44      public MemoryBoundedObjectManager(String name, long maxSize) {
45          this(name, maxSize, false);
46      }
47  
48      public MemoryBoundedObjectManager(String name, long maxSize, boolean supportJMSPriority) {
49          super(name, maxSize);
50          this.supportJMSPriority = supportJMSPriority;
51      }
52  
53      synchronized public void add(MemoryBoundedObject o) {
54          managedObjects.add(o);
55      }
56  
57      /**
58       * close this queue manager and all associated MemoryBoundedQueues
59       */
60      public void close() {        
61          HashSet copy;
62          synchronized (this) {
63              if(closed)
64                  return;
65              closed=true;
66              copy = new HashSet(managedObjects);
67          }        
68          for (Iterator i = copy.iterator(); i.hasNext();) {
69              MemoryBoundedObject o = (MemoryBoundedObject) i.next();
70              o.close();
71          }
72      }
73  
74      /**
75       * @param name
76       */
77      public synchronized void remove(MemoryBoundedObject o) {
78          managedObjects.remove(o);
79      }
80  
81      /**
82       * @return the calculated total memory usage assocated with all it's queues
83       */
84      public long getTotalMemoryUsedSize() {
85          return totalMemoryUsedSize.get();
86      }
87  
88      /**
89       * @return true if this MemoryBoundedObjectManager has reached it's predefined limit
90       */
91      public boolean isFull() {
92          boolean result = totalMemoryUsedSize.get() >= super.getValueLimit();
93          return result;
94      }
95  
96      /**
97       * @return true if this MemoryBoundedObjectManager has reached it's predefined limit
98       */
99      public float getPercentFull() {
100         return ( totalMemoryUsedSize.get() / super.getValueLimit() );
101     }
102 
103     public void incrementMemoryUsed(int size) {
104         totalMemoryUsedSize.add(size);
105         super.setCurrentValue(totalMemoryUsedSize.get());
106     }
107 
108     public void decrementMemoryUsed(int size) {
109         totalMemoryUsedSize.subtract(size);
110         super.setCurrentValue(totalMemoryUsedSize.get());
111     }
112 
113     protected void finalize() {
114         close();
115     }
116 
117   /**
118    * @return Returns the supportJMSPriority.
119    */
120   public boolean isSupportJMSPriority() {
121     return supportJMSPriority;
122   }
123   /**
124    * @param supportJMSPriority The supportJMSPriority to set.
125    */
126   public void setSupportJMSPriority(boolean supportJMSPriority) {
127     this.supportJMSPriority = supportJMSPriority;
128   }
129 }