1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 module hunt.pool.KeyedPooledObjectFactory; 18 19 /** 20 * An interface defining life-cycle methods for 21 * instances to be served by a {@link KeyedObjectPool}. 22 * <p> 23 * By contract, when an {@link KeyedObjectPool} 24 * delegates to a {@link KeyedPooledObjectFactory}, 25 * </p> 26 * <ol> 27 * <li> 28 * {@link #makeObject} is called whenever a new instance is needed. 29 * </li> 30 * <li> 31 * {@link #activateObject} is invoked on every instance that has been 32 * {@link #passivateObject passivated} before it is 33 * {@link KeyedObjectPool#borrowObject borrowed} from the pool. 34 * </li> 35 * <li> 36 * {@link #validateObject} may be invoked on {@link #activateObject activated} 37 * instances to make sure they can be 38 * {@link KeyedObjectPool#borrowObject borrowed} from the pool. 39 * <code>validateObject</code> may also be used to test an 40 * instance being {@link KeyedObjectPool#returnObject returned} to the pool 41 * before it is {@link #passivateObject passivated}. It will only be invoked 42 * on an activated instance. 43 * </li> 44 * <li> 45 * {@link #passivateObject passivateObject} 46 * is invoked on every instance when it is returned to the pool. 47 * </li> 48 * <li> 49 * {@link #destroyObject destroyObject} 50 * is invoked on every instance when it is being "dropped" from the 51 * pool (whether due to the response from <code>validateObject</code>, 52 * or for reasons specific to the pool implementation.) There is no 53 * guarantee that the instance being destroyed will 54 * be considered active, passive or in a generally consistent state. 55 * </li> 56 * </ol> 57 * {@link KeyedPooledObjectFactory} must be thread-safe. The only promise 58 * an {@link KeyedObjectPool} makes is that the same instance of an object will 59 * not be passed to more than one method of a 60 * <code>KeyedPoolableObjectFactory</code> at a time. 61 * <p> 62 * While clients of a {@link KeyedObjectPool} borrow and return instances of 63 * the underlying value type V, the factory methods act on instances of 64 * {@link PooledObject PooledObject<V>}. These are the object wrappers that 65 * pools use to track and maintain state informations about the objects that 66 * they manage. 67 * </p> 68 * 69 * @see KeyedObjectPool 70 * @see BaseKeyedPooledObjectFactory 71 * 72 * @param <K> The type of keys managed by this factory. 73 * @param <V> Type of element managed by this factory. 74 * 75 */ 76 interface KeyedPooledObjectFactory(K, V) { 77 78 /** 79 * Create an instance that can be served by the pool and 80 * wrap it in a {@link PooledObject} to be managed by the pool. 81 * 82 * @param key the key used when constructing the object 83 * 84 * @return a {@code PooledObject} wrapping an instance that can 85 * be served by the pool. 86 * 87 * @throws Exception if there is a problem creating a new instance, 88 * this will be propagated to the code requesting an object. 89 */ 90 PooledObject!(V) makeObject(K key); 91 92 /** 93 * Destroy an instance no longer needed by the pool. 94 * <p> 95 * It is important for implementations of this method to be aware that there 96 * is no guarantee about what state <code>obj</code> will be in and the 97 * implementation should be prepared to handle unexpected errors. 98 * </p> 99 * <p> 100 * Also, an implementation must take in to consideration that instances lost 101 * to the garbage collector may never be destroyed. 102 * </p> 103 * 104 * @param key the key used when selecting the instance 105 * @param p a {@code PooledObject} wrapping the instance to be destroyed 106 * 107 * @throws Exception should be avoided as it may be swallowed by 108 * the pool implementation. 109 * 110 * @see #validateObject 111 * @see KeyedObjectPool#invalidateObject 112 */ 113 void destroyObject(K key, PooledObject!(V) p); 114 115 /** 116 * Ensures that the instance is safe to be returned by the pool. 117 * 118 * @param key the key used when selecting the object 119 * @param p a {@code PooledObject} wrapping the instance to be validated 120 * 121 * @return <code>false</code> if <code>obj</code> is not valid and should 122 * be dropped from the pool, <code>true</code> otherwise. 123 */ 124 bool validateObject(K key, PooledObject!(V) p); 125 126 /** 127 * Reinitialize an instance to be returned by the pool. 128 * 129 * @param key the key used when selecting the object 130 * @param p a {@code PooledObject} wrapping the instance to be activated 131 * 132 * @throws Exception if there is a problem activating <code>obj</code>, 133 * this exception may be swallowed by the pool. 134 * 135 * @see #destroyObject 136 */ 137 void activateObject(K key, PooledObject!(V) p); 138 139 /** 140 * Uninitialize an instance to be returned to the idle object pool. 141 * 142 * @param key the key used when selecting the object 143 * @param p a {@code PooledObject} wrapping the instance to be passivated 144 * 145 * @throws Exception if there is a problem passivating <code>obj</code>, 146 * this exception may be swallowed by the pool. 147 * 148 * @see #destroyObject 149 */ 150 void passivateObject(K key, PooledObject!(V) p); 151 } 152