org.apache.commons.pool2.impl.GenericObjectPool Java Examples

The following examples show how to use org.apache.commons.pool2.impl.GenericObjectPool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExtendedFormatParser.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public ExtendedFormatParser(
    ProtoConfigurableEntity.Context context,
    String readerId,
    OverrunReader reader,
    long readerOffset,
    int maxObjectLen,
    boolean retainOriginalText,
    String formatName,
    ExtendedFormatType formatType,
    GenericObjectPool<StringBuilder> currentLineBuilderPool,
    GenericObjectPool<StringBuilder> previousLineBuilderPool
) throws IOException {
  super(context,
      readerId,
      reader,
      readerOffset,
      maxObjectLen,
      retainOriginalText,
      -1,
      currentLineBuilderPool,
      previousLineBuilderPool
  );
  this.formatName = formatName;
  this.formatType = formatType;
}
 
Example #2
Source File: ElasticsearchUtils.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
public static Client getClient(String hostIps, int port, String clusterName){
    //System.out.println( "hostIps = [" + hostIps + "], port = [" + port + "], clusterName = [" + clusterName + "]" );
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();//对象池配置类,不写也可以,采用默认配置
    //poolConfig.setMaxTotal(list.length);//采用默认配置maxTotal是8,池中有8个client
    // System.out.println(Integer.parseInt( prop.get("dkSearch.commons-pool.MaxTotal") )  );
    poolConfig.setMaxTotal( Integer.parseInt( providerProp.getProperty("consumer.es.dkSearch.commons-pool.MaxTotal") ) );

    poolConfig.setMaxIdle(Integer.parseInt(providerProp.getProperty("consumer.es.dkSearch.commons-pool.MaxIdlel")));
    poolConfig.setMinIdle(Integer.parseInt(providerProp.getProperty("consumer.es.dkSearch.commons-pool.MinIdle")));
    poolConfig.setMaxWaitMillis(Integer.parseInt(providerProp.getProperty("consumer.es.dkSearch.commons-pool.MaxWaitMillis")));
    EsClientPoolFactory esClientPoolFactory = new EsClientPoolFactory(hostIps,port,clusterName);//要池化的对象的工厂类,这个是我们要实现的类
    GenericObjectPool<TransportClient> clientPool = new GenericObjectPool<TransportClient>(esClientPoolFactory, poolConfig);//利用对象工厂类和配置类生成对象池
    try {
        client = clientPool.borrowObject(); //从池中取一个对象
    } catch (Exception e) {
        e.printStackTrace();
    }
    clientPool.returnObject( (TransportClient) client );//使用完毕之后,归还对象
    return client;
}
 
Example #3
Source File: ESUtils.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
public static Client getClient(String hostIps, int port, String clusterName){
    //System.out.println( "hostIps = [" + hostIps + "], port = [" + port + "], clusterName = [" + clusterName + "]" );
    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();//对象池配置类,不写也可以,采用默认配置
    //poolConfig.setMaxTotal(list.length);//采用默认配置maxTotal是8,池中有8个client
    // System.out.println(Integer.parseInt( prop.get("dkSearch.commons-pool.MaxTotal") )  );
    poolConfig.setMaxTotal( Integer.parseInt( prop.get("dkSearch.commons-pool.MaxTotal") ) );

    poolConfig.setMaxIdle(Integer.parseInt(prop.get("dkSearch.commons-pool.MaxIdlel")));
    poolConfig.setMinIdle(Integer.parseInt(prop.get("dkSearch.commons-pool.MinIdle")));
    poolConfig.setMaxWaitMillis(Integer.parseInt(prop.get("dkSearch.commons-pool.MaxWaitMillis")));
    EsClientPoolFactory esClientPoolFactory = new EsClientPoolFactory(hostIps,port,clusterName);//要池化的对象的工厂类,这个是我们要实现的类
    GenericObjectPool<TransportClient> clientPool = new GenericObjectPool<>(esClientPoolFactory,poolConfig);//利用对象工厂类和配置类生成对象池
    try {
        client = clientPool.borrowObject(); //从池中取一个对象
    } catch (Exception e) {
        e.printStackTrace();
    }
    clientPool.returnObject( (TransportClient) client );//使用完毕之后,归还对象
    return client;
}
 
