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 019/** 020 * An interface defining life-cycle methods for 021 * instances to be served by a {@link KeyedObjectPool}. 022 * <p> 023 * By contract, when an {@link KeyedObjectPool} 024 * delegates to a {@link KeyedPooledObjectFactory}, 025 * </p> 026 * <ol> 027 * <li> 028 * {@link #makeObject} is called whenever a new instance is needed. 029 * </li> 030 * <li> 031 * {@link #activateObject} is invoked on every instance that has been 032 * {@link #passivateObject passivated} before it is 033 * {@link KeyedObjectPool#borrowObject borrowed} from the pool. 034 * </li> 035 * <li> 036 * {@link #validateObject} may be invoked on {@link #activateObject activated} 037 * instances to make sure they can be 038 * {@link KeyedObjectPool#borrowObject borrowed} from the pool. 039 * {@code validateObject} may also be used to test an 040 * instance being {@link KeyedObjectPool#returnObject returned} to the pool 041 * before it is {@link #passivateObject passivated}. It will only be invoked 042 * on an activated instance. 043 * </li> 044 * <li> 045 * {@link #passivateObject passivateObject} 046 * is invoked on every instance when it is returned to the pool. 047 * </li> 048 * <li> 049 * {@link #destroyObject destroyObject} 050 * is invoked on every instance when it is being "dropped" from the 051 * pool (whether due to the response from {@code validateObject}, 052 * or for reasons specific to the pool implementation.) There is no 053 * guarantee that the instance being destroyed will 054 * be considered active, passive or in a generally consistent state. 055 * </li> 056 * </ol> 057 * {@link KeyedPooledObjectFactory} must be thread-safe. The only promise 058 * an {@link KeyedObjectPool} makes is that the same instance of an object will 059 * not be passed to more than one method of a 060 * {@code KeyedPoolableObjectFactory} at a time. 061 * <p> 062 * While clients of a {@link KeyedObjectPool} borrow and return instances of 063 * the underlying value type V, the factory methods act on instances of 064 * {@link PooledObject PooledObject<V>}. These are the object wrappers that 065 * pools use to track and maintain state informations about the objects that 066 * they manage. 067 * </p> 068 * 069 * @see KeyedObjectPool 070 * @see BaseKeyedPooledObjectFactory 071 * 072 * @param <K> The type of keys managed by this factory. 073 * @param <V> Type of element managed by this factory. 074 * 075 * @since 2.0 076 */ 077public interface KeyedPooledObjectFactory<K, V> { 078 079 /** 080 * Create an instance that can be served by the pool and 081 * wrap it in a {@link PooledObject} to be managed by the pool. 082 * 083 * @param key the key used when constructing the object 084 * 085 * @return a {@code PooledObject} wrapping an instance that can 086 * be served by the pool. 087 * 088 * @throws Exception if there is a problem creating a new instance, 089 * this will be propagated to the code requesting an object. 090 */ 091 PooledObject<V> makeObject(K key) throws Exception; 092 093 /** 094 * Destroy an instance no longer needed by the pool. 095 * <p> 096 * It is important for implementations of this method to be aware that there 097 * is no guarantee about what state {@code obj} will be in and the 098 * implementation should be prepared to handle unexpected errors. 099 * </p> 100 * <p> 101 * Also, an implementation must take in to consideration that instances lost 102 * to the garbage collector may never be destroyed. 103 * </p> 104 * 105 * @param key the key used when selecting the instance 106 * @param p a {@code PooledObject} wrapping the instance to be destroyed 107 * 108 * @throws Exception should be avoided as it may be swallowed by 109 * the pool implementation. 110 * 111 * @see #validateObject 112 * @see KeyedObjectPool#invalidateObject 113 */ 114 void destroyObject(K key, PooledObject<V> p) throws Exception; 115 116 /** 117 * Destroy an instance no longer needed by the pool, using the provided {@link DestroyMode}. 118 * 119 * @param key the key used when selecting the instance 120 * @param p a {@code PooledObject} wrapping the instance to be destroyed 121 * @param mode DestroyMode providing context to the factory 122 * 123 * @throws Exception should be avoided as it may be swallowed by 124 * the pool implementation. 125 * 126 * @see #validateObject 127 * @see KeyedObjectPool#invalidateObject 128 * @see #destroyObject(Object, PooledObject) 129 * @see DestroyMode 130 * @since 2.9.0 131 */ 132 default void destroyObject(final K key, final PooledObject<V> p, final DestroyMode mode) throws Exception { 133 destroyObject(key, p); 134 } 135 136 /** 137 * Ensures that the instance is safe to be returned by the pool. 138 * 139 * @param key the key used when selecting the object 140 * @param p a {@code PooledObject} wrapping the instance to be validated 141 * 142 * @return {@code false} if {@code obj} is not valid and should 143 * be dropped from the pool, {@code true} otherwise. 144 */ 145 boolean validateObject(K key, PooledObject<V> p); 146 147 /** 148 * Reinitialize an instance to be returned by the pool. 149 * 150 * @param key the key used when selecting the object 151 * @param p a {@code PooledObject} wrapping the instance to be activated 152 * 153 * @throws Exception if there is a problem activating {@code obj}, 154 * this exception may be swallowed by the pool. 155 * 156 * @see #destroyObject 157 */ 158 void activateObject(K key, PooledObject<V> p) throws Exception; 159 160 /** 161 * Uninitialize an instance to be returned to the idle object pool. 162 * 163 * @param key the key used when selecting the object 164 * @param p a {@code PooledObject} wrapping the instance to be passivated 165 * 166 * @throws Exception if there is a problem passivating {@code obj}, 167 * this exception may be swallowed by the pool. 168 * 169 * @see #destroyObject 170 */ 171 void passivateObject(K key, PooledObject<V> p) throws Exception; 172} 173