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