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 */ 017package org.apache.commons.pool2; 018 019import java.io.Closeable; 020import java.util.NoSuchElementException; 021 022/** 023 * A pooling simple interface. 024 * <p> 025 * Example of use: 026 * </p> 027 * <pre style="border:solid thin; padding: 1ex;" 028 * > Object obj = <code style="color:#00C">null</code>; 029 * 030 * <code style="color:#00C">try</code> { 031 * obj = pool.borrowObject(); 032 * <code style="color:#00C">try</code> { 033 * <code style="color:#0C0">//...use the object...</code> 034 * } <code style="color:#00C">catch</code> (Exception e) { 035 * <code style="color:#0C0">// invalidate the object</code> 036 * pool.invalidateObject(obj); 037 * <code style="color:#0C0">// do not return the object to the pool twice</code> 038 * obj = <code style="color:#00C">null</code>; 039 * } <code style="color:#00C">finally</code> { 040 * <code style="color:#0C0">// make sure the object is returned to the pool</code> 041 * <code style="color:#00C">if</code> (<code style="color:#00C">null</code> != obj) { 042 * pool.returnObject(obj); 043 * } 044 * } 045 * } <code style="color:#00C">catch</code>(Exception e) { 046 * <code style="color:#0C0">// failed to borrow an object</code> 047 * }</pre> 048 * <p> 049 * See {@link BaseObjectPool} for a simple base implementation. 050 * </p> 051 * 052 * @param <T> Type of element pooled in this pool. 053 * 054 * 055 * @see PooledObjectFactory 056 * @see KeyedObjectPool 057 * @see BaseObjectPool 058 * 059 * @since 2.0 060 */ 061public interface ObjectPool<T> extends Closeable { 062 063 /** 064 * Creates an object using the {@link PooledObjectFactory factory} or other 065 * implementation dependent mechanism, passivate it, and then place it in 066 * the idle object pool. {@code addObject} is useful for "pre-loading" 067 * a pool with idle objects. (Optional operation). 068 * 069 * @throws Exception 070 * when {@link PooledObjectFactory#makeObject} fails. 071 * @throws IllegalStateException 072 * after {@link #close} has been called on this pool. 073 * @throws UnsupportedOperationException 074 * when this pool cannot add new idle objects. 075 */ 076 void addObject() throws Exception; 077 078 /** 079 * Calls {@link ObjectPool#addObject()} {@code count} 080 * number of times. 081 * 082 * @param count 083 * the number of idle objects to add. 084 * @throws Exception See {@link ObjectPool#addObject()}. 085 * @since 2.8.0 086 */ 087 default void addObjects(final int count) throws Exception { 088 for (int i = 0; i < count; i++) { 089 addObject(); 090 } 091 } 092 093 /** 094 * Borrows an instance from this pool. 095 * <p> 096 * Instances returned from this method will have been either newly created 097 * with {@link PooledObjectFactory#makeObject} or will be a previously 098 * idle object and have been activated with 099 * {@link PooledObjectFactory#activateObject} and then validated with 100 * {@link PooledObjectFactory#validateObject}. 101 * </p> 102 * <p> 103 * By contract, clients <strong>must</strong> return the borrowed instance 104 * using {@link #returnObject}, {@link #invalidateObject}, or a related 105 * method as defined in an implementation or sub-interface. 106 * </p> 107 * <p> 108 * The behavior of this method when the pool has been exhausted 109 * is not strictly specified (although it may be specified by 110 * implementations). 111 * </p> 112 * 113 * @return an instance from this pool. 114 * 115 * @throws IllegalStateException 116 * after {@link #close close} has been called on this pool. 117 * @throws Exception 118 * when {@link PooledObjectFactory#makeObject} throws an 119 * exception. 120 * @throws NoSuchElementException 121 * when the pool is exhausted and cannot or will not return 122 * another instance. 123 */ 124 T borrowObject() throws Exception; 125 126 /** 127 * Clears any objects sitting idle in the pool, releasing any associated 128 * resources (optional operation). Idle objects cleared must be 129 * {@link PooledObjectFactory#destroyObject(PooledObject)}. 130 * 131 * @throws UnsupportedOperationException 132 * if this implementation does not support the operation 133 * 134 * @throws Exception if the pool cannot be cleared 135 */ 136 void clear() throws Exception; 137 138 /** 139 * Closes this pool, and free any resources associated with it. 140 * <p> 141 * Calling {@link #addObject} or {@link #borrowObject} after invoking this 142 * method on a pool will cause them to throw an {@link IllegalStateException}. 143 * </p> 144 * <p> 145 * Implementations should silently fail if not all resources can be freed. 146 * </p> 147 */ 148 @Override 149 void close(); 150 151 /** 152 * Gets the number of instances currently borrowed from this pool. Returns 153 * a negative value if this information is not available. 154 * @return the number of instances currently borrowed from this pool. 155 */ 156 int getNumActive(); 157 158 /** 159 * Gets the number of instances currently idle in this pool. This may be 160 * considered an approximation of the number of objects that can be 161 * {@link #borrowObject borrowed} without creating any new instances. 162 * Returns a negative value if this information is not available. 163 * @return the number of instances currently idle in this pool. 164 */ 165 int getNumIdle(); 166 167 /** 168 * Invalidates an object from the pool. 169 * <p> 170 * By contract, {@code obj} <strong>must</strong> have been obtained 171 * using {@link #borrowObject} or a related method as defined in an 172 * implementation or sub-interface. 173 * </p> 174 * <p> 175 * This method should be used when an object that has been borrowed is 176 * determined (due to an exception or other problem) to be invalid. 177 * </p> 178 * 179 * @param obj a {@link #borrowObject borrowed} instance to be disposed. 180 * 181 * @throws Exception if the instance cannot be invalidated 182 */ 183 void invalidateObject(T obj) throws Exception; 184 185 /** 186 * Invalidates an object from the pool, using the provided 187 * {@link DestroyMode} 188 * <p> 189 * By contract, {@code obj} <strong>must</strong> have been obtained 190 * using {@link #borrowObject} or a related method as defined in an 191 * implementation or sub-interface. 192 * </p> 193 * <p> 194 * This method should be used when an object that has been borrowed is 195 * determined (due to an exception or other problem) to be invalid. 196 * </p> 197 * 198 * @param obj a {@link #borrowObject borrowed} instance to be disposed. 199 * @param destroyMode destroy activation context provided to the factory 200 * @throws Exception if the instance cannot be invalidated 201 * @since 2.9.0 202 */ 203 default void invalidateObject(final T obj, final DestroyMode destroyMode) throws Exception { 204 invalidateObject(obj); 205 } 206 207 /** 208 * Returns an instance to the pool. By contract, {@code obj} 209 * <strong>must</strong> have been obtained using {@link #borrowObject()} or 210 * a related method as defined in an implementation or sub-interface. 211 * 212 * @param obj a {@link #borrowObject borrowed} instance to be returned. 213 * 214 * @throws IllegalStateException 215 * if an attempt is made to return an object to the pool that 216 * is in any state other than allocated (i.e. borrowed). 217 * Attempting to return an object more than once or attempting 218 * to return an object that was never borrowed from the pool 219 * will trigger this exception. 220 * 221 * @throws Exception if an instance cannot be returned to the pool 222 */ 223 void returnObject(T obj) throws Exception; 224 225}