Example #4
Source File: TestPStmtPooling.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
@Test
public void testBatchUpdate() throws Exception {
    DriverManager.registerDriver(new TesterDriver());
    final ConnectionFactory connFactory = new DriverManagerConnectionFactory(
            "jdbc:apache:commons:testdriver","u1","p1");

    final PoolableConnectionFactory pcf =
        new PoolableConnectionFactory(connFactory, null);
    pcf.setPoolStatements(true);
    pcf.setDefaultReadOnly(Boolean.FALSE);
    pcf.setDefaultAutoCommit(Boolean.TRUE);
    final ObjectPool<PoolableConnection> connPool = new GenericObjectPool<>(pcf);
    pcf.setPool(connPool);

    final PoolingDataSource<?> ds = new PoolingDataSource<>(connPool);

    final Connection conn = ds.getConnection();
    final PreparedStatement ps = conn.prepareStatement("select 1 from dual");
    final Statement inner = ((DelegatingPreparedStatement) ps).getInnermostDelegate();
    // Check DBCP-372
    ps.addBatch();
    ps.close();
    conn.close();
    Assertions.assertFalse(inner.isClosed());
    ds.close();
}
 
Example #5
Source File: FastPFORVBCODEC.java    From RankSys with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public int dec(int[] in, int[] out, int outOffset, int len) {
    IntegerCODEC pfor;
    try {
        if (pool == null) {
            synchronized (this) {
                if (pool == null) {
                    pool = new GenericObjectPool<>(new FastPFORFactory());
                }
            }
        }
        pfor = pool.borrowObject();
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, null, ex);
        return -1;
    }
    int nInts = in[0];
    IntWrapper inputoffset = new IntWrapper(1);
    IntWrapper outputoffset = new IntWrapper(outOffset);
    pfor.uncompress(in, inputoffset, nInts, out, outputoffset);
    pool.returnObject(pfor);

    return inputoffset.get();
}
 
Example #6
Source File: PjedisPool.java    From pepper-metrics with Apache License 2.0 6 votes vote down vote up
public PjedisPool(final String host, final SSLSocketFactory sslSocketFactory,
                 final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
  URI uri = URI.create(host);
  if (JedisURIHelper.isValid(uri)) {
    String h = uri.getHost();
    int port = uri.getPort();
    String password = JedisURIHelper.getPassword(uri);
    int database = JedisURIHelper.getDBIndex(uri);
    boolean ssl = uri.getScheme().equals("rediss");
    this.internalPool = new GenericObjectPool<Jedis>(new PjedisFactory(h, port,
            Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, password, database, null, ssl,
            sslSocketFactory, sslParameters, hostnameVerifier),
            new GenericObjectPoolConfig());
  } else {
    this.internalPool = new GenericObjectPool<Jedis>(new PjedisFactory(host,
            Protocol.DEFAULT_PORT, Protocol.DEFAULT_TIMEOUT, Protocol.DEFAULT_TIMEOUT, null,
            Protocol.DEFAULT_DATABASE, null, false, null, null, null), new GenericObjectPoolConfig());
  }
}
 
Example #7
Source File: TestPStmtPooling.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
private DataSource createPoolingDataSource() throws Exception {
    DriverManager.registerDriver(new TesterDriver());
    final ConnectionFactory connFactory = new DriverManagerConnectionFactory(
            "jdbc:apache:commons:testdriver","u1","p1");

    final PoolableConnectionFactory pcf =
        new PoolableConnectionFactory(connFactory, null);
    pcf.setPoolStatements(true);
    pcf.setDefaultReadOnly(Boolean.FALSE);
    pcf.setDefaultAutoCommit(Boolean.TRUE);
    final ObjectPool<PoolableConnection> connPool = new GenericObjectPool<>(pcf);
    pcf.setPool(connPool);

    return new PoolingDataSource<>(connPool);

}
 
Example #8
Source File: EmbeddedConnectionProvider.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
    File databaseDir = new File(JiveGlobals.getHomeDirectory(), File.separator + "embedded-db");
    // If the database doesn't exist, create it.
    if (!databaseDir.exists()) {
        databaseDir.mkdirs();
    }

    try {
        serverURL = "jdbc:hsqldb:" + databaseDir.getCanonicalPath() + File.separator + "openfire";
    }
    catch (IOException ioe) {
        Log.error("EmbeddedConnectionProvider: Error starting connection pool: ", ioe);
    }
    final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(serverURL, "sa", "");
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    poolableConnectionFactory.setMaxConnLifetimeMillis((long) (0.5 * JiveConstants.DAY));

    final GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMinIdle(3);
    poolConfig.setMaxTotal(25);
    final GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, poolConfig);
    poolableConnectionFactory.setPool(connectionPool);
    dataSource = new PoolingDataSource<>(connectionPool);
}
 
