001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.pool; 019 020 import java.util.NoSuchElementException; 021 022 /** 023 * A "keyed" pooling interface. 024 * <p> 025 * A keyed pool pools instances of multiple types. Each 026 * type may be accessed using an arbitrary key. 027 * </p> 028 * <p> 029 * Example of use: 030 * <pre style="border:solid thin; padding: 1ex;" 031 * > Object obj = <code style="color:#00C">null</code>; 032 * Object key = <code style="color:#C00">"Key"</code>; 033 * 034 * <code style="color:#00C">try</code> { 035 * obj = pool.borrowObject(key); 036 * <code style="color:#0C0">//...use the object...</code> 037 * } <code style="color:#00C">catch</code>(Exception e) { 038 * <code style="color:#0C0">// invalidate the object</code> 039 * pool.invalidateObject(key, obj); 040 * <code style="color:#0C0">// do not return the object to the pool twice</code> 041 * obj = <code style="color:#00C">null</code>; 042 * } <code style="color:#00C">finally</code> { 043 * <code style="color:#0C0">// make sure the object is returned to the pool</code> 044 * <code style="color:#00C">if</code>(<code style="color:#00C">null</code> != obj) { 045 * pool.returnObject(key, obj); 046 * } 047 * }</pre> 048 * </p> 049 * <p> 050 * {@link KeyedObjectPool} implementations <i>may</i> choose to store at most 051 * one instance per key value, or may choose to maintain a pool of instances 052 * for each key (essentially creating a {@link java.util.Map Map} of 053 * {@link ObjectPool pools}). 054 * </p> 055 * 056 * <p>See {@link BaseKeyedObjectPool} for a simple base implementation.</p> 057 * 058 * @param <K> the type of keys in this pool 059 * @param <V> the type of objects held in this pool 060 * 061 * @author Rodney Waldhoff 062 * @author Sandy McArthur 063 * @version $Revision: 1222396 $ $Date: 2011-12-22 14:02:25 -0500 (Thu, 22 Dec 2011) $ 064 * @see KeyedPoolableObjectFactory 065 * @see KeyedObjectPoolFactory 066 * @see ObjectPool 067 * @see BaseKeyedObjectPool 068 * @since Pool 1.0 069 */ 070 public interface KeyedObjectPool<K, V> { 071 /** 072 * Obtains an instance from this pool for the specified <code>key</code>. 073 * <p> 074 * Instances returned from this method will have been either newly created with 075 * {@link KeyedPoolableObjectFactory#makeObject makeObject} or will be a previously idle object and 076 * have been activated with {@link KeyedPoolableObjectFactory#activateObject activateObject} and 077 * then validated with {@link KeyedPoolableObjectFactory#validateObject validateObject}. 078 * <p> 079 * By contract, clients <strong>must</strong> return the borrowed object using 080 * {@link #returnObject returnObject}, {@link #invalidateObject invalidateObject}, or a related method 081 * as defined in an implementation or sub-interface, 082 * using a <code>key</code> that is {@link Object#equals equivalent} to the one used to 083 * borrow the instance in the first place. 084 * <p> 085 * The behaviour of this method when the pool has been exhausted 086 * is not strictly specified (although it may be specified by implementations). 087 * Older versions of this method would return <code>null</code> to indicate exhaustion, 088 * newer versions are encouraged to throw a {@link NoSuchElementException}. 089 * 090 * @param key the key used to obtain the object 091 * @return an instance from this pool. 092 * @throws IllegalStateException after {@link #close close} has been called on this pool 093 * @throws Exception when {@link KeyedPoolableObjectFactory#makeObject makeObject} throws an exception 094 * @throws NoSuchElementException when the pool is exhausted and cannot or will not return another instance 095 */ 096 V borrowObject(K key) throws Exception, NoSuchElementException, IllegalStateException; 097 098 /** 099 * Return an instance to the pool. 100 * By contract, <code>obj</code> <strong>must</strong> have been obtained 101 * using {@link #borrowObject borrowObject} 102 * or a related method as defined in an implementation 103 * or sub-interface 104 * using a <code>key</code> that is equivalent to the one used to 105 * borrow the instance in the first place. 106 * 107 * @param key the key used to obtain the object 108 * @param obj a {@link #borrowObject borrowed} instance to be returned. 109 * @throws Exception 110 */ 111 void returnObject(K key, V obj) throws Exception; 112 113 /** 114 * <p>Invalidates an object from the pool.</p> 115 * 116 * <p>By contract, <code>obj</code> <strong>must</strong> have been obtained 117 * using {@link #borrowObject borrowObject} or a related method as defined 118 * in an implementation or sub-interface using a <code>key</code> that is 119 * equivalent to the one used to borrow the <code>Object</code> in the first place.</p> 120 * 121 * <p>This method should be used when an object that has been borrowed 122 * is determined (due to an exception or other problem) to be invalid.</p> 123 * 124 * @param key the key used to obtain the object 125 * @param obj a {@link #borrowObject borrowed} instance to be returned. 126 * @throws Exception 127 */ 128 void invalidateObject(K key, V obj) throws Exception; 129 130 /** 131 * Create an object using the {@link KeyedPoolableObjectFactory factory} or other 132 * implementation dependent mechanism, passivate it, and then place it in the idle object pool. 133 * <code>addObject</code> is useful for "pre-loading" a pool with idle objects 134 * (Optional operation). 135 * 136 * @param key the key a new instance should be added to 137 * @throws Exception when {@link KeyedPoolableObjectFactory#makeObject} fails. 138 * @throws IllegalStateException after {@link #close} has been called on this pool. 139 * @throws UnsupportedOperationException when this pool cannot add new idle objects. 140 */ 141 void addObject(K key) throws Exception, IllegalStateException, UnsupportedOperationException; 142 143 /** 144 * Returns the number of instances 145 * corresponding to the given <code>key</code> 146 * currently idle in this pool (optional operation). 147 * Returns a negative value if this information is not available. 148 * 149 * @param key the key to query 150 * @return the number of instances corresponding to the given <code>key</code> currently idle in this pool or a negative value if unsupported 151 * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation 152 */ 153 int getNumIdle(K key) throws UnsupportedOperationException; 154 155 /** 156 * Returns the number of instances 157 * currently borrowed from but not yet returned 158 * to the pool corresponding to the 159 * given <code>key</code> (optional operation). 160 * Returns a negative value if this information is not available. 161 * 162 * @param key the key to query 163 * @return the number of instances corresponding to the given <code>key</code> currently borrowed in this pool or a negative value if unsupported 164 * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation 165 */ 166 int getNumActive(K key) throws UnsupportedOperationException; 167 168 /** 169 * Returns the total number of instances 170 * currently idle in this pool (optional operation). 171 * Returns a negative value if this information is not available. 172 * 173 * @return the total number of instances currently idle in this pool or a negative value if unsupported 174 * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation 175 */ 176 int getNumIdle() throws UnsupportedOperationException; 177 178 /** 179 * Returns the total number of instances 180 * current borrowed from this pool but not 181 * yet returned (optional operation). 182 * Returns a negative value if this information is not available. 183 * 184 * @return the total number of instances currently borrowed from this pool or a negative value if unsupported 185 * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation 186 */ 187 int getNumActive() throws UnsupportedOperationException; 188 189 /** 190 * Clears the pool, removing all pooled instances (optional operation). 191 * Throws {@link UnsupportedOperationException} if the pool cannot be cleared. 192 * 193 * @throws UnsupportedOperationException when this implementation doesn't support the operation 194 */ 195 void clear() throws Exception, UnsupportedOperationException; 196 197 /** 198 * Clears the specified pool, removing all 199 * pooled instances corresponding to 200 * the given <code>key</code> (optional operation). 201 * Throws {@link UnsupportedOperationException} if the pool cannot be cleared. 202 * 203 * @param key the key to clear 204 * @throws UnsupportedOperationException when this implementation doesn't support the operation 205 */ 206 void clear(K key) throws Exception, UnsupportedOperationException; 207 208 /** 209 * Close this pool, and free any resources associated with it. 210 * <p> 211 * Calling {@link #addObject addObject} or {@link #borrowObject borrowObject} after invoking 212 * this method on a pool will cause them to throw an {@link IllegalStateException}. 213 * </p> 214 * 215 * @throws Exception 216 */ 217 void close() throws Exception; 218 219 /** 220 * Sets the {@link KeyedPoolableObjectFactory factory} the pool uses 221 * to create new instances (optional operation). 222 * Trying to change the <code>factory</code> after a pool has been used will frequently 223 * throw an {@link UnsupportedOperationException}. It is up to the pool 224 * implementation to determine when it is acceptable to call this method. 225 * 226 * @param factory the {@link KeyedPoolableObjectFactory} used to create new instances. 227 * @throws IllegalStateException when the factory cannot be set at this time 228 * @throws UnsupportedOperationException when this implementation doesn't support the operation 229 * @deprecated to be removed in pool 2.0 230 */ 231 @Deprecated 232 void setFactory(KeyedPoolableObjectFactory<K, V> factory) throws IllegalStateException, UnsupportedOperationException; 233 }