private <K, V> GenericKeyedObjectPool<K, V> getObjectPool(KeyedPoolableObjectFactory<K, V> factory, String poolName, long maxWaitMillis) { GenericKeyedObjectPool<K, V> pool = new GenericKeyedObjectPool<>(factory); pool.setTestOnBorrow(true); pool.setMaxActive(-1); pool.setMaxIdle(-1); pool.setTimeBetweenEvictionRunsMillis(MILLISECONDS.convert(5, MINUTES)); pool.setMinEvictableIdleTimeMillis(MILLISECONDS.convert(5, MINUTES)); pool.setMaxWait(maxWaitMillis); pool.setTestOnReturn(true); pool.setTestOnBorrow(true); try { ManagedGenericKeyedObjectPool mbean = new ManagedGenericKeyedObjectPool( pool, poolName); ManagementFactory.getPlatformMBeanServer() .registerMBean(mbean, mbean.getObjectName()); } catch (Exception e) { log.error("Could not register mbean for pool [{}]", poolName, e); } return pool; }
@Override public void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception { Socket socket = null; PrintWriter writer = null; try { socket = pool.borrowObject(address); writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), UTF_8), true); for (String formattedResult : messageFormatter.formatResults(results, server)) { log.debug("OpenTSDB Message: {}", formattedResult); writer.write("put " + formattedResult + "\n"); } } catch (ConnectException e) { log.error("Error while connecting to OpenTSDB", e); } finally { if (writer != null && writer.checkError()) { log.error("Error writing to OpenTSDB, clearing OpenTSDB socket pool"); pool.invalidateObject(address, socket); } else { pool.returnObject(address, socket); } } }
@Override public void close() throws LifecycleException { try { if (this.mbean != null) { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); mbs.unregisterMBean(this.mbean.getObjectName()); this.mbean = null; } if (this.pool != null) { pool.close(); this.pool = null; } } catch (Exception e) { throw new LifecycleException(e); } }
/** * Sets the configuration. * @param conf the new configuration to use. * @see GenericKeyedObjectPool.Config */ public synchronized void setConfig(GenericKeyedObjectPool.Config conf) { setMaxIdle(conf.maxIdle); setMaxActive(conf.maxActive); setMaxTotal(conf.maxTotal); setMinIdle(conf.minIdle); setMaxWait(conf.maxWait); setWhenExhaustedAction(conf.whenExhaustedAction); setTestOnBorrow(conf.testOnBorrow); setTestOnReturn(conf.testOnReturn); setTestWhileIdle(conf.testWhileIdle); setNumTestsPerEvictionRun(conf.numTestsPerEvictionRun); setMinEvictableIdleTimeMillis(conf.minEvictableIdleTimeMillis); setTimeBetweenEvictionRunsMillis(conf.timeBetweenEvictionRunsMillis); }
@Override public void start() throws LifecycleException { try { pool = new GenericKeyedObjectPool<>(new DatagramSocketFactory()); pool.setTestOnBorrow(true); pool.setMaxActive(-1); pool.setMaxIdle(-1); pool.setTimeBetweenEvictionRunsMillis(MILLISECONDS.convert(5, MINUTES)); pool.setMinEvictableIdleTimeMillis(MILLISECONDS.convert(5, MINUTES)); this.mbean = new ManagedGenericKeyedObjectPool((GenericKeyedObjectPool) pool, "StatsdConnectionPool"); ManagementFactory.getPlatformMBeanServer() .registerMBean(this.mbean, this.mbean.getObjectName()); } catch (Exception e) { throw new LifecycleException(e); } }
public void initializeService() { _pool = new GenericKeyedObjectPool(this); _pool.setMaxActive(Integer.parseInt(_propertySource.getPropertyValue("org.apache.tapestry.page-pool-max-active"))); _pool.setMaxIdle(Integer.parseInt(_propertySource.getPropertyValue("org.apache.tapestry.page-pool-max-idle"))); _pool.setMinIdle(Integer.parseInt(_propertySource.getPropertyValue("org.apache.tapestry.page-pool-min-idle"))); _pool.setMinEvictableIdleTimeMillis(MINUTE * Long.parseLong(_propertySource.getPropertyValue("org.apache.tapestry.page-pool-evict-idle-page-minutes"))); _pool.setTimeBetweenEvictionRunsMillis(MINUTE * Long.parseLong(_propertySource.getPropertyValue("org.apache.tapestry.page-pool-evict-thread-sleep-minutes"))); _pool.setTestWhileIdle(false); _pool.setTestOnBorrow(false); _pool.setTestOnReturn(false); }
public GroovyPool(String scriptBaseClass) { super(); Configuration conf = Configuration.getInstance(); try { pool = new GenericKeyedObjectPool<>(new GroovyPoolFactory(scriptBaseClass)); pool.setTestOnBorrow(true); pool.setMaxTotal(conf.getPropertyAsInteger("tec.expressions.pool.maxtotal",1000)); pool.setMaxActive(-1); pool.setMaxIdle(conf.getPropertyAsInteger("tec.expressions.pool.maxidle",-1)); pool.setWhenExhaustedAction(GenericKeyedObjectPool.WHEN_EXHAUSTED_BLOCK); pool.setTimeBetweenEvictionRunsMillis(30000); pool.setMinEvictableIdleTimeMillis(-1); } catch(Exception e) { logger.error("An error occurred while starting GroovyPool.", e); } }
public AlfrescoJavaMailSender() { transportPool.setMaxActive(-1); transportPool.setTestOnBorrow(true); transportPool.setTestOnReturn(true); transportPool.setTimeBetweenEvictionRunsMillis(DEAFULT_TIME_BETWEEN_EVICTION_RUNS); transportPool.setMinEvictableIdleTimeMillis(DEAFULT_TIME_BETWEEN_EVICTION_RUNS); }
public DefaultConfigurableKeyedObjectPool() { pool = new GenericKeyedObjectPool(); // NOTE: testOnBorrow MUST be FALSE. this is a bit of a design bug in // commons-pool since validate is used for both activation and passivation, // but has no way of knowing which way it is going. pool.setTestOnBorrow(false); pool.setTestOnReturn(true); }
public boolean matches(String pattern, String input) { Pattern compiled = null; try { compiled = (Pattern)_pool.borrowObject(pattern); return compiled.matcher(input).matches(); } catch (Exception e) { throw new ApplicationRuntimeException(e); } finally { try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { } } }
public RegexpMatcher() { _pool = new GenericKeyedObjectPool(_factory); _pool.setMinEvictableIdleTimeMillis(EVICT_IDLE_TIME); _pool.setTimeBetweenEvictionRunsMillis(SLEEP_TIME); }
@Override protected void doInitialise() throws InitialisationException { socketFactory = new UdpSocketFactory(); dispatcherSocketsPool.setFactory(socketFactory); dispatcherSocketsPool.setTestOnBorrow(true); dispatcherSocketsPool.setTestOnReturn(true); //There should only be one pooled instance per socket (key) dispatcherSocketsPool.setMaxActive(1); }
/** * Connect channel. * * @param serverAddress the server address * @return the channel */ private Channel doConnect(String serverAddress) { Channel channelToServer = channels.get(serverAddress); if (null != channelToServer && channelToServer.isActive()) { return channelToServer; } try { RegisterTMRequest registerTransactionManagerRequest = new RegisterTMRequest( applicationId, transactionServiceGroup); poolKeyMap.putIfAbsent(serverAddress, new NettyPoolKey(getTransactionRole(), serverAddress, registerTransactionManagerRequest)); channelToServer = nettyClientKeyPool.borrowObject(poolKeyMap.get(serverAddress)); } catch (Exception exx) { LOGGER.error("get channel from pool error.", exx); throw new FrameworkException("can not register TM,err:" + exx.getMessage()); } return channelToServer; }
/** * Creates a new pooling context source, setting up the DirContext object * factory and generic keyed object pool. */ public PoolingContextSource() { this.dirContextPoolableObjectFactory = new DirContextPoolableObjectFactory(); this.keyedObjectPool = new GenericKeyedObjectPool(); this.keyedObjectPool.setFactory(this.dirContextPoolableObjectFactory); }
@Override public void destroyChannel(String serverAddress, Channel channel) { if (null == channel) { return; } try { if (channel.equals(channels.get(serverAddress))) { channels.remove(serverAddress); } nettyClientKeyPool.returnObject(poolKeyMap.get(serverAddress), channel); } catch (Exception exx) { LOGGER.error("return channel to rpcPool error:" + exx.getMessage()); } }
/** * Create a new GenericKeyedObjectPool with the currently configured properties. * * @return GenericKeyedObjectPool with {@link GenericKeyedObjectPool.Config Configuration} determined by * current property settings */ public KeyedObjectPool<K, V> createPool() { return new GenericKeyedObjectPool<K, V>(_factory,_maxActive,_whenExhaustedAction,_maxWait,_maxIdle,_maxTotal,_minIdle,_testOnBorrow,_testOnReturn,_timeBetweenEvictionRunsMillis,_numTestsPerEvictionRun,_minEvictableIdleTimeMillis,_testWhileIdle,_lifo); }
@Override public void setMaxIdle(int maxIdle) { this.pool.setMaxIdle(maxIdle); }
@Override public void setMaxActive(int maxActive) { this.pool.setMaxActive(maxActive); }
/** * Configures the maximum number of requesters that can be concurrently active per endpoint * * @param maxActive max. number of active requesters */ public void setMaxRequestersActive(int maxActive) { this.requesters.setMaxActive(maxActive); // adjust maxIdle in tandem to avoid thrashing this.requesters.setMaxIdle(maxActive); // this tells the pool to expire some objects eventually if we start // running out. This happens if one is using a lot of dynamic endpoints. this.requesters.setMaxTotal(20 * maxActive); }
@Override protected void doInitialise() throws InitialisationException { socketFactory = new MulticastSocketFactory(); dispatcherSocketsPool.setFactory(socketFactory); dispatcherSocketsPool.setTestOnBorrow(false); dispatcherSocketsPool.setTestOnReturn(true); //For clarity, note that the max active value does not need to be 1 since you can have multiple //Multicast sockets bound to a single port //dispatcherSocketsPool.setMaxActive(1); }