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 pooling interface. 024 * <p> 025 * <code>ObjectPool</code> defines a trivially simple pooling interface. The only 026 * required methods are {@link #borrowObject borrowObject}, {@link #returnObject returnObject} 027 * and {@link #invalidateObject invalidateObject}. 028 * </p> 029 * <p> 030 * Example of use: 031 * <pre style="border:solid thin; padding: 1ex;" 032 * > Object obj = <code style="color:#00C">null</code>; 033 * 034 * <code style="color:#00C">try</code> { 035 * obj = pool.borrowObject(); 036 * <code style="color:#00C">try</code> { 037 * <code style="color:#0C0">//...use the object...</code> 038 * } <code style="color:#00C">catch</code>(Exception e) { 039 * <code style="color:#0C0">// invalidate the object</code> 040 * pool.invalidateObject(obj); 041 * <code style="color:#0C0">// do not return the object to the pool twice</code> 042 * obj = <code style="color:#00C">null</code>; 043 * } <code style="color:#00C">finally</code> { 044 * <code style="color:#0C0">// make sure the object is returned to the pool</code> 045 * <code style="color:#00C">if</code>(<code style="color:#00C">null</code> != obj) { 046 * pool.returnObject(obj); 047 * } 048 * } 049 * } <code style="color:#00C">catch</code>(Exception e) { 050 * <code style="color:#0C0">// failed to borrow an object</code> 051 * }</pre> 052 * </p> 053 * 054 * <p>See {@link BaseObjectPool} for a simple base implementation.</p> 055 * 056 * @author Rodney Waldhoff 057 * @author Sandy McArthur 058 * @version $Revision: 962893 $ $Date: 2010-07-10 10:37:27 -0700 (Sat, 10 Jul 2010) $ 059 * @see PoolableObjectFactory 060 * @see ObjectPoolFactory 061 * @see KeyedObjectPool 062 * @see BaseObjectPool 063 * @since Pool 1.0 064 */ 065 public interface ObjectPool { 066 /** 067 * Obtains an instance from this pool. 068 * <p> 069 * Instances returned from this method will have been either newly created with 070 * {@link PoolableObjectFactory#makeObject makeObject} or will be a previously idle object and 071 * have been activated with {@link PoolableObjectFactory#activateObject activateObject} and 072 * then validated with {@link PoolableObjectFactory#validateObject validateObject}. 073 * </p> 074 * <p> 075 * By contract, clients <strong>must</strong> return the borrowed instance using 076 * {@link #returnObject returnObject}, {@link #invalidateObject invalidateObject}, or a related method 077 * as defined in an implementation or sub-interface. 078 * </p> 079 * <p> 080 * The behaviour of this method when the pool has been exhausted 081 * is not strictly specified (although it may be specified by implementations). 082 * Older versions of this method would return <code>null</code> to indicate exhaustion, 083 * newer versions are encouraged to throw a {@link NoSuchElementException}. 084 * </p> 085 * 086 * @return an instance from this pool. 087 * @throws IllegalStateException after {@link #close close} has been called on this pool. 088 * @throws Exception when {@link PoolableObjectFactory#makeObject makeObject} throws an exception. 089 * @throws NoSuchElementException when the pool is exhausted and cannot or will not return another instance. 090 */ 091 Object borrowObject() throws Exception, NoSuchElementException, IllegalStateException; 092 093 /** 094 * Return an instance to the pool. 095 * By contract, <code>obj</code> <strong>must</strong> have been obtained 096 * using {@link #borrowObject() borrowObject} 097 * or a related method as defined in an implementation 098 * or sub-interface. 099 * 100 * @param obj a {@link #borrowObject borrowed} instance to be returned. 101 * @throws Exception 102 */ 103 void returnObject(Object obj) throws Exception; 104 105 /** 106 * <p>Invalidates an object from the pool.</p> 107 * 108 * <p>By contract, <code>obj</code> <strong>must</strong> have been obtained 109 * using {@link #borrowObject borrowObject} or a related method as defined in 110 * an implementation or sub-interface.</p> 111 * 112 * <p>This method should be used when an object that has been borrowed 113 * is determined (due to an exception or other problem) to be invalid.</p> 114 * 115 * @param obj a {@link #borrowObject borrowed} instance to be disposed. 116 * @throws Exception 117 */ 118 void invalidateObject(Object obj) throws Exception; 119 120 /** 121 * Create an object using the {@link PoolableObjectFactory factory} or other 122 * implementation dependent mechanism, passivate it, and then place it in the idle object pool. 123 * <code>addObject</code> is useful for "pre-loading" a pool with idle objects. 124 * (Optional operation). 125 * 126 * @throws Exception when {@link PoolableObjectFactory#makeObject} fails. 127 * @throws IllegalStateException after {@link #close} has been called on this pool. 128 * @throws UnsupportedOperationException when this pool cannot add new idle objects. 129 */ 130 void addObject() throws Exception, IllegalStateException, UnsupportedOperationException; 131 132 /** 133 * Return the number of instances 134 * currently idle in this pool (optional operation). 135 * This may be considered an approximation of the number 136 * of objects that can be {@link #borrowObject borrowed} 137 * without creating any new instances. 138 * Returns a negative value if this information is not available. 139 * 140 * @return the number of instances currently idle in this pool or a negative value if unsupported 141 * @throws UnsupportedOperationException <strong>deprecated</strong>: if this implementation does not support the operation 142 */ 143 int getNumIdle() throws UnsupportedOperationException; 144 145 /** 146 * Return the number of instances 147 * currently borrowed from this pool 148 * (optional operation). 149 * Returns a negative value if this information is not available. 150 * 151 * @return the number of instances currently borrowed from this pool or a negative value if unsupported 152 * @throws UnsupportedOperationException <strong>deprecated</strong>: if this implementation does not support the operation 153 */ 154 int getNumActive() throws UnsupportedOperationException; 155 156 /** 157 * Clears any objects sitting idle in the pool, releasing any 158 * associated resources (optional operation). 159 * Idle objects cleared must be {@link PoolableObjectFactory#destroyObject(Object) destroyed}. 160 * 161 * @throws UnsupportedOperationException if this implementation does not support the operation 162 */ 163 void clear() throws Exception, UnsupportedOperationException; 164 165 /** 166 * Close this pool, and free any resources associated with it. 167 * <p> 168 * Calling {@link #addObject} or {@link #borrowObject} after invoking 169 * this method on a pool will cause them to throw an 170 * {@link IllegalStateException}. 171 * </p> 172 * 173 * @throws Exception <strong>deprecated</strong>: implementations should silently fail if not all resources can be freed. 174 */ 175 void close() throws Exception; 176 177 /** 178 * Sets the {@link PoolableObjectFactory factory} this pool uses 179 * to create new instances (optional operation). Trying to change 180 * the <code>factory</code> after a pool has been used will frequently 181 * throw an {@link UnsupportedOperationException}. It is up to the pool 182 * implementation to determine when it is acceptable to call this method. 183 * 184 * @param factory the {@link PoolableObjectFactory} used to create new instances. 185 * @throws IllegalStateException when the factory cannot be set at this time 186 * @throws UnsupportedOperationException if this implementation does not support the operation 187 * @deprecated to be removed in pool 2.0 188 */ 189 void setFactory(PoolableObjectFactory factory) throws IllegalStateException, UnsupportedOperationException; 190 }