Example #9
Source File: AbstractTestMode.java    From x-pipe with Apache License 2.0 6 votes vote down vote up
public AbstractTestMode(HostPort master, List<HostPort> slaves, int producerThreadNum, int producerIntervalMicro, int msgSize, long maxKeys) {
    this.master = master;
    this.slaves = slaves;
    this.producerThreadNum = producerThreadNum;
    this.producerIntervalMicro = producerIntervalMicro;
    this.maxKeys = maxKeys;

    producerThreadPool = Executors.newScheduledThreadPool(producerThreadNum,
            XpipeThreadFactory.create("ProducerThreadPool"));
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(producerThreadNum * 3);
    dataPool = new GenericObjectPool<>(new BytesFactory(msgSize), config);
    consumerThreadPool = Executors.newFixedThreadPool(slaves.size() * 2 + 1, XpipeThreadFactory.create("ConsumerThreadPool"));
    qpsCheckThreadPool = Executors.newScheduledThreadPool(1, XpipeThreadFactory.create("QpsCheckThreadPool"));

    masterPool = getJedisPool(master.getHost(), master.getPort(), producerThreadNum * 2, producerThreadNum);
    beginSend = new DelayManager(qpsCheckThreadPool, "beginsend", master.toString(), TIME_TOO_LONG_TO_LOG_MILLI, false);
    slaves.forEach((slave) -> allDelays.put(slave, new DelayManager(qpsCheckThreadPool, "delay", String.format("%s.%d.%d.%d", slave.getHost(), slave.getPort(), producerThreadNum, producerIntervalMicro), TIME_TOO_LONG_TO_LOG_MILLI)));
    slaves.forEach((slave) -> slavePools.put(slave, getJedisPool(slave.getHost(), slave.getPort(), 2, 2)));

}
 
Example #10
Source File: ConnectionPool.java    From AsyncDao with MIT License 6 votes vote down vote up
public ConnectionPool(PoolConfiguration configuration, Vertx vertx) {
    this.vertx = vertx;
    this.executionContext = VertxEventLoopExecutionContext.create(vertx);
    this.borrowMaxWaitMillis = configuration.getBorrowMaxWaitMillis();
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(configuration.getMaxTotal());
    config.setMaxIdle(configuration.getMaxIdle());
    config.setMinIdle(configuration.getMinIdle());
    config.setMaxWaitMillis(0);
    pool = new GenericObjectPool<>(new ConnectionFactory(configuration.getConfig(), vertx), config);
    try {
        pool.preparePool();
    } catch (Exception e) {
        throw new RuntimeException("{init connectionpool error: {}}", e);
    }
}
 
Example #11
Source File: KryoSerialization.java    From cuba with Apache License 2.0 6 votes vote down vote up
protected void initPool() {
    // assume that application context is already initialized
    Configuration configuration = AppBeans.get(Configuration.NAME);
    config = configuration.getConfig(KryoSerializationConfig.class);

    int poolSize = config.getMaxPoolSize();
    GenericObjectPoolConfig<Kryo> poolConfig = new GenericObjectPoolConfig<>();
    poolConfig.setMaxIdle(poolSize);
    poolConfig.setMaxTotal(poolSize);
    poolConfig.setMaxWaitMillis(config.getMaxBorrowWaitMillis());

    String jmxName = "kryo-" + AppContext.getProperty("cuba.webContextName");
    poolConfig.setJmxNamePrefix(jmxName);

    PooledObjectFactory<Kryo> factory = new KryoObjectFactory(this);
    pool = new GenericObjectPool<>(factory, poolConfig);
    log.debug("Kryo context pool created");
}
 
Example #12
Source File: PoolingDataSource.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a new instance backed by the given connection pool.
 *
 * @param pool
 *            the given connection pool.
 */
