| /*=============================================================================# |
| # Copyright (c) 2009, 2019 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.internal.rj.servi; |
| |
| import org.apache.commons.pool2.PooledObject; |
| import org.apache.commons.pool2.impl.DefaultEvictionPolicy; |
| import org.apache.commons.pool2.impl.EvictionConfig; |
| import org.apache.commons.pool2.impl.EvictionPolicy; |
| import org.apache.commons.pool2.impl.GenericObjectPool; |
| import org.apache.commons.pool2.impl.GenericObjectPoolConfig; |
| |
| import org.eclipse.statet.jcommons.collections.ImList; |
| |
| import org.eclipse.statet.rj.servi.pool.PoolConfig; |
| |
| |
| public class APool2 extends GenericObjectPool<APool2NodeHandler> { |
| |
| |
| private static final EvictionPolicy<APool2NodeHandler> EVICTION_POLICY= new DefaultEvictionPolicy<APool2NodeHandler>() { |
| |
| @Override |
| public boolean evict(final EvictionConfig config, |
| final PooledObject<APool2NodeHandler> underTest, final int idleCount) { |
| return (super.evict(config, underTest, idleCount) |
| || underTest.getObject().isEvictRequested(0) ); |
| } |
| |
| }; |
| |
| |
| private static GenericObjectPoolConfig<APool2NodeHandler> createAConfig(final PoolConfig config) { |
| final GenericObjectPoolConfig<APool2NodeHandler> aConfig= new GenericObjectPoolConfig<>(); |
| |
| aConfig.setLifo(true); |
| aConfig.setTestOnReturn(true); |
| aConfig.setTestWhileIdle(false); |
| aConfig.setTestOnBorrow(false); |
| aConfig.setBlockWhenExhausted(true); |
| aConfig.setMaxTotal(config.getMaxTotalCount()); |
| aConfig.setMaxWaitMillis(config.getMaxWaitTime()); |
| aConfig.setMinIdle(config.getMinIdleCount()); |
| aConfig.setMaxIdle(config.getMaxIdleCount()); |
| aConfig.setMinEvictableIdleTimeMillis(0L); |
| aConfig.setSoftMinEvictableIdleTimeMillis(config.getMinIdleTime()); |
| aConfig.setTimeBetweenEvictionRunsMillis(7500L); |
| aConfig.setNumTestsPerEvictionRun(-3); |
| |
| return aConfig; |
| } |
| |
| |
| private final APool2NodeFactory factory; |
| |
| private volatile boolean closing; |
| |
| |
| public APool2(final APool2NodeFactory factory, final PoolConfig config) { |
| super(factory, createAConfig(config)); |
| |
| factory.setPool(this); |
| this.factory= factory; |
| } |
| |
| |
| public void setConfig(final PoolConfig config) { |
| setConfig(createAConfig(config)); |
| } |
| |
| |
| public APool2NodeHandler borrowObject(final String client) throws Exception { |
| if (this.closing) { |
| throw new IllegalStateException("Pool not open"); |
| } |
| |
| this.factory.registerArgs(client); |
| try { |
| return super.borrowObject(); |
| } |
| finally { |
| this.factory.clearArgs(); |
| } |
| } |
| |
| @Override |
| public int getMinIdle() { |
| if (this.closing) { |
| return 0; |
| } |
| return super.getMinIdle(); |
| } |
| |
| public void close(final long evictionTimeout) { |
| this.closing= true; |
| |
| clear(); |
| |
| final long evictNanos= APool2NodeHandler.evictNanos(evictionTimeout); |
| final boolean evictDirect= (evictionTimeout == 0); |
| |
| final ImList<APool2NodeHandler> objects= this.factory.getAllObjects(); |
| for (final APool2NodeHandler poolObj : objects) { |
| poolObj.doEvict(evictNanos, evictDirect); |
| } |
| } |
| |
| @Override |
| public void evict() throws Exception { |
| int evicted; |
| do { |
| evicted= 0; |
| final long nanos= APool2NodeHandler.safeNanos(System.nanoTime()); |
| |
| final ImList<APool2NodeHandler> objects= this.factory.getAllObjects(); |
| for (final APool2NodeHandler poolObj : objects) { |
| if (poolObj.isEvictRequested(nanos)) { |
| try { |
| evicted++; |
| invalidateObject(poolObj); |
| } |
| catch (final Exception e) {} |
| } |
| } |
| } |
| while (evicted > 0); |
| |
| super.evict(); |
| |
| if (this.closing && this.factory.getNumAll() == 0) { |
| super.close(); |
| |
| this.factory.dispose(); |
| } |
| } |
| |
| @Override |
| public EvictionPolicy<APool2NodeHandler> getEvictionPolicy() { |
| return EVICTION_POLICY; |
| } |
| |
| } |