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