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 * @author Rodney Waldhoff 059 * @author Sandy McArthur 060 * @version $Revision: 778480 $ $Date: 2009-05-25 15:34:55 -0400 (Mon, 25 May 2009) $ 061 * @see KeyedPoolableObjectFactory 062 * @see KeyedObjectPoolFactory 063 * @see ObjectPool 064 * @see BaseKeyedObjectPool 065 * @since Pool 1.0 066 */ 067 public interface KeyedObjectPool { 068 /** 069 * Obtains an instance from this pool for the specified <code>key</code>. 070 * <p> 071 * Instances returned from this method will have been either newly created with 072 * {@link KeyedPoolableObjectFactory#makeObject makeObject} or will be a previously idle object and 073 * have been activated with {@link KeyedPoolableObjectFactory#activateObject activateObject} and 074 * then validated with {@link KeyedPoolableObjectFactory#validateObject validateObject}. 075 * <p> 076 * By contract, clients <strong>must</strong> return the borrowed object using 077 * {@link #returnObject returnObject}, {@link #invalidateObject invalidateObject}, or a related method 078 * as defined in an implementation or sub-interface, 079 * using a <code>key</code> that is {@link Object#equals equivalent} to the one used to 080 * borrow the instance in the first place. 081 * <p> 082 * The behaviour of this method when the pool has been exhausted 083 * is not strictly specified (although it may be specified by implementations). 084 * Older versions of this method would return <code>null</code> to indicate exhaustion, 085 * newer versions are encouraged to throw a {@link NoSuchElementException}. 086 * 087 * @param key the key used to obtain the object 088 * @return an instance from this pool. 089 * @throws IllegalStateException after {@link #close close} has been called on this pool 090 * @throws Exception when {@link KeyedPoolableObjectFactory#makeObject makeObject} throws an exception 091 * @throws NoSuchElementException when the pool is exhausted and cannot or will not return another instance 092 */ 093 Object borrowObject(Object key) throws Exception, NoSuchElementException, IllegalStateException; 094 095 /** 096 * Return an instance to the pool. 097 * By contract, <code>obj</code> <strong>must</strong> have been obtained 098 * using {@link #borrowObject borrowObject} 099 * or a related method as defined in an implementation 100 * or sub-interface 101 * using a <code>key</code> that is equivalent to the one used to 102 * borrow the instance in the first place. 103 * 104 * @param key the key used to obtain the object 105 * @param obj a {@link #borrowObject borrowed} instance to be returned. 106 * @throws Exception 107 */ 108 void returnObject(Object key, Object obj) throws Exception; 109 110 /** 111 * Invalidates an object from the pool 112 * By contract, <code>obj</code> <strong>must</strong> have been obtained 113 * using {@link #borrowObject borrowObject} 114 * or a related method as defined in an implementation 115 * or sub-interface 116 * using a <code>key</code> that is equivalent to the one used to 117 * borrow the <code>Object</code> in the first place. 118 * <p> 119 * This method should be used when an object that has been borrowed 120 * is determined (due to an exception or other problem) to be invalid. 121 * </p> 122 * 123 * @param key the key used to obtain the object 124 * @param obj a {@link #borrowObject borrowed} instance to be returned. 125 * @throws Exception 126 */ 127 void invalidateObject(Object key, Object obj) throws Exception; 128 129 /** 130 * Create an object using the {@link KeyedPoolableObjectFactory factory} or other 131 * implementation dependent mechanism, passivate it, and then place it in the idle object pool. 132 * <code>addObject</code> is useful for "pre-loading" a pool with idle objects 133 * (Optional operation). 134 * 135 * @param key the key a new instance should be added to 136 * @throws Exception when {@link KeyedPoolableObjectFactory#makeObject} fails. 137 * @throws IllegalStateException after {@link #close} has been called on this pool. 138 * @throws UnsupportedOperationException when this pool cannot add new idle objects. 139 */ 140 void addObject(Object key) throws Exception, IllegalStateException, UnsupportedOperationException; 141 142 /** 143 * Returns the number of instances 144 * corresponding to the given <code>key</code> 145 * currently idle in this pool (optional operation). 146 * Returns a negative value if this information is not available. 147 * 148 * @param key the key to query 149 * @return the number of instances corresponding to the given <code>key</code> currently idle in this pool or a negative value if unsupported 150 * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation 151 */ 152 int getNumIdle(Object key) throws UnsupportedOperationException; 153 154 /** 155 * Returns the number of instances 156 * currently borrowed from but not yet returned 157 * to the pool corresponding to the 158 * given <code>key</code> (optional operation). 159 * Returns a negative value if this information is not available. 160 * 161 * @param key the key to query 162 * @return the number of instances corresponding to the given <code>key</code> currently borrowed in this pool or a negative value if unsupported 163 * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation 164 */ 165 int getNumActive(Object key) throws UnsupportedOperationException; 166 167 /** 168 * Returns the total number of instances 169 * currently idle in this pool (optional operation). 170 * Returns a negative value if this information is not available. 171 * 172 * @return the total number of instances currently idle in this pool or a negative value if unsupported 173 * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation 174 */ 175 int getNumIdle() throws UnsupportedOperationException; 176 177 /** 178 * Returns the total number of instances 179 * current borrowed from this pool but not 180 * yet returned (optional operation). 181 * Returns a negative value if this information is not available. 182 * 183 * @return the total number of instances currently borrowed from this pool or a negative value if unsupported 184 * @throws UnsupportedOperationException <strong>deprecated</strong>: when this implementation doesn't support the operation 185 */ 186 int getNumActive() throws UnsupportedOperationException; 187 188 /** 189 * Clears the pool, removing all pooled instances (optional operation). 190 * Throws {@link UnsupportedOperationException} if the pool cannot be cleared. 191 * 192 * @throws UnsupportedOperationException when this implementation doesn't support the operation 193 */ 194 void clear() throws Exception, UnsupportedOperationException; 195 196 /** 197 * Clears the specified pool, removing all 198 * pooled instances corresponding to 199 * the given <code>key</code> (optional operation). 200 * Throws {@link UnsupportedOperationException} if the pool cannot be cleared. 201 * 202 * @param key the key to clear 203 * @throws UnsupportedOperationException when this implementation doesn't support the operation 204 */ 205 void clear(Object key) throws Exception, UnsupportedOperationException; 206 207 /** 208 * Close this pool, and free any resources associated with it. 209 * <p> 210 * Calling {@link #addObject addObject} or {@link #borrowObject borrowObject} after invoking 211 * this method on a pool will cause them to throw an {@link IllegalStateException}. 212 * </p> 213 * 214 * @throws Exception 215 */ 216 void close() throws Exception; 217 218 /** 219 * Sets the {@link KeyedPoolableObjectFactory factory} the pool uses 220 * to create new instances (optional operation). 221 * Trying to change the <code>factory</code> after a pool has been used will frequently 222 * throw an {@link UnsupportedOperationException}. It is up to the pool 223 * implementation to determine when it is acceptable to call this method. 224 * 225 * @param factory the {@link KeyedPoolableObjectFactory} used to create new instances. 226 * @throws IllegalStateException when the factory cannot be set at this time 227 * @throws UnsupportedOperationException when this implementation doesn't support the operation 228 */ 229 void setFactory(KeyedPoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException; 230 }