Java tutorial
/* * Copyright 2011-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package kr.debop4j.core.pool; import lombok.extern.slf4j.Slf4j; import org.apache.commons.pool.PoolableObjectFactory; import org.apache.commons.pool.impl.GenericObjectPool; /** * Apache common pool? Generic Pool (Jedis ? JedisPool ?) * * @author ? ( sunghyouk.bae@gmail.com ) * @since 13. 4. 8. 12:41 */ @Slf4j @SuppressWarnings("unchecked") public abstract class AbstractPool<T> implements AutoCloseable { protected GenericObjectPool pool; protected AbstractPool() { } public AbstractPool(final GenericObjectPool.Config poolConfig, PoolableObjectFactory<T> factory) { log.info(" pool ? ?... config=[{}]", poolConfig); pool = new GenericObjectPool<T>(factory, poolConfig); } /** ? . */ public T getResource() { log.trace("Pool? resource ..."); try { T result = (T) pool.borrowObject(); log.trace("Pool? resource . resource=[{}]", result); return result; } catch (Exception e) { log.error("Pool? ??? .", e); throw new RuntimeException(e); } } /** ? ? . */ public void returnResource(final T resource) { returnResourceObject(resource); } @SuppressWarnings("unchecked") protected void returnResourceObject(final Object resource) { log.trace("Pool? resource ... resource=[{}]", resource); try { pool.returnObject(resource); } catch (Exception e) { log.error(" Pool ? . resource=" + resource, e); throw new RuntimeException(e); } } /** ???? . */ public void returnBrokenResource(final T resource) { returnBrokenResourceObject(resource); } @SuppressWarnings("unchecked") protected void returnBrokenResourceObject(final Object resource) { try { pool.invalidateObject(resource); } catch (Exception e) { log.error("??? ? .", e); throw new RuntimeException(e); } } /** ? . ? . */ public void destroy() { log.trace("Pool? ..."); try { pool.close(); pool = null; } catch (Exception e) { log.error("pool? ? .", e); throw new RuntimeException(e); } log.info("Pool? ."); } public void close() throws Exception { destroy(); } }