public PoolingDataSource(final ObjectPool<C> pool) {
    Objects.requireNonNull(pool, "Pool must not be null.");
    this.pool = pool;
    // Verify that pool's factory refers back to it. If not, log a warning and try to fix.
    if (this.pool instanceof GenericObjectPool<?>) {
        final PoolableConnectionFactory pcf = (PoolableConnectionFactory) ((GenericObjectPool<?>) this.pool)
                .getFactory();
        Objects.requireNonNull(pcf, "PoolableConnectionFactory must not be null.");
        if (pcf.getPool() != this.pool) {
            log.warn(Utils.getMessage("poolingDataSource.factoryConfig"));
            @SuppressWarnings("unchecked") // PCF must have a pool of PCs
            final ObjectPool<PoolableConnection> p = (ObjectPool<PoolableConnection>) this.pool;
            pcf.setPool(p);
        }
    }
}
 
Example #13
Source File: JdbcIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public DataSource apply(Void input) {
  return instances.computeIfAbsent(
      config.config,
      ignored -> {
        DataSource basicSource = config.apply(input);
        DataSourceConnectionFactory connectionFactory =
            new DataSourceConnectionFactory(basicSource);
        PoolableConnectionFactory poolableConnectionFactory =
            new PoolableConnectionFactory(connectionFactory, null);
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(1);
        poolConfig.setMinIdle(0);
        poolConfig.setMinEvictableIdleTimeMillis(10000);
        poolConfig.setSoftMinEvictableIdleTimeMillis(30000);
        GenericObjectPool connectionPool =
            new GenericObjectPool(poolableConnectionFactory, poolConfig);
        poolableConnectionFactory.setPool(connectionPool);
        poolableConnectionFactory.setDefaultAutoCommit(false);
        poolableConnectionFactory.setDefaultReadOnly(false);
        return new PoolingDataSource(connectionPool);
      });
}
 
Example #14
Source File: TestBasicDataSourceFactory.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
private void checkConnectionPoolProperties(final GenericObjectPool<PoolableConnection> cp) {
    assertEquals(10, cp.getMaxTotal());
    assertEquals(8, cp.getMaxIdle());
    assertEquals(0, cp.getMinIdle());
    assertEquals(500, cp.getMaxWaitMillis());
    assertEquals(5, cp.getNumIdle());
    assertTrue(cp.getTestOnBorrow());
    assertFalse(cp.getTestOnReturn());
    assertEquals(1000, cp.getTimeBetweenEvictionRunsMillis());
    assertEquals(2000, cp.getMinEvictableIdleTimeMillis());
    assertEquals(3000, cp.getSoftMinEvictableIdleTimeMillis());
    assertEquals(2, cp.getNumTestsPerEvictionRun());
    assertTrue(cp.getTestWhileIdle());
    assertTrue(cp.getRemoveAbandonedOnBorrow());
    assertTrue(cp.getRemoveAbandonedOnMaintenance());
    assertEquals(3000, cp.getRemoveAbandonedTimeout());
    assertTrue(cp.getLogAbandoned());
    assertTrue(cp.getLifo());
}
 
Example #15
Source File: BaseTestProxiedObjectPool.java    From commons-pool with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    log = new StringWriter();

    final PrintWriter pw = new PrintWriter(log);
    final AbandonedConfig abandonedConfig = new AbandonedConfig();
    abandonedConfig.setLogAbandoned(true);
    abandonedConfig.setRemoveAbandonedOnBorrow(true);
    abandonedConfig.setUseUsageTracking(true);
    abandonedConfig.setRemoveAbandonedTimeout(ABANDONED_TIMEOUT_SECS);
    abandonedConfig.setLogWriter(pw);

    final GenericObjectPoolConfig<TestObject> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(3);

    final PooledObjectFactory<TestObject> factory = new TestObjectFactory();

    @SuppressWarnings("resource")
    final ObjectPool<TestObject> innerPool =
            new GenericObjectPool<>(factory, config, abandonedConfig);

    pool = new ProxiedObjectPool<>(innerPool, getproxySource());
}
 
