-
Notifications
You must be signed in to change notification settings - Fork 52
Home
业余攻城狮 edited this page Sep 20, 2015
·
8 revisions
基于 Apache Commons Pool ™ 框架
连接池客户端 ( Apache Kafka & Apache Hbase & Redis )
API documentation is available at this
Use the KafkaConnectionPool need instantiate PoolConfig
and Properties
For example, the following
/* poolConfig */
PoolConfig config = new PoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000);
config.setTestOnBorrow(true);
/* properties */
Properties props = new Properties();
props.setProperty("metadata.broker.list", "localhost:9092");
props.setProperty("producer.type", "async");
props.setProperty("request.required.acks", "0");
props.setProperty("compression.codec", "snappy");
props.setProperty("batch.num.messages", "200");
/* connection pool */
KafkaConnectionPool pool = new KafkaConnectionPool(config, props);
/* pool getConnection */
Producer<byte[], byte[]> producer = pool.getConnection();
...
/* producer send */
producer.send(...);
/* pool returnConnection */
pool.returnConnection(producer);
Use the HbaseConnectionPool need instantiate PoolConfig
and Configuration
For example, the following
/* poolConfig */
PoolConfig config = new PoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000);
config.setTestOnBorrow(true);
/* configuration */
Configuration hbaseConfig = new Configuration();
hbaseConfig.set("hbase.zookeeper.quorum", "localhost");
hbaseConfig.set("hbase.zookeeper.property.clientPort", "2181");
hbaseConfig.set("hbase.master", "localhost:60000");
hbaseConfig.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
/* connection pool */
HbaseConnectionPool pool = new HbaseConnectionPool(config, hbaseConfig);
/* pool getConnection */
Connection conn = pool.getConnection();
/* conn getTable */
Table table = conn.getTable(TableName.valueOf("TableTest"));
...
/* table close */
table.close();
/* pool returnConnection */
pool.returnConnection(conn);
Use the RedisConnectionPool need instantiate PoolConfig
For example, the following
/* poolConfig */
PoolConfig config = new PoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000);
config.setTestOnBorrow(true);
/* connection pool */
RedisConnectionPool pool = new RedisConnectionPool(config, "localhost", 6379);
/* pool getConnection */
Jedis jedis = pool.getConnection();
...
/* pool getConnection */
pool.returnConnection(jedis);