001 /**
002 *
003 * Copyright 2004 Protique Ltd
004 * Copyright 2004 Hiram Chirino
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 **/
019
020 package org.activemq.io.util;
021 import java.util.HashSet;
022 import java.util.Iterator;
023
024 import org.activemq.capacity.BasicCapacityMonitor;
025
026 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
027
028 /**
029 * Keeps track of MemoryBoundedObjects
030 *
031 * @version $Revision: 1.1.1.1 $
032 */
033 public class MemoryBoundedObjectManager extends BasicCapacityMonitor {
034
035 private final SynchronizedLong totalMemoryUsedSize = new SynchronizedLong(0);
036 private final HashSet managedObjects = new HashSet();
037 boolean closed;
038 private boolean supportJMSPriority = false;
039
040 /**
041 * @param name
042 * @param maxSize
043 */
044 public MemoryBoundedObjectManager(String name, long maxSize) {
045 this(name, maxSize, false);
046 }
047
048 public MemoryBoundedObjectManager(String name, long maxSize, boolean supportJMSPriority) {
049 super(name, maxSize);
050 this.supportJMSPriority = supportJMSPriority;
051 }
052
053 synchronized public void add(MemoryBoundedObject o) {
054 managedObjects.add(o);
055 }
056
057 /**
058 * close this queue manager and all associated MemoryBoundedQueues
059 */
060 public void close() {
061 HashSet copy;
062 synchronized (this) {
063 if(closed)
064 return;
065 closed=true;
066 copy = new HashSet(managedObjects);
067 }
068 for (Iterator i = copy.iterator(); i.hasNext();) {
069 MemoryBoundedObject o = (MemoryBoundedObject) i.next();
070 o.close();
071 }
072 }
073
074 /**
075 * @param name
076 */
077 public synchronized void remove(MemoryBoundedObject o) {
078 managedObjects.remove(o);
079 }
080
081 /**
082 * @return the calculated total memory usage assocated with all it's queues
083 */
084 public long getTotalMemoryUsedSize() {
085 return totalMemoryUsedSize.get();
086 }
087
088 /**
089 * @return true if this MemoryBoundedObjectManager has reached it's predefined limit
090 */
091 public boolean isFull() {
092 boolean result = totalMemoryUsedSize.get() >= super.getValueLimit();
093 return result;
094 }
095
096 /**
097 * @return true if this MemoryBoundedObjectManager has reached it's predefined limit
098 */
099 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 }