Example #16
Source File: DefaultSessionPool.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
public DefaultSessionPool(final ConnectionService connect, final X509TrustManager trust, final X509KeyManager key,
                          final VaultRegistry registry, final Cache<Path> cache, final TranscriptListener transcript,
                          final Host bookmark) {
    this.connect = connect;
    this.registry = registry;
    this.cache = cache;
    this.bookmark = bookmark;
    this.transcript = transcript;
    final GenericObjectPoolConfig<Session> configuration = new GenericObjectPoolConfig<Session>();
    configuration.setJmxEnabled(false);
    configuration.setEvictionPolicyClassName(CustomPoolEvictionPolicy.class.getName());
    configuration.setBlockWhenExhausted(true);
    configuration.setMaxWaitMillis(BORROW_MAX_WAIT_INTERVAL);
    this.pool = new GenericObjectPool<Session>(new PooledSessionFactory(connect, trust, key, cache, bookmark, registry), configuration);
    final AbandonedConfig abandon = new AbandonedConfig();
    abandon.setUseUsageTracking(true);
    this.pool.setAbandonedConfig(abandon);
}
 
Example #17
Source File: RabbitMQClient.java    From jweb-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
public RabbitMQClient(RabbitMQOptions rabbitMQOptions) {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setUsername(rabbitMQOptions.username);
    connectionFactory.setPassword(rabbitMQOptions.password);
    connectionFactory.setVirtualHost(rabbitMQOptions.virtualHost);
    connectionFactory.setHost(rabbitMQOptions.host);
    connectionFactory.setPort(rabbitMQOptions.port);
    connectionFactory.setSharedExecutor(workers);
    connectionFactory.setAutomaticRecoveryEnabled(true);

    GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    genericObjectPoolConfig.setMinIdle(8);
    genericObjectPoolConfig.setMaxTotal(18);
    genericObjectPoolConfig.setMinIdle(8);
    pool = new GenericObjectPool<>(new RabbitMQChannelFactory(createConnection(connectionFactory)), genericObjectPoolConfig);
}
 
Example #18
Source File: LiKafkaSchemaRegistry.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * @param props properties should contain property "kafka.schema.registry.url", and optionally
 * "kafka.schema.registry.max.cache.size" (default = 1000) and
 * "kafka.schema.registry.cache.expire.after.write.min" (default = 10).
 */
public LiKafkaSchemaRegistry(Properties props) {
  Preconditions.checkArgument(props.containsKey(KafkaSchemaRegistryConfigurationKeys.KAFKA_SCHEMA_REGISTRY_URL),
      String.format("Property %s not provided.", KafkaSchemaRegistryConfigurationKeys.KAFKA_SCHEMA_REGISTRY_URL));

  this.url = props.getProperty(KafkaSchemaRegistryConfigurationKeys.KAFKA_SCHEMA_REGISTRY_URL);
  this.namespaceOverride = KafkaReporterUtils.extractOverrideNamespace(props);
  this.switchTopicNames = PropertiesUtils.getPropAsBoolean(props, KafkaSchemaRegistryConfigurationKeys.KAFKA_SCHEMA_REGISTRY_SWITCH_NAME,
      KafkaSchemaRegistryConfigurationKeys.KAFKA_SCHEMA_REGISTRY_SWITCH_NAME_DEFAULT);

  int objPoolSize =
      Integer.parseInt(props.getProperty(ConfigurationKeys.KAFKA_SOURCE_WORK_UNITS_CREATION_THREADS,
          "" + ConfigurationKeys.KAFKA_SOURCE_WORK_UNITS_CREATION_DEFAULT_THREAD_COUNT));
  LOG.info("Create HttpClient pool with size " + objPoolSize);

  GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  config.setMaxTotal(objPoolSize);
  config.setMaxIdle(objPoolSize);
  this.httpClientPool = new GenericObjectPool<>(new HttpClientFactory(), config);
}
 
Example #19
Source File: TestPoolingDataSource.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setUp() throws Exception {
    final Properties props = new Properties();
    props.setProperty("user", "userName");
    props.setProperty("password", "password");
    final PoolableConnectionFactory factory =
        new PoolableConnectionFactory(
                new DriverConnectionFactory(new TesterDriver(),
                        "jdbc:apache:commons:testdriver", props),
                null);
    factory.setValidationQuery("SELECT DUMMY FROM DUAL");
    factory.setDefaultReadOnly(Boolean.TRUE);
    factory.setDefaultAutoCommit(Boolean.TRUE);
    pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);
    pool.setMaxTotal(getMaxTotal());
    pool.setMaxWaitMillis(getMaxWaitMillis());
    ds = new PoolingDataSource<>(pool);
    ds.setAccessToUnderlyingConnectionAllowed(true);
}
 
Example #20
Source File: WebDriverPool.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
/**
 * 如果需要使用WebDriverPool,则必须先调用这个init()方法
 *
 * @param config
 */
public static void init(WebDriverPoolConfig config) {

    webDriverPool = new GenericObjectPool<>(new WebDriverPooledFactory(config));
    webDriverPool.setMaxTotal(Integer.parseInt(System.getProperty(
            "webdriver.pool.max.total", "20"))); // 最多能放多少个对象
    webDriverPool.setMinIdle(Integer.parseInt(System.getProperty(
            "webdriver.pool.min.idle", "1")));   // 最少有几个闲置对象
    webDriverPool.setMaxIdle(Integer.parseInt(System.getProperty(
            "webdriver.pool.max.idle", "20"))); // 最多允许多少个闲置对象

    try {
        webDriverPool.preparePool();
    } catch (Exception e) {
        throw new SpiderException(e);
    }
}
 
Example #21
Source File: TestPoolingDataSource.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
/**
 * DBCP-412
 * Verify that omitting factory.setPool(pool) when setting up PDS does not
 * result in NPE.
 */
@Test
public void testFixFactoryConfig() throws Exception {
    final Properties props = new Properties();
    props.setProperty("user", "userName");
    props.setProperty("password", "password");
    final PoolableConnectionFactory f =
        new PoolableConnectionFactory(
                new DriverConnectionFactory(new TesterDriver(),
                        "jdbc:apache:commons:testdriver", props),
                null);
    f.setValidationQuery("SELECT DUMMY FROM DUAL");
    f.setDefaultReadOnly(Boolean.TRUE);
    f.setDefaultAutoCommit(Boolean.TRUE);
    final GenericObjectPool<PoolableConnection> p = new GenericObjectPool<>(f);
    p.setMaxTotal(getMaxTotal());
    p.setMaxWaitMillis(getMaxWaitMillis());
    ds = new PoolingDataSource<>(p);
    assertTrue(f.getPool().equals(p));
    ds.getConnection();
}
 
Example #22
Source File: HiveMetastoreClientPool.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor for {@link HiveMetastoreClientPool}.
 * By default we will using the default eviction strategy for the client pool. Client will be evicted if the following conditions are met:
 *  * <ul>
 *  * <li>the object has been idle longer than
 *  *     {@link GenericObjectPool#getMinEvictableIdleTimeMillis()}</li>
 *  * <li>there are more than {@link GenericObjectPool#getMinIdle()} idle objects in
 *  *     the pool and the object has been idle for longer than
 *  *     {@link GenericObjectPool#getSoftMinEvictableIdleTimeMillis()} </li>
 *  * </ul>
 * @deprecated It is recommended to use the static {@link #get} method instead. Use this constructor only if you
 *             different pool configurations are required.
 */
@Deprecated
public HiveMetastoreClientPool(Properties properties, Optional<String> metastoreURI) {
  this.hiveRegProps = new HiveRegProps(new State(properties));
  GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  config.setMaxTotal(this.hiveRegProps.getNumThreads());
  config.setMaxIdle(this.hiveRegProps.getNumThreads());

  String extraConfigTarget = properties.getProperty(POOL_HIVE_ADDITIONAL_CONFIG_TARGET, "");

  this.factory = new HiveMetaStoreClientFactory(metastoreURI);
  if (metastoreURI.isPresent() && StringUtils.isNotEmpty(extraConfigTarget)
      && metastoreURI.get().equals(extraConfigTarget)) {
    log.info("Setting additional hive config for metastore {}", extraConfigTarget);
    properties.forEach((key, value) -> {
      String configKey = key.toString();
      if (configKey.startsWith(POOL_HIVE_ADDITIONAL_CONFIG_PREFIX) && !configKey.equals(
          POOL_HIVE_ADDITIONAL_CONFIG_TARGET)) {
        log.info("Setting additional hive config {}={}", configKey.substring(POOL_HIVE_ADDITIONAL_CONFIG_PREFIX.length()),
            value.toString());
        this.factory.getHiveConf().set(configKey.substring(POOL_HIVE_ADDITIONAL_CONFIG_PREFIX.length()), value.toString());
      }
    });
  }
  this.pool = new GenericObjectPool<>(this.factory, config);
  //Set the eviction policy for the client pool
  this.pool.setEvictionPolicyClassName(properties.getProperty(POOL_EVICTION_POLICY_CLASS_NAME, DEFAULT_POOL_EVICTION_POLICY_CLASS_NAME));
  this.pool.setMinEvictableIdleTimeMillis(PropertiesUtils.getPropAsLong(properties, POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS, DEFAULT_POOL_MIN_EVICTABLE_IDLE_TIME_MILLIS));
  this.pool.setTimeBetweenEvictionRunsMillis(PropertiesUtils.getPropAsLong(properties, POOL_TIME_BETWEEN_EVICTION_MILLIS, DEFAULT_POOL_TIME_BETWEEN_EVICTION_MILLIS));
  this.hiveConf = this.factory.getHiveConf();
}
 
Example #23
Source File: DataParserFactory.java    From datacollector with Apache License 2.0 5 votes vote down vote up
protected GenericObjectPool<StringBuilder> getStringBuilderPool(Settings settings) {
  int maxRecordLen = getSettings().getMaxRecordLen();
  int poolSize = getSettings().getStringBuilderPoolSize();
  GenericObjectPoolConfig stringBuilderPoolConfig = new GenericObjectPoolConfig();
  stringBuilderPoolConfig.setMaxTotal(poolSize);
  stringBuilderPoolConfig.setMinIdle(poolSize);
  stringBuilderPoolConfig.setMaxIdle(poolSize);
  stringBuilderPoolConfig.setBlockWhenExhausted(false);
  return new GenericObjectPool<>(
    new StringBuilderPoolFactory(maxRecordLen > 0 ? maxRecordLen : DEFAULT_MAX_RECORD_LENGTH),
    stringBuilderPoolConfig
  );
}
 
Example #24
Source File: ReactorRabbitMQChannelPool.java    From james-project with Apache License 2.0 5 votes vote down vote up
public ReactorRabbitMQChannelPool(Mono<Connection> connectionMono, int poolSize) {
    this.connectionMono = connectionMono;
    ChannelFactory channelFactory = new ChannelFactory(connectionMono);

    GenericObjectPoolConfig<Channel> config = new GenericObjectPoolConfig<>();
    config.setMaxTotal(poolSize);
    this.pool = new GenericObjectPool<>(channelFactory, config);
    this.borrowedChannels = new ConcurrentSkipListSet<>(Comparator.comparingInt(System::identityHashCode));
}
 
Example #25
Source File: PerUserPoolDataSource.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
private synchronized void registerPool(final String userName, final String password)
        throws NamingException, SQLException {

    final ConnectionPoolDataSource cpds = testCPDS(userName, password);

    // Set up the factory we will use (passing the pool associates
    // the factory with the pool, so we do not have to do so
    // explicitly)
    final CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, getValidationQuery(),
            getValidationQueryTimeout(), isRollbackAfterValidation(), userName, password);
    factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());

    // Create an object pool to contain our PooledConnections
    final GenericObjectPool<PooledConnectionAndInfo> pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);
    pool.setBlockWhenExhausted(getPerUserBlockWhenExhausted(userName));
    pool.setEvictionPolicyClassName(getPerUserEvictionPolicyClassName(userName));
    pool.setLifo(getPerUserLifo(userName));
    pool.setMaxIdle(getPerUserMaxIdle(userName));
    pool.setMaxTotal(getPerUserMaxTotal(userName));
    pool.setMaxWaitMillis(getPerUserMaxWaitMillis(userName));
    pool.setMinEvictableIdleTimeMillis(getPerUserMinEvictableIdleTimeMillis(userName));
    pool.setMinIdle(getPerUserMinIdle(userName));
    pool.setNumTestsPerEvictionRun(getPerUserNumTestsPerEvictionRun(userName));
    pool.setSoftMinEvictableIdleTimeMillis(getPerUserSoftMinEvictableIdleTimeMillis(userName));
    pool.setTestOnCreate(getPerUserTestOnCreate(userName));
    pool.setTestOnBorrow(getPerUserTestOnBorrow(userName));
    pool.setTestOnReturn(getPerUserTestOnReturn(userName));
    pool.setTestWhileIdle(getPerUserTestWhileIdle(userName));
    pool.setTimeBetweenEvictionRunsMillis(getPerUserTimeBetweenEvictionRunsMillis(userName));

    pool.setSwallowedExceptionListener(new SwallowedExceptionLogger(log));

    final Object old = managers.put(getPoolKey(userName), factory);
    if (old != null) {
        throw new IllegalStateException("Pool already contains an entry for this user/password: " + userName);
    }
}
 
Example #26
Source File: DefaultSessionPool.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
public DefaultSessionPool(final ConnectionService connect, final VaultRegistry registry, final Cache<Path> cache,
                          final TranscriptListener transcript, final Host bookmark, final GenericObjectPool<Session> pool) {
    this.connect = connect;
    this.transcript = transcript;
    this.cache = cache;
    this.bookmark = bookmark;
    this.registry = registry;
    this.pool = pool;
}
 
Example #27
Source File: HessianSerializePool.java    From Raincat with GNU Lesser General Public License v3.0 5 votes vote down vote up
public HessianSerializePool(final int maxTotal, final int minIdle, final long maxWaitMillis, final long minEvictableIdleTimeMillis) {
    hessianPool = new GenericObjectPool<>(new HessianSerializeFactory());
    GenericObjectPoolConfig config = new GenericObjectPoolConfig();
    config.setMaxTotal(maxTotal);
    config.setMinIdle(minIdle);
    config.setMaxWaitMillis(maxWaitMillis);
    config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
    hessianPool.setConfig(config);
}
 
Example #28
Source File: Pool.java    From feeyo-redisproxy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void initPool(final GenericObjectPoolConfig poolConfig, PooledObjectFactory<T> factory) {

    if (this.internalPool != null) {
      try {
        closeInternalPool();
      } catch (Exception e) {
      }
    }

    this.internalPool = new GenericObjectPool<T>(factory, poolConfig);
  }
 
Example #29
Source File: DebugManagedDataSource.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
@Override
public Connection getConnection() throws SQLException {
    if (Debug.verboseOn()) {
        if (super.getPool() instanceof GenericObjectPool) {
            GenericObjectPool<?> objectPool = (GenericObjectPool<?>)super.getPool();
            Debug.logVerbose("Borrowing a connection from the pool; used/idle/total: " + objectPool.getNumActive() + "/" + objectPool.getNumIdle() + "/" + (objectPool.getNumActive() + objectPool.getNumIdle()) + "; min idle/max idle/max total: " + objectPool.getMinIdle() + "/" + objectPool.getMaxIdle() + "/" + objectPool.getMaxTotal(), module);
        } else {
            if (Debug.verboseOn()) Debug.logVerbose("Borrowing a connection from the pool; used/idle/total: " + super.getPool().getNumActive() + "/" + super.getPool().getNumIdle() + "/" + (super.getPool().getNumActive() + super.getPool().getNumIdle()), module);
        }
    }
    return super.getConnection();
}
 
Example #30
Source File: TestTextDataParserFactory.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testStringBuilderPoolException() throws Exception {

  // Parser with default string builder pool config
  DataParserFactoryBuilder dataParserFactoryBuilder = new DataParserFactoryBuilder(getContext(), DataParserFormat.TEXT);
  WrapperDataParserFactory factory = (WrapperDataParserFactory) dataParserFactoryBuilder
    .setMaxDataLen(1000)
    .setConfig(TextDataParserFactory.USE_CUSTOM_DELIMITER_KEY, true)
    .setConfig(TextDataParserFactory.CUSTOM_DELIMITER_KEY, "\\\\r\\\\n")
    .build();

  TextDataParserFactory textDataParserFactory = (TextDataParserFactory) factory.getFactory();
  GenericObjectPool<StringBuilder> stringBuilderPool = textDataParserFactory.getStringBuilderPool();
  Assert.assertNotNull(stringBuilderPool);
  Assert.assertEquals(1, stringBuilderPool.getMaxIdle());
  Assert.assertEquals(1, stringBuilderPool.getMinIdle());
  Assert.assertEquals(1, stringBuilderPool.getMaxTotal());
  Assert.assertEquals(0, stringBuilderPool.getNumIdle());
  Assert.assertEquals(0, stringBuilderPool.getNumActive());

  factory.getParser("id", "Hello\\r\\nBye");

  Assert.assertEquals(0, stringBuilderPool.getNumIdle());
  Assert.assertEquals(1, stringBuilderPool.getNumActive());

  try {
    factory.getParser("id", "Hello\\r\\nBye");
    Assert.fail("Expected IOException which wraps NoSuchElementException since pool is empty");
  } catch (DataParserException e) {
    Assert.assertTrue(e.getCause() instanceof IOException);
    Assert.assertTrue(e.getCause().getCause() instanceof NoSuchElementException);
  }

}