├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── pom.xml └── src ├── main ├── java │ └── org │ │ └── darkphoenixs │ │ └── pool │ │ ├── ConnectionException.java │ │ ├── ConnectionFactory.java │ │ ├── ConnectionPool.java │ │ ├── PoolBase.java │ │ ├── PoolConfig.java │ │ ├── hbase │ │ ├── HbaseConfig.java │ │ ├── HbaseConnectionFactory.java │ │ ├── HbaseConnectionPool.java │ │ └── HbaseSharedConnPool.java │ │ ├── http │ │ ├── HttpConfig.java │ │ ├── HttpConnectionFactory.java │ │ └── HttpConnectionPool.java │ │ ├── jdbc │ │ ├── JdbcConfig.java │ │ ├── JdbcConnectionFactory.java │ │ └── JdbcConnectionPool.java │ │ ├── kafka │ │ ├── KafkaConfig.java │ │ ├── KafkaConnectionFactory.java │ │ ├── KafkaConnectionPool.java │ │ └── KafkaSharedConnPool.java │ │ ├── redis │ │ ├── RedisClusterConnPool.java │ │ ├── RedisConfig.java │ │ ├── RedisConnectionFactoryOld.java │ │ ├── RedisConnectionPool.java │ │ ├── RedisConnectionPoolOld.java │ │ ├── RedisSentinelConnPool.java │ │ ├── RedisSentinelConnPoolOld.java │ │ ├── RedisShardedConnPool.java │ │ └── RedisShardedConnPoolOld.java │ │ └── socket │ │ ├── SocketConfig.java │ │ ├── SocketConnectionFactory.java │ │ └── SocketConnectionPool.java └── resources │ └── META-INF │ ├── LICENSE.txt │ └── NOTICE.txt └── test ├── java └── org │ └── darkphoenixs │ └── pool │ ├── ConnectionExceptionTest.java │ ├── ConnectionFactoryTest.java │ ├── ConnectionPoolTest.java │ ├── PoolBaseTest.java │ ├── PoolConfigTest.java │ ├── hbase │ ├── HbaseConfigTest.java │ ├── HbaseConnectionFactoryTest.java │ ├── HbaseConnectionPoolTest.java │ ├── HbaseSharedConnPoolTest.java │ └── HbaseTest.java │ ├── http │ ├── HttpConfigTest.java │ ├── HttpConnectionFactoryTest.java │ ├── HttpConnectionPoolTest.java │ └── HttpTest.java │ ├── jdbc │ ├── JdbcConfigTest.java │ ├── JdbcConnectionFactoryTest.java │ ├── JdbcConnectionPoolTest.java │ └── JdbcTest.java │ ├── kafka │ ├── KafkaConfigTest.java │ ├── KafkaConnectionFactoryTest.java │ ├── KafkaConnectionPoolTest.java │ ├── KafkaSharedConnPoolTest.java │ └── KafkaTest.java │ ├── redis │ ├── JedisConn.java │ ├── JedisConn2.java │ ├── JedisConn3.java │ ├── JedisConn4.java │ ├── RedisClusterConnPoolTest.java │ ├── RedisConfigTest.java │ ├── RedisConnectionFactoryOldTest.java │ ├── RedisConnectionPoolOldTest.java │ ├── RedisConnectionPoolTest.java │ ├── RedisSentinelConnPoolOldTest.java │ ├── RedisSentinelConnPoolTest.java │ ├── RedisShardedConnPoolOldTest.java │ ├── RedisShardedConnPoolTest.java │ ├── RedisTest.java │ ├── ShardedJedisConn.java │ ├── ShardedJedisConn2.java │ ├── ShardedJedisConn3.java │ ├── ShardedJedisConn4.java │ └── ShardedJedisConn5.java │ └── socket │ ├── SocketConfigTest.java │ ├── SocketConnectionFactoryTest.java │ ├── SocketConnectionPoolTest.java │ └── SocketTest.java └── resources └── log4j.properties /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.iml 3 | 4 | # Mobile Tools for Java (J2ME) 5 | .mtj.tmp/ 6 | 7 | # Package Files # 8 | *.war 9 | *.ear 10 | 11 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 12 | hs_err_pid* 13 | /target 14 | /.settings/ 15 | /.classpath 16 | /.project 17 | /.idea/ 18 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - openjdk8 4 | before_install: 5 | - pip install --user codecov 6 | after_success: 7 | - codecov 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Connection Pool Client 2 | 3 | [![Organization](https://img.shields.io/badge/org-%20DarkPhoenixs-yellow.svg)](http://www.darkphoenixs.org) 4 | [![Build Status](https://travis-ci.org/DarkPhoenixs/connection-pool-client.svg?branch=master)](https://travis-ci.org/DarkPhoenixs/connection-pool-client) 5 | [![Codecov](https://codecov.io/gh/darkphoenixs/connection-pool-client/branch/master/graph/badge.svg)](https://codecov.io/gh/DarkPhoenixs/connection-pool-client) 6 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.darkphoenixs/connectionpool-client/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.darkphoenixs/connectionpool-client/) 7 | [![Javadoc](https://javadoc.io/badge2/org.darkphoenixs/connectionpool-client/javadoc.svg)](https://javadoc.io/doc/org.darkphoenixs/connectionpool-client) 8 | [![GitHub release](https://img.shields.io/github/release/DarkPhoenixs/connection-pool-client.svg)](https://github.com/DarkPhoenixs/connection-pool-client/releases) 9 | [![License](https://img.shields.io/badge/license-%20Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) 10 | 11 | 12 | This is a simple multi-purpose connection pool client base on [Apache Commons Pool ™](http://commons.apache.org/proper/commons-pool/) 13 | 14 | _Support_ 15 | * [Apache Kafka](http://kafka.apache.org/) 16 | * [Apache Hbase](http://hbase.apache.org/) 17 | * [Redis](http://redis.io/) 18 | * RMDB (Oracle/MySQL/SQL Server) 19 | * Socket (TCP) 20 | * HTTP 21 | 22 | ## Documentation 23 | 24 | The documentation is available at [Wiki](https://github.com/DarkPhoenixs/connection-pool-client/wiki). 25 | 26 | ## Download 27 | 28 | #### Maven 29 | 30 | ```xml 31 | 32 | org.darkphoenixs 33 | connectionpool-client 34 | x.x.x 35 | 36 | ``` 37 | 38 | #### Gradle 39 | 40 | ```groovy 41 | compile 'org.darkphoenixs:connectionpool-client:x.x.x' 42 | ``` 43 | 44 | #### SBT 45 | 46 | ```scala 47 | libraryDependencies += "org.darkphoenixs" % "connectionpool-client" % "x.x.x" 48 | ``` 49 | 50 | ## License 51 | 52 | ``` 53 | Copyright 2015-2020 Dark Phoenixs (Open-Source Organization). 54 | 55 | Licensed under the Apache License, Version 2.0 (the "License"); 56 | you may not use this file except in compliance with the License. 57 | You may obtain a copy of the License at 58 | 59 | http://www.apache.org/licenses/LICENSE-2.0 60 | 61 | Unless required by applicable law or agreed to in writing, software 62 | distributed under the License is distributed on an "AS IS" BASIS, 63 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 64 | See the License for the specific language governing permissions and 65 | limitations under the License. 66 | ``` 67 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/ConnectionException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool; 17 | 18 | /** 19 | *

Title: ConnectionException

20 | *

Description: 连接异常

21 | * 22 | * @author Victor 23 | * @version 1.0 24 | * @see RuntimeException 25 | * @since 2015年9月19日 26 | */ 27 | public class ConnectionException extends RuntimeException { 28 | 29 | private static final long serialVersionUID = -6503525110247209484L; 30 | 31 | public ConnectionException() { 32 | super(); 33 | } 34 | 35 | public ConnectionException(String message) { 36 | super(message); 37 | } 38 | 39 | public ConnectionException(Throwable e) { 40 | super(e); 41 | } 42 | 43 | public ConnectionException(String message, Throwable cause) { 44 | super(message, cause); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/ConnectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool; 17 | 18 | import org.apache.commons.pool2.PooledObjectFactory; 19 | 20 | import java.io.Serializable; 21 | 22 | /** 23 | *

Title: ConnectionFactory

24 | *

Description: 连接工厂接口

25 | * 26 | * @author Victor 27 | * @version 1.0 28 | * @see PooledObjectFactory 29 | * @see Serializable 30 | * @since 2015年9月19日 31 | */ 32 | public interface ConnectionFactory extends PooledObjectFactory, Serializable { 33 | 34 | /** 35 | *

Title: createConnection

36 | *

Description: 创建连接

37 | * 38 | * @return 连接 39 | * @throws Exception 40 | */ 41 | public abstract T createConnection() throws Exception; 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/ConnectionPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool; 17 | 18 | import java.io.Serializable; 19 | 20 | /** 21 | *

Title: ConnectionPool

22 | *

Description: 连接池接口

23 | * 24 | * @author Victor 25 | * @version 1.0 26 | * @see Serializable 27 | * @since 2015年9月19日 28 | */ 29 | public interface ConnectionPool extends Serializable { 30 | 31 | /** 32 | *

Title: getConnection

33 | *

Description: 获取连接

34 | * 35 | * @return 连接 36 | */ 37 | public abstract T getConnection(); 38 | 39 | /** 40 | *

Title: returnConnection

41 | *

Description: 返回连接

42 | * 43 | * @param conn 连接 44 | */ 45 | public void returnConnection(T conn); 46 | 47 | /** 48 | *

Title: invalidateConnection

49 | *

Description: 废弃连接

50 | * 51 | * @param conn 连接 52 | */ 53 | public void invalidateConnection(T conn); 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/PoolConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool; 17 | 18 | import org.apache.commons.pool2.impl.GenericObjectPoolConfig; 19 | 20 | import java.io.Serializable; 21 | 22 | /** 23 | *

Title: PoolConfig

24 | *

Description: 默认池配置

25 | * 26 | * @author Victor 27 | * @version 1.0 28 | * @see GenericObjectPoolConfig 29 | * @see Serializable 30 | * @since 2015年9月19日 31 | */ 32 | public class PoolConfig extends GenericObjectPoolConfig implements Serializable { 33 | 34 | /** 35 | * DEFAULT_TEST_WHILE_IDLE 36 | */ 37 | public static final boolean DEFAULT_TEST_WHILE_IDLE = true; 38 | /** 39 | * DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS 40 | */ 41 | public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS = 60000; 42 | /** 43 | * DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS 44 | */ 45 | public static final long DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 30000; 46 | /** 47 | * DEFAULT_NUM_TESTS_PER_EVICTION_RUN 48 | */ 49 | public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUN = -1; 50 | /** 51 | * serialVersionUID 52 | */ 53 | private static final long serialVersionUID = -2414567557372345057L; 54 | 55 | /** 56 | *

Title: PoolConfig

57 | *

Description: 默认构造方法

58 | */ 59 | public PoolConfig() { 60 | 61 | // defaults to make your life with connection pool easier :) 62 | setTestWhileIdle(DEFAULT_TEST_WHILE_IDLE); 63 | setMinEvictableIdleTimeMillis(DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS); 64 | setTimeBetweenEvictionRunsMillis(DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS); 65 | setNumTestsPerEvictionRun(DEFAULT_NUM_TESTS_PER_EVICTION_RUN); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/hbase/HbaseConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.hbase; 17 | 18 | /** 19 | *

HbaseConfig

20 | *

Hbase配置

21 | * 22 | * @author Victor 23 | * @since 1.2.1 24 | */ 25 | public interface HbaseConfig { 26 | 27 | /** 28 | * DEFAULT_HOST 29 | */ 30 | public static final String DEFAULT_HOST = "localhost"; 31 | /** 32 | * DEFAULT_PORT 33 | */ 34 | public static final String DEFAULT_PORT = "2181"; 35 | /** 36 | * DEFAULT_MASTER 37 | */ 38 | public static final String DEFAULT_MASTER = null; 39 | /** 40 | * DEFAULT_ROOTDIR 41 | */ 42 | public static final String DEFAULT_ROOTDIR = null; 43 | 44 | /** 45 | * ZOOKEEPER_QUORUM_PROPERTY 46 | */ 47 | public static final String ZOOKEEPER_QUORUM_PROPERTY = "hbase.zookeeper.quorum"; 48 | /** 49 | * ZOOKEEPER_CLIENTPORT_PROPERTY 50 | */ 51 | public static final String ZOOKEEPER_CLIENTPORT_PROPERTY = "hbase.zookeeper.property.clientPort"; 52 | /** 53 | * MASTER_PROPERTY 54 | */ 55 | public static final String MASTER_PROPERTY = "hbase.master"; 56 | /** 57 | * ROOTDIR_PROPERTY 58 | */ 59 | public static final String ROOTDIR_PROPERTY = "hbase.rootdir"; 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/hbase/HbaseConnectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.hbase; 17 | 18 | import org.apache.commons.pool2.PooledObject; 19 | import org.apache.commons.pool2.impl.DefaultPooledObject; 20 | import org.apache.hadoop.conf.Configuration; 21 | import org.apache.hadoop.hbase.client.Connection; 22 | import org.darkphoenixs.pool.ConnectionException; 23 | import org.darkphoenixs.pool.ConnectionFactory; 24 | 25 | import java.util.Map.Entry; 26 | import java.util.Properties; 27 | 28 | /** 29 | *

Title: HbaseConnectionFactory

30 | *

Description: Hbase连接工厂

31 | * 32 | * @author Victor 33 | * @version 1.0 34 | * @see ConnectionFactory 35 | * @since 2015年9月19日 36 | */ 37 | class HbaseConnectionFactory implements ConnectionFactory { 38 | 39 | /** 40 | * serialVersionUID 41 | */ 42 | private static final long serialVersionUID = 4024923894283696465L; 43 | 44 | /** 45 | * hadoopConfiguration 46 | */ 47 | private final Configuration hadoopConfiguration; 48 | 49 | /** 50 | *

Title: HbaseConnectionFactory

51 | *

Description: 构造方法

52 | * 53 | * @param hadoopConfiguration hbase配置 54 | */ 55 | public HbaseConnectionFactory(final Configuration hadoopConfiguration) { 56 | 57 | this.hadoopConfiguration = hadoopConfiguration; 58 | } 59 | 60 | /** 61 | *

Title: HbaseConnectionFactory

62 | *

Description: 构造方法

63 | * 64 | * @param host zookeeper地址 65 | * @param port zookeeper端口 66 | * @param master hbase主机 67 | * @param rootdir hdfs数据目录 68 | */ 69 | public HbaseConnectionFactory(final String host, final String port, final String master, final String rootdir) { 70 | 71 | this.hadoopConfiguration = new Configuration(); 72 | 73 | if (host == null) 74 | throw new ConnectionException("[" + HbaseConfig.ZOOKEEPER_QUORUM_PROPERTY + "] is required !"); 75 | this.hadoopConfiguration.set(HbaseConfig.ZOOKEEPER_QUORUM_PROPERTY, host); 76 | 77 | if (port == null) 78 | throw new ConnectionException("[" + HbaseConfig.ZOOKEEPER_CLIENTPORT_PROPERTY + "] is required !"); 79 | this.hadoopConfiguration.set(HbaseConfig.ZOOKEEPER_CLIENTPORT_PROPERTY, port); 80 | 81 | if (master != null) 82 | this.hadoopConfiguration.set(HbaseConfig.MASTER_PROPERTY, master); 83 | 84 | if (rootdir != null) 85 | this.hadoopConfiguration.set(HbaseConfig.ROOTDIR_PROPERTY, rootdir); 86 | } 87 | 88 | /** 89 | * @param properties 参数配置 90 | * @since 1.2.1 91 | */ 92 | public HbaseConnectionFactory(final Properties properties) { 93 | 94 | this.hadoopConfiguration = new Configuration(); 95 | 96 | for (Entry entry : properties.entrySet()) { 97 | 98 | this.hadoopConfiguration.set((String) entry.getKey(), (String) entry.getValue()); 99 | } 100 | } 101 | 102 | @Override 103 | public PooledObject makeObject() throws Exception { 104 | 105 | Connection connection = this.createConnection(); 106 | 107 | return new DefaultPooledObject(connection); 108 | } 109 | 110 | @Override 111 | public void destroyObject(PooledObject p) throws Exception { 112 | 113 | Connection connection = p.getObject(); 114 | 115 | if (connection != null) 116 | 117 | connection.close(); 118 | } 119 | 120 | @Override 121 | public boolean validateObject(PooledObject p) { 122 | 123 | Connection connection = p.getObject(); 124 | 125 | if (connection != null) 126 | 127 | return ((!connection.isAborted()) && (!connection.isClosed())); 128 | 129 | return false; 130 | } 131 | 132 | @Override 133 | public void activateObject(PooledObject p) throws Exception { 134 | // TODO Auto-generated method stub 135 | 136 | } 137 | 138 | @Override 139 | public void passivateObject(PooledObject p) throws Exception { 140 | // TODO Auto-generated method stub 141 | 142 | } 143 | 144 | @Override 145 | public Connection createConnection() throws Exception { 146 | 147 | Connection connection = org.apache.hadoop.hbase.client.ConnectionFactory 148 | .createConnection(hadoopConfiguration); 149 | 150 | return connection; 151 | } 152 | 153 | } 154 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/hbase/HbaseConnectionPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.hbase; 17 | 18 | import org.apache.hadoop.conf.Configuration; 19 | import org.apache.hadoop.hbase.client.Connection; 20 | import org.darkphoenixs.pool.ConnectionPool; 21 | import org.darkphoenixs.pool.PoolBase; 22 | import org.darkphoenixs.pool.PoolConfig; 23 | 24 | import java.util.Properties; 25 | 26 | /** 27 | *

Title: HbaseConnectionPool

28 | *

Description: Hbase连接池

29 | * 30 | * @author Victor 31 | * @version 1.0 32 | * @see PoolBase 33 | * @see ConnectionPool 34 | * @since 2015年9月19日 35 | */ 36 | public class HbaseConnectionPool extends PoolBase implements ConnectionPool { 37 | 38 | /** 39 | * serialVersionUID 40 | */ 41 | private static final long serialVersionUID = -9126420905798370243L; 42 | 43 | /** 44 | *

Title: HbaseConnectionPool

45 | *

Description: 默认构造方法

46 | */ 47 | public HbaseConnectionPool() { 48 | 49 | this(HbaseConfig.DEFAULT_HOST, HbaseConfig.DEFAULT_PORT); 50 | } 51 | 52 | /** 53 | *

Title: HbaseConnectionPool

54 | *

Description: 构造方法

55 | * 56 | * @param host 地址 57 | * @param port 端口 58 | */ 59 | public HbaseConnectionPool(final String host, final String port) { 60 | 61 | this(new PoolConfig(), host, port, HbaseConfig.DEFAULT_MASTER, HbaseConfig.DEFAULT_ROOTDIR); 62 | } 63 | 64 | /** 65 | *

Title: HbaseConnectionPool

66 | *

Description: 构造方法

67 | * 68 | * @param host 地址 69 | * @param port 端口 70 | * @param master hbase主机 71 | * @param rootdir hdfs目录 72 | */ 73 | public HbaseConnectionPool(final String host, final String port, final String master, final String rootdir) { 74 | 75 | this(new PoolConfig(), host, port, master, rootdir); 76 | } 77 | 78 | /** 79 | *

Title: HbaseConnectionPool

80 | *

Description: 构造方法

81 | * 82 | * @param hadoopConfiguration hbase配置 83 | */ 84 | public HbaseConnectionPool(final Configuration hadoopConfiguration) { 85 | 86 | this(new PoolConfig(), hadoopConfiguration); 87 | } 88 | 89 | /** 90 | *

Title: HbaseConnectionPool

91 | *

Description: 构造方法

92 | * 93 | * @param poolConfig 池配置 94 | * @param host 地址 95 | * @param port 端口 96 | */ 97 | public HbaseConnectionPool(final PoolConfig poolConfig, final String host, final String port) { 98 | 99 | this(poolConfig, host, port, HbaseConfig.DEFAULT_MASTER, HbaseConfig.DEFAULT_ROOTDIR); 100 | } 101 | 102 | /** 103 | *

Title: HbaseConnectionPool

104 | *

Description: 构造方法

105 | * 106 | * @param poolConfig 池配置 107 | * @param hadoopConfiguration hbase配置 108 | */ 109 | public HbaseConnectionPool(final PoolConfig poolConfig, final Configuration hadoopConfiguration) { 110 | 111 | super(poolConfig, new HbaseConnectionFactory(hadoopConfiguration)); 112 | } 113 | 114 | /** 115 | *

Title: HbaseConnectionPool

116 | *

Description: 构造方法

117 | * 118 | * @param poolConfig 池配置 119 | * @param host 地址 120 | * @param port 端口 121 | * @param master hbase主机 122 | * @param rootdir hdfs目录 123 | */ 124 | public HbaseConnectionPool(final PoolConfig poolConfig, final String host, final String port, final String master, final String rootdir) { 125 | 126 | super(poolConfig, new HbaseConnectionFactory(host, port, master, rootdir)); 127 | } 128 | 129 | /** 130 | * @param poolConfig 池配置 131 | * @param properties 参数配置 132 | * @since 1.2.1 133 | */ 134 | public HbaseConnectionPool(final PoolConfig poolConfig, final Properties properties) { 135 | 136 | super(poolConfig, new HbaseConnectionFactory(properties)); 137 | } 138 | 139 | @Override 140 | public Connection getConnection() { 141 | 142 | return super.getResource(); 143 | } 144 | 145 | @Override 146 | public void returnConnection(Connection conn) { 147 | 148 | super.returnResource(conn); 149 | } 150 | 151 | @Override 152 | public void invalidateConnection(Connection conn) { 153 | 154 | super.invalidateResource(conn); 155 | } 156 | 157 | } 158 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/hbase/HbaseSharedConnPool.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.hbase; 2 | 3 | import org.apache.hadoop.conf.Configuration; 4 | import org.apache.hadoop.hbase.client.Connection; 5 | import org.apache.hadoop.hbase.client.ConnectionFactory; 6 | import org.darkphoenixs.pool.ConnectionException; 7 | import org.darkphoenixs.pool.ConnectionPool; 8 | 9 | import java.io.IOException; 10 | import java.util.Map; 11 | import java.util.Properties; 12 | import java.util.concurrent.atomic.AtomicReference; 13 | 14 | /** 15 | *

Title: HbaseSharedConnPool

16 | *

Description: Hbase共享连接池(单例)

17 | * 18 | * @author Victor.Zxy 19 | * @version 1.2.3 20 | * @see ConnectionPool 21 | * @since 2016 /8/25 22 | */ 23 | public class HbaseSharedConnPool implements ConnectionPool { 24 | 25 | private static final AtomicReference pool = new AtomicReference(); 26 | 27 | private final Connection connection; 28 | 29 | private HbaseSharedConnPool(Configuration configuration) throws IOException { 30 | 31 | this.connection = ConnectionFactory.createConnection(configuration); 32 | } 33 | 34 | /** 35 | * Gets instance. 36 | * 37 | * @param host the host 38 | * @param port the port 39 | * @param master the master 40 | * @param rootdir the rootdir 41 | * @return the instance 42 | */ 43 | public synchronized static HbaseSharedConnPool getInstance(final String host, final String port, final String master, final String rootdir) { 44 | 45 | Properties properties = new Properties(); 46 | 47 | if (host == null) 48 | throw new ConnectionException("[" + HbaseConfig.ZOOKEEPER_QUORUM_PROPERTY + "] is required !"); 49 | properties.setProperty(HbaseConfig.ZOOKEEPER_QUORUM_PROPERTY, host); 50 | 51 | if (port == null) 52 | throw new ConnectionException("[" + HbaseConfig.ZOOKEEPER_CLIENTPORT_PROPERTY + "] is required !"); 53 | properties.setProperty(HbaseConfig.ZOOKEEPER_CLIENTPORT_PROPERTY, port); 54 | 55 | if (master != null) 56 | properties.setProperty(HbaseConfig.MASTER_PROPERTY, master); 57 | 58 | if (rootdir != null) 59 | properties.setProperty(HbaseConfig.ROOTDIR_PROPERTY, rootdir); 60 | 61 | return getInstance(properties); 62 | } 63 | 64 | /** 65 | * Gets instance. 66 | * 67 | * @param properties the properties 68 | * @return the instance 69 | */ 70 | public synchronized static HbaseSharedConnPool getInstance(final Properties properties) { 71 | 72 | Configuration configuration = new Configuration(); 73 | 74 | for (Map.Entry entry : properties.entrySet()) { 75 | 76 | configuration.set((String) entry.getKey(), (String) entry.getValue()); 77 | } 78 | 79 | return getInstance(configuration); 80 | } 81 | 82 | /** 83 | * Gets instance. 84 | * 85 | * @param configuration the configuration 86 | * @return the instance 87 | */ 88 | public synchronized static HbaseSharedConnPool getInstance(final Configuration configuration) { 89 | 90 | if (pool.get() == null) 91 | 92 | try { 93 | pool.set(new HbaseSharedConnPool(configuration)); 94 | 95 | } catch (IOException e) { 96 | 97 | e.printStackTrace(); 98 | } 99 | 100 | return pool.get(); 101 | } 102 | 103 | @Override 104 | public Connection getConnection() { 105 | 106 | return connection; 107 | } 108 | 109 | @Override 110 | public void returnConnection(Connection conn) { 111 | 112 | // TODO: 2016/8/25 113 | } 114 | 115 | @Override 116 | public void invalidateConnection(Connection conn) { 117 | 118 | try { 119 | if (conn != null) 120 | 121 | conn.close(); 122 | 123 | } catch (IOException e) { 124 | 125 | e.printStackTrace(); 126 | } 127 | } 128 | 129 | /** 130 | * Close. 131 | */ 132 | public void close() { 133 | 134 | try { 135 | connection.close(); 136 | 137 | pool.set(null); 138 | 139 | } catch (IOException e) { 140 | 141 | e.printStackTrace(); 142 | } 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/http/HttpConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.http; 17 | 18 | import java.util.Collections; 19 | import java.util.Map; 20 | 21 | /** 22 | *

HttpConfig

23 | *

Http配置

24 | * 25 | * @author Victor 26 | * @since 1.2.7 27 | */ 28 | public interface HttpConfig { 29 | 30 | String DEFAULT_URL = "https://www.baidu.com/"; 31 | 32 | String DEFAULT_METHOD = "GET"; 33 | 34 | Map DEFAULT_HEADER = Collections.singletonMap("Content-type", "application/json"); 35 | 36 | int DEFAULT_IMEOUT = 30000; 37 | 38 | String PROXY_HOST_PROPERTY = "proxyHost"; 39 | 40 | String PROXY_PORT_PROPERTY = "proxyPort"; 41 | 42 | String HTTP_URL_PROPERTY = "httpUrl"; 43 | 44 | String HTTP_METHOD_PROPERTY = "httpMethod"; 45 | 46 | String CONNECT_TIMEOUT_PROPERTY = "connectTimeout"; 47 | 48 | String READ_TIMEOUT_PROPERTY = "readTimeout"; 49 | 50 | String HTTP_HEADER_PROPERTY = "httpHeader"; 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/http/HttpConnectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.http; 17 | 18 | import org.apache.commons.pool2.PooledObject; 19 | import org.apache.commons.pool2.impl.DefaultPooledObject; 20 | import org.darkphoenixs.pool.ConnectionException; 21 | import org.darkphoenixs.pool.ConnectionFactory; 22 | 23 | import java.io.IOException; 24 | import java.net.HttpURLConnection; 25 | import java.net.InetSocketAddress; 26 | import java.net.Proxy; 27 | import java.net.URL; 28 | import java.util.HashMap; 29 | import java.util.Map; 30 | import java.util.Properties; 31 | 32 | /** 33 | *

HttpConnectionFactory

34 | *

Http连接工厂

35 | * 36 | * @author Victor 37 | * @see ConnectionFactory 38 | * @since 1.2.7 39 | */ 40 | public class HttpConnectionFactory implements ConnectionFactory { 41 | 42 | private static final long serialVersionUID = 7077039510216492412L; 43 | 44 | private final Proxy proxy; 45 | 46 | private final String address; 47 | 48 | private final String method; 49 | 50 | private final int connectTimeout; 51 | 52 | private final int readTimeout; 53 | 54 | private final Map header; 55 | 56 | /** 57 | * Instantiates a new Http connection factory. 58 | * 59 | * @param proxy the proxy 60 | * @param address the address 61 | * @param method the method 62 | * @param connectTimeout the connect timeout 63 | * @param readTimeout the read timeout 64 | * @param header the header 65 | */ 66 | public HttpConnectionFactory(final Proxy proxy, final String address, 67 | final String method, final int connectTimeout, 68 | final int readTimeout, final Map header) { 69 | 70 | this.proxy = proxy; 71 | this.address = address; 72 | this.method = method; 73 | this.connectTimeout = connectTimeout; 74 | this.readTimeout = readTimeout; 75 | this.header = header; 76 | } 77 | 78 | /** 79 | * Instantiates a new Http connection factory. 80 | * 81 | * @param properties the properties 82 | */ 83 | public HttpConnectionFactory(final Properties properties) { 84 | 85 | String proxyHost = properties.getProperty(HttpConfig.PROXY_HOST_PROPERTY); 86 | String proxyPort = properties.getProperty(HttpConfig.PROXY_PORT_PROPERTY); 87 | 88 | if (proxyHost != null && proxyPort != null) 89 | proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, Integer.valueOf(proxyPort))); 90 | else 91 | proxy = null; 92 | 93 | address = properties.getProperty(HttpConfig.HTTP_URL_PROPERTY); 94 | if (address == null) 95 | throw new ConnectionException("[" + HttpConfig.HTTP_URL_PROPERTY + "] is required !"); 96 | 97 | method = properties.getProperty(HttpConfig.HTTP_METHOD_PROPERTY); 98 | if (method == null) 99 | throw new ConnectionException("[" + HttpConfig.HTTP_METHOD_PROPERTY + "] is required !"); 100 | 101 | connectTimeout = Integer.valueOf(properties.getProperty(HttpConfig.CONNECT_TIMEOUT_PROPERTY, String.valueOf(HttpConfig.DEFAULT_IMEOUT))); 102 | 103 | readTimeout = Integer.valueOf(properties.getProperty(HttpConfig.READ_TIMEOUT_PROPERTY, String.valueOf(HttpConfig.DEFAULT_IMEOUT))); 104 | 105 | header = new HashMap(); 106 | 107 | String headers = properties.getProperty(HttpConfig.HTTP_HEADER_PROPERTY, " : "); 108 | 109 | for (String headStr : headers.split(",")) 110 | 111 | header.put(headStr.split(":")[0], headStr.split(":")[1]); 112 | } 113 | 114 | 115 | @Override 116 | public PooledObject makeObject() throws Exception { 117 | 118 | HttpURLConnection connection = this.createConnection(); 119 | 120 | return new DefaultPooledObject(connection); 121 | } 122 | 123 | @Override 124 | public void destroyObject(PooledObject p) throws Exception { 125 | 126 | HttpURLConnection connection = p.getObject(); 127 | 128 | if (connection != null) 129 | 130 | connection.disconnect(); 131 | } 132 | 133 | @Override 134 | public boolean validateObject(PooledObject p) { 135 | 136 | HttpURLConnection connection = p.getObject(); 137 | 138 | if (connection != null) 139 | 140 | try { 141 | return connection.getResponseCode() == HttpURLConnection.HTTP_OK; 142 | } catch (IOException e) { 143 | return false; 144 | } 145 | 146 | return false; 147 | } 148 | 149 | @Override 150 | public void activateObject(PooledObject p) throws Exception { 151 | // TODO Auto-generated method stub 152 | } 153 | 154 | @Override 155 | public void passivateObject(PooledObject p) throws Exception { 156 | // TODO Auto-generated method stub 157 | } 158 | 159 | 160 | @Override 161 | public HttpURLConnection createConnection() throws Exception { 162 | 163 | HttpURLConnection urlConnection = null; 164 | 165 | try { 166 | URL url = new URL(this.address); 167 | 168 | if (proxy != null) 169 | urlConnection = (HttpURLConnection) url.openConnection(proxy); 170 | else 171 | urlConnection = (HttpURLConnection) url.openConnection(); 172 | 173 | urlConnection.setConnectTimeout(connectTimeout); 174 | urlConnection.setReadTimeout(readTimeout); 175 | 176 | for (Map.Entry entry : header.entrySet()) 177 | urlConnection.setRequestProperty(entry.getKey(), entry.getValue()); 178 | 179 | urlConnection.setRequestMethod(method); 180 | urlConnection.setDoOutput(true); 181 | urlConnection.setDoInput(true); 182 | 183 | if (method.equals("POST")) 184 | urlConnection.setUseCaches(false); 185 | 186 | urlConnection.connect(); 187 | 188 | } catch (Exception e) { 189 | if (urlConnection != null) 190 | urlConnection.disconnect(); 191 | throw e; 192 | } 193 | 194 | return urlConnection; 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/jdbc/JdbcConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.jdbc; 17 | 18 | /** 19 | *

JdbcConfig

20 | *

JDBC配置

21 | * 22 | * @author Victor 23 | * @since 1.2.1 24 | */ 25 | public interface JdbcConfig { 26 | 27 | /** 28 | * MYSQL DEFAULT_DRIVER_CLASS 29 | */ 30 | public static final String DEFAULT_DRIVER_CLASS = "com.mysql.jdbc.Driver"; 31 | /** 32 | * MYSQL DEFAULT_JDBC_URL 33 | */ 34 | public static final String DEFAULT_JDBC_URL = "jdbc:mysql://localhost:3306/test"; 35 | /** 36 | * MYSQL DEFAULT_JDBC_USERNAME 37 | */ 38 | public static final String DEFAULT_JDBC_USERNAME = "root"; 39 | /** 40 | * MYSQL DEFAULT_JDBC_PASSWORD 41 | */ 42 | public static final String DEFAULT_JDBC_PASSWORD = "root"; 43 | 44 | /** 45 | * DRIVER_CLASS_PROPERTY 46 | */ 47 | public static final String DRIVER_CLASS_PROPERTY = "driverClass"; 48 | /** 49 | * JDBC_URL_PROPERTY 50 | */ 51 | public static final String JDBC_URL_PROPERTY = "jdbcUrl"; 52 | /** 53 | * JDBC_USERNAME_PROPERTY 54 | */ 55 | public static final String JDBC_USERNAME_PROPERTY = "username"; 56 | /** 57 | * JDBC_PASSWORD_PROPERTY 58 | */ 59 | public static final String JDBC_PASSWORD_PROPERTY = "password"; 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/jdbc/JdbcConnectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.jdbc; 17 | 18 | import org.apache.commons.pool2.PooledObject; 19 | import org.apache.commons.pool2.impl.DefaultPooledObject; 20 | import org.darkphoenixs.pool.ConnectionException; 21 | import org.darkphoenixs.pool.ConnectionFactory; 22 | 23 | import java.sql.Connection; 24 | import java.sql.DriverManager; 25 | import java.sql.SQLException; 26 | import java.util.Properties; 27 | 28 | /** 29 | *

Title: JdbcConnectionFactory

30 | *

Description: JDBC连接工厂

31 | * 32 | * @author Victor.Zxy 33 | * @version 1.0 34 | * @see ConnectionFactory 35 | * @since 2015年11月17日 36 | */ 37 | class JdbcConnectionFactory implements ConnectionFactory { 38 | 39 | /** 40 | * serialVersionUID 41 | */ 42 | private static final long serialVersionUID = 4941500146671191616L; 43 | 44 | /** 45 | * driverClass 46 | */ 47 | private final String driverClass; 48 | 49 | /** 50 | * jdbcUrl 51 | */ 52 | private final String jdbcUrl; 53 | 54 | /** 55 | * username 56 | */ 57 | private final String username; 58 | 59 | /** 60 | * password 61 | */ 62 | private final String password; 63 | 64 | /** 65 | *

Title: JdbcConnectionFactory

66 | *

Description: 构造方法

67 | * 68 | * @param properties JDBC参数 69 | */ 70 | public JdbcConnectionFactory(final Properties properties) { 71 | 72 | this.driverClass = properties.getProperty(JdbcConfig.DRIVER_CLASS_PROPERTY); 73 | if (driverClass == null) 74 | throw new ConnectionException("[" + JdbcConfig.DRIVER_CLASS_PROPERTY + "] is required !"); 75 | 76 | this.jdbcUrl = properties.getProperty(JdbcConfig.JDBC_URL_PROPERTY); 77 | if (jdbcUrl == null) 78 | throw new ConnectionException("[" + JdbcConfig.JDBC_URL_PROPERTY + "] is required !"); 79 | 80 | this.username = properties.getProperty(JdbcConfig.JDBC_USERNAME_PROPERTY); 81 | if (username == null) 82 | throw new ConnectionException("[" + JdbcConfig.JDBC_USERNAME_PROPERTY + "] is required !"); 83 | 84 | this.password = properties.getProperty(JdbcConfig.JDBC_PASSWORD_PROPERTY); 85 | if (password == null) 86 | throw new ConnectionException("[" + JdbcConfig.JDBC_PASSWORD_PROPERTY + "] is required !"); 87 | 88 | this.loadDriver(); 89 | } 90 | 91 | /** 92 | *

Title: JdbcConnectionFactory

93 | *

Description: 构造方法

94 | * 95 | * @param driverClass 驱动类 96 | * @param jdbcUrl 数据库URL 97 | * @param username 数据库用户名 98 | * @param password 数据密码 99 | */ 100 | public JdbcConnectionFactory(final String driverClass, final String jdbcUrl, final String username, final String password) { 101 | 102 | this.driverClass = driverClass; 103 | this.jdbcUrl = jdbcUrl; 104 | this.username = username; 105 | this.password = password; 106 | this.loadDriver(); 107 | } 108 | 109 | /** 110 | *

Title: loadDriver

111 | *

Description: 加载驱动

112 | */ 113 | private void loadDriver() { 114 | 115 | try { 116 | Class.forName(driverClass); 117 | } catch (ClassNotFoundException e) { 118 | e.printStackTrace(); 119 | } 120 | 121 | } 122 | 123 | @Override 124 | public PooledObject makeObject() throws Exception { 125 | 126 | Connection connection = this.createConnection(); 127 | 128 | return new DefaultPooledObject(connection); 129 | } 130 | 131 | @Override 132 | public void destroyObject(PooledObject p) throws Exception { 133 | 134 | Connection connection = p.getObject(); 135 | 136 | if (connection != null) 137 | 138 | connection.close(); 139 | } 140 | 141 | @Override 142 | public boolean validateObject(PooledObject p) { 143 | 144 | Connection connection = p.getObject(); 145 | 146 | if (connection != null) 147 | try { 148 | return ((!connection.isClosed()) && (connection.isValid(1))); 149 | } catch (SQLException e) { 150 | e.printStackTrace(); 151 | } 152 | 153 | return false; 154 | } 155 | 156 | @Override 157 | public void activateObject(PooledObject p) throws Exception { 158 | // TODO Auto-generated method stub 159 | 160 | } 161 | 162 | @Override 163 | public void passivateObject(PooledObject p) throws Exception { 164 | // TODO Auto-generated method stub 165 | 166 | } 167 | 168 | @Override 169 | public Connection createConnection() throws Exception { 170 | 171 | Connection connection = DriverManager.getConnection(jdbcUrl, username, password); 172 | 173 | return connection; 174 | } 175 | 176 | } 177 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/jdbc/JdbcConnectionPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.jdbc; 17 | 18 | import org.darkphoenixs.pool.ConnectionPool; 19 | import org.darkphoenixs.pool.PoolBase; 20 | import org.darkphoenixs.pool.PoolConfig; 21 | 22 | import java.sql.Connection; 23 | import java.util.Properties; 24 | 25 | /** 26 | *

Title: JdbcConnectionPool

27 | *

Description: Jdbc连接池

28 | * 29 | * @author Victor.Zxy 30 | * @version 1.0 31 | * @see PoolBase 32 | * @see ConnectionPool 33 | * @since 2015年11月17日 34 | */ 35 | public class JdbcConnectionPool extends PoolBase implements ConnectionPool { 36 | 37 | /** 38 | * serialVersionUID 39 | */ 40 | private static final long serialVersionUID = 2743612676107943708L; 41 | 42 | /** 43 | *

Title: JdbcConnectionPool

44 | *

Description: 默认构造方法

45 | */ 46 | public JdbcConnectionPool() { 47 | 48 | this(JdbcConfig.DEFAULT_DRIVER_CLASS, JdbcConfig.DEFAULT_JDBC_URL, JdbcConfig.DEFAULT_JDBC_USERNAME, JdbcConfig.DEFAULT_JDBC_PASSWORD); 49 | } 50 | 51 | /** 52 | *

Title: JdbcConnectionPool

53 | *

Description: 构造方法

54 | * 55 | * @param properties JDBC参数 56 | */ 57 | public JdbcConnectionPool(final Properties properties) { 58 | 59 | this(new PoolConfig(), properties); 60 | } 61 | 62 | /** 63 | *

Title: JdbcConnectionPool

64 | *

Description: 构造方法

65 | * 66 | * @param driverClass 驱动类 67 | * @param jdbcUrl 数据库URL 68 | * @param username 数据库用户名 69 | * @param password 数据密码 70 | */ 71 | public JdbcConnectionPool(final String driverClass, final String jdbcUrl, final String username, final String password) { 72 | 73 | this(new PoolConfig(), driverClass, jdbcUrl, username, password); 74 | } 75 | 76 | /** 77 | *

Title: JdbcConnectionPool

78 | *

Description: 构造方法

79 | * 80 | * @param poolConfig 池配置 81 | * @param properties JDBC参数 82 | */ 83 | public JdbcConnectionPool(final PoolConfig poolConfig, final Properties properties) { 84 | 85 | super(poolConfig, new JdbcConnectionFactory(properties)); 86 | } 87 | 88 | /** 89 | *

Title: JdbcConnectionPool

90 | *

Description: 构造方法

91 | * 92 | * @param poolConfig 池配置 93 | * @param driverClass 驱动类 94 | * @param jdbcUrl 数据库URL 95 | * @param username 数据库用户名 96 | * @param password 数据密码 97 | */ 98 | public JdbcConnectionPool(final PoolConfig poolConfig, final String driverClass, final String jdbcUrl, final String username, final String password) { 99 | 100 | super(poolConfig, new JdbcConnectionFactory(driverClass, jdbcUrl, username, password)); 101 | } 102 | 103 | @Override 104 | public Connection getConnection() { 105 | 106 | return super.getResource(); 107 | } 108 | 109 | @Override 110 | public void returnConnection(Connection conn) { 111 | 112 | super.returnResource(conn); 113 | } 114 | 115 | @Override 116 | public void invalidateConnection(Connection conn) { 117 | 118 | super.invalidateResource(conn); 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/kafka/KafkaConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.kafka; 17 | 18 | /** 19 | *

KafkaConfig

20 | *

Kafka配置

21 | * 22 | * @author Victor 23 | * @since 1.2.1 24 | */ 25 | public interface KafkaConfig { 26 | 27 | /** 28 | * DEFAULT_BROKERS 29 | */ 30 | public static final String DEFAULT_BROKERS = "localhost:9092"; 31 | /** 32 | * DEFAULT_TYPE 33 | */ 34 | public static final String DEFAULT_TYPE = "sync"; 35 | /** 36 | * DEFAULT_ACKS 37 | */ 38 | public static final String DEFAULT_ACKS = "0"; 39 | /** 40 | * DEFAULT_CODEC 41 | */ 42 | public static final String DEFAULT_CODEC = "none"; 43 | /** 44 | * DEFAULT_BATCH 45 | */ 46 | public static final String DEFAULT_BATCH = "200"; 47 | 48 | /** 49 | * BROKERS_LIST_PROPERTY 50 | */ 51 | public static final String BROKERS_LIST_PROPERTY = "metadata.broker.list"; 52 | /** 53 | * PRODUCER_TYPE_PROPERTY 54 | */ 55 | public static final String PRODUCER_TYPE_PROPERTY = "producer.type"; 56 | /** 57 | * REQUEST_ACKS_PROPERTY 58 | */ 59 | public static final String REQUEST_ACKS_PROPERTY = "request.required.acks"; 60 | /** 61 | * COMPRESSION_CODEC_PROPERTY 62 | */ 63 | public static final String COMPRESSION_CODEC_PROPERTY = "compression.codec"; 64 | /** 65 | * BATCH_NUMBER_PROPERTY 66 | */ 67 | public static final String BATCH_NUMBER_PROPERTY = "batch.num.messages"; 68 | 69 | /** 70 | * @since 1.2.3 71 | */ 72 | public static final String BOOTSTRAP_SERVERS_PROPERTY = "bootstrap.servers"; 73 | public static final String KEY_SERIALIZER_PROPERTY = "key.serializer"; 74 | public static final String VAL_SERIALIZER_PROPERTY = "value.serializer"; 75 | public static final String DEFAULT_KEY_SERIALIZER = "org.apache.kafka.common.serialization.ByteArraySerializer"; 76 | public static final String DEFAULT_VAL_SERIALIZER = "org.apache.kafka.common.serialization.ByteArraySerializer"; 77 | 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/kafka/KafkaConnectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.kafka; 17 | 18 | import kafka.javaapi.producer.Producer; 19 | import kafka.producer.ProducerConfig; 20 | import org.apache.commons.pool2.PooledObject; 21 | import org.apache.commons.pool2.impl.DefaultPooledObject; 22 | import org.darkphoenixs.pool.ConnectionException; 23 | import org.darkphoenixs.pool.ConnectionFactory; 24 | 25 | import java.util.Properties; 26 | 27 | /** 28 | *

Title: KafkaConnectionFactory

29 | *

Description: Kafka连接工厂

30 | * 31 | * @author Victor 32 | * @version 1.0 33 | * @see ConnectionFactory 34 | * @since 2015年9月19日 35 | */ 36 | class KafkaConnectionFactory implements ConnectionFactory> { 37 | 38 | /** 39 | * serialVersionUID 40 | */ 41 | private static final long serialVersionUID = 8271607366818512399L; 42 | 43 | /** 44 | * config 45 | */ 46 | private final ProducerConfig config; 47 | 48 | /** 49 | *

Title: KafkaConnectionFactory

50 | *

Description: 构造方法

51 | * 52 | * @param config 生产者配置 53 | */ 54 | public KafkaConnectionFactory(final ProducerConfig config) { 55 | 56 | this.config = config; 57 | } 58 | 59 | /** 60 | *

Title: KafkaConnectionFactory

61 | *

Description: 构造方法

62 | * 63 | * @param brokers broker列表 64 | * @param type 生产者类型 65 | * @param acks 确认类型 66 | * @param codec 压缩类型 67 | * @param batch 批量大小 68 | */ 69 | public KafkaConnectionFactory(final String brokers, final String type, final String acks, final String codec, final String batch) { 70 | 71 | Properties props = new Properties(); 72 | props.setProperty(KafkaConfig.BROKERS_LIST_PROPERTY, brokers); 73 | props.setProperty(KafkaConfig.PRODUCER_TYPE_PROPERTY, type); 74 | props.setProperty(KafkaConfig.REQUEST_ACKS_PROPERTY, acks); 75 | props.setProperty(KafkaConfig.COMPRESSION_CODEC_PROPERTY, codec); 76 | props.setProperty(KafkaConfig.BATCH_NUMBER_PROPERTY, batch); 77 | this.config = new ProducerConfig(props); 78 | } 79 | 80 | /** 81 | * @param properties 参数配置 82 | * @since 1.2.1 83 | */ 84 | public KafkaConnectionFactory(final Properties properties) { 85 | 86 | String brokers = properties.getProperty(KafkaConfig.BROKERS_LIST_PROPERTY); 87 | if (brokers == null) 88 | throw new ConnectionException("[" + KafkaConfig.BROKERS_LIST_PROPERTY + "] is required !"); 89 | 90 | this.config = new ProducerConfig(properties); 91 | } 92 | 93 | @Override 94 | public PooledObject> makeObject() throws Exception { 95 | 96 | Producer producer = this.createConnection(); 97 | 98 | return new DefaultPooledObject>(producer); 99 | } 100 | 101 | @Override 102 | public void destroyObject(PooledObject> p) 103 | throws Exception { 104 | 105 | Producer producer = p.getObject(); 106 | 107 | if (null != producer) 108 | 109 | producer.close(); 110 | } 111 | 112 | @Override 113 | public boolean validateObject(PooledObject> p) { 114 | 115 | Producer producer = p.getObject(); 116 | 117 | return (null != producer); 118 | } 119 | 120 | @Override 121 | public void activateObject(PooledObject> p) 122 | throws Exception { 123 | // TODO Auto-generated method stub 124 | 125 | } 126 | 127 | @Override 128 | public void passivateObject(PooledObject> p) 129 | throws Exception { 130 | // TODO Auto-generated method stub 131 | 132 | } 133 | 134 | @Override 135 | public Producer createConnection() throws Exception { 136 | 137 | Producer producer = new Producer(config); 138 | 139 | return producer; 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/kafka/KafkaConnectionPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.kafka; 17 | 18 | import kafka.javaapi.producer.Producer; 19 | import kafka.producer.ProducerConfig; 20 | import org.darkphoenixs.pool.ConnectionPool; 21 | import org.darkphoenixs.pool.PoolBase; 22 | import org.darkphoenixs.pool.PoolConfig; 23 | 24 | import java.util.Properties; 25 | 26 | /** 27 | *

Title: KafkaConnectionPool

28 | *

Description: Kafka连接池

29 | * 30 | * @author Victor 31 | * @version 1.0 32 | * @see PoolBase 33 | * @see ConnectionPool 34 | * @since 2015年9月19日 35 | */ 36 | public class KafkaConnectionPool extends PoolBase> implements ConnectionPool> { 37 | 38 | /** 39 | * serialVersionUID 40 | */ 41 | private static final long serialVersionUID = -1506435964498488591L; 42 | 43 | /** 44 | *

Title: KafkaConnectionPool

45 | *

Description: 默认构造方法

46 | */ 47 | public KafkaConnectionPool() { 48 | 49 | this(KafkaConfig.DEFAULT_BROKERS); 50 | } 51 | 52 | /** 53 | *

Title: KafkaConnectionPool

54 | *

Description: 构造方法

55 | * 56 | * @param brokers broker列表 57 | */ 58 | public KafkaConnectionPool(final String brokers) { 59 | 60 | this(new PoolConfig(), brokers); 61 | } 62 | 63 | /** 64 | *

Title: KafkaConnectionPool

65 | *

Description: 构造方法

66 | * 67 | * @param props 生产者配置 68 | */ 69 | public KafkaConnectionPool(final Properties props) { 70 | 71 | this(new PoolConfig(), new ProducerConfig(props)); 72 | } 73 | 74 | /** 75 | *

Title: KafkaConnectionPool

76 | *

Description: 构造方法

77 | * 78 | * @param config 生产者配置 79 | */ 80 | public KafkaConnectionPool(final ProducerConfig config) { 81 | 82 | this(new PoolConfig(), config); 83 | } 84 | 85 | /** 86 | *

Title: KafkaConnectionPool

87 | *

Description: 构造方法

88 | * 89 | * @param poolConfig 池配置 90 | * @param props 生产者配置 91 | */ 92 | public KafkaConnectionPool(final PoolConfig poolConfig, final Properties props) { 93 | 94 | this(poolConfig, new ProducerConfig(props)); 95 | } 96 | 97 | /** 98 | *

Title: KafkaConnectionPool

99 | *

Description: 构造方法

100 | * 101 | * @param poolConfig 池配置 102 | * @param brokers broker列表 103 | */ 104 | public KafkaConnectionPool(final PoolConfig poolConfig, final String brokers) { 105 | 106 | this(poolConfig, brokers, KafkaConfig.DEFAULT_TYPE, KafkaConfig.DEFAULT_ACKS, KafkaConfig.DEFAULT_CODEC, KafkaConfig.DEFAULT_BATCH); 107 | } 108 | 109 | /** 110 | *

Title: KafkaConnectionPool

111 | *

Description: 构造方法

112 | * 113 | * @param poolConfig 池配置 114 | * @param brokers broker列表 115 | * @param type 生产者类型 116 | */ 117 | public KafkaConnectionPool(final PoolConfig poolConfig, final String brokers, final String type) { 118 | 119 | this(poolConfig, brokers, type, KafkaConfig.DEFAULT_ACKS, KafkaConfig.DEFAULT_CODEC, KafkaConfig.DEFAULT_BATCH); 120 | } 121 | 122 | /** 123 | *

Title: KafkaConnectionPool

124 | *

Description: 构造方法

125 | * 126 | * @param poolConfig 池配置 127 | * @param config 生产者配置 128 | */ 129 | public KafkaConnectionPool(final PoolConfig poolConfig, final ProducerConfig config) { 130 | 131 | super(poolConfig, new KafkaConnectionFactory(config)); 132 | } 133 | 134 | /** 135 | *

Title: KafkaConnectionPool

136 | *

Description: 构造方法

137 | * 138 | * @param poolConfig 池配置 139 | * @param brokers broker列表 140 | * @param type 生产者类型 141 | * @param acks 确认类型 142 | * @param codec 压缩类型 143 | * @param batch 批量大小 144 | */ 145 | public KafkaConnectionPool(final PoolConfig poolConfig, final String brokers, final String type, final String acks, final String codec, final String batch) { 146 | 147 | super(poolConfig, new KafkaConnectionFactory(brokers, type, acks, codec, batch)); 148 | } 149 | 150 | @Override 151 | public Producer getConnection() { 152 | 153 | return super.getResource(); 154 | } 155 | 156 | @Override 157 | public void returnConnection(Producer conn) { 158 | 159 | super.returnResource(conn); 160 | } 161 | 162 | @Override 163 | public void invalidateConnection(Producer conn) { 164 | 165 | super.invalidateResource(conn); 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/kafka/KafkaSharedConnPool.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.kafka; 2 | 3 | import org.apache.kafka.clients.producer.KafkaProducer; 4 | import org.apache.kafka.clients.producer.Producer; 5 | import org.darkphoenixs.pool.ConnectionPool; 6 | 7 | import java.util.Properties; 8 | import java.util.concurrent.atomic.AtomicReference; 9 | 10 | /** 11 | *

Title: KafkaSharedConnPool

12 | *

Description: Kafka共享连接池(单例)

13 | * 14 | * @author Victor.Zxy 15 | * @version 1.2.3 16 | * @see ConnectionPool 17 | * @since 2016 /8/23 18 | */ 19 | public class KafkaSharedConnPool implements ConnectionPool> { 20 | 21 | private static final AtomicReference pool = new AtomicReference(); 22 | 23 | private final Producer producer; 24 | 25 | private KafkaSharedConnPool(Properties properties) { 26 | 27 | this.producer = new KafkaProducer(properties); 28 | } 29 | 30 | /** 31 | * Gets instance. 32 | * 33 | * @param brokers the brokers 34 | * @param codec the codec 35 | * @param keySer the key ser 36 | * @param valSer the val ser 37 | * @return the instance 38 | */ 39 | public synchronized static KafkaSharedConnPool getInstance(final String brokers, final String codec, final String keySer, final String valSer) { 40 | 41 | Properties properties = new Properties(); 42 | 43 | properties.setProperty(KafkaConfig.BOOTSTRAP_SERVERS_PROPERTY, brokers); 44 | properties.setProperty(KafkaConfig.COMPRESSION_CODEC_PROPERTY, codec); 45 | properties.setProperty(KafkaConfig.KEY_SERIALIZER_PROPERTY, keySer); 46 | properties.setProperty(KafkaConfig.VAL_SERIALIZER_PROPERTY, valSer); 47 | 48 | return getInstance(properties); 49 | } 50 | 51 | /** 52 | * Gets instance. 53 | * 54 | * @param properties the properties 55 | * @return the instance 56 | */ 57 | public synchronized static KafkaSharedConnPool getInstance(final Properties properties) { 58 | 59 | if (pool.get() == null) 60 | 61 | pool.set(new KafkaSharedConnPool(properties)); 62 | 63 | return pool.get(); 64 | } 65 | 66 | @Override 67 | public Producer getConnection() { 68 | 69 | return producer; 70 | } 71 | 72 | @Override 73 | public void returnConnection(Producer conn) { 74 | 75 | if (conn != null) 76 | 77 | conn.flush(); 78 | } 79 | 80 | @Override 81 | public void invalidateConnection(Producer conn) { 82 | 83 | if (conn != null) 84 | 85 | conn.close(); 86 | } 87 | 88 | /** 89 | * Close. 90 | */ 91 | public void close() { 92 | 93 | producer.flush(); 94 | 95 | producer.close(); 96 | 97 | pool.set(null); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/redis/RedisConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.redis; 17 | 18 | /** 19 | *

RedisConfig

20 | *

Redis配置

21 | * 22 | * @author Victor 23 | * @since 1.2.1 24 | */ 25 | public interface RedisConfig { 26 | 27 | /** 28 | * DEFAULT_HOST 29 | */ 30 | public static final String DEFAULT_HOST = "localhost"; 31 | /** 32 | * DEFAULT_PORT 33 | */ 34 | public static final int DEFAULT_PORT = 6379; 35 | /** 36 | * DEFAULT_TIMEOUT 37 | */ 38 | public static final int DEFAULT_TIMEOUT = 2000; 39 | /** 40 | * DEFAULT_DATABASE 41 | */ 42 | public static final int DEFAULT_DATABASE = 0; 43 | /** 44 | * DEFAULT_PASSWORD 45 | */ 46 | public static final String DEFAULT_PASSWORD = null; 47 | /** 48 | * DEFAULT_CLIENTNAME 49 | */ 50 | public static final String DEFAULT_CLIENTNAME = null; 51 | /** 52 | * DEFAULT_MAXATTE 53 | */ 54 | public static final int DEFAULT_MAXATTE = 5; 55 | /** 56 | * ADDRESS_PROPERTY 57 | */ 58 | public static final String ADDRESS_PROPERTY = "address"; 59 | /** 60 | * TIMEOUT_PROPERTY 61 | */ 62 | public static final String TIMEOUT_PROPERTY = "timeout"; 63 | /** 64 | * CONN_TIMEOUT_PROPERTY 65 | */ 66 | public static final String CONN_TIMEOUT_PROPERTY = "connectionTimeout"; 67 | /** 68 | * SO_TIMEOUT_PROPERTY 69 | */ 70 | public static final String SO_TIMEOUT_PROPERTY = "soTimeout"; 71 | /** 72 | * DATABASE_PROPERTY 73 | */ 74 | public static final String DATABASE_PROPERTY = "database"; 75 | /** 76 | * PASSWORD_PROPERTY 77 | */ 78 | public static final String PASSWORD_PROPERTY = "password"; 79 | /** 80 | * CLIENTNAME_PROPERTY 81 | */ 82 | public static final String CLIENTNAME_PROPERTY = "clientName"; 83 | /** 84 | * MASTERNAME_PROPERTY 85 | */ 86 | public static final String MASTERNAME_PROPERTY = "masterName"; 87 | /** 88 | * SENTINELS_PROPERTY 89 | */ 90 | public static final String SENTINELS_PROPERTY = "sentinels"; 91 | /** 92 | * CLUSTER_PROPERTY 93 | */ 94 | public static final String CLUSTER_PROPERTY = "cluster"; 95 | /** 96 | * MAXATTE_PROPERTY 97 | */ 98 | public static final String MAXATTE_PROPERTY = "maxAttempts"; 99 | 100 | } 101 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/redis/RedisConnectionFactoryOld.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.redis; 17 | 18 | import org.apache.commons.pool2.PooledObject; 19 | import org.apache.commons.pool2.impl.DefaultPooledObject; 20 | import org.darkphoenixs.pool.ConnectionException; 21 | import org.darkphoenixs.pool.ConnectionFactory; 22 | import redis.clients.jedis.BinaryJedis; 23 | import redis.clients.jedis.HostAndPort; 24 | import redis.clients.jedis.Jedis; 25 | 26 | import java.util.Properties; 27 | import java.util.concurrent.atomic.AtomicReference; 28 | 29 | /** 30 | *

Title: RedisConnectionFactoryOld

31 | *

Description: Redis连接工厂

32 | * 33 | * @author Victor 34 | * @version 1.0 35 | * @see ConnectionFactory 36 | * @since 2015年9月19日 37 | */ 38 | @Deprecated 39 | class RedisConnectionFactoryOld implements ConnectionFactory { 40 | 41 | /** 42 | * serialVersionUID 43 | */ 44 | private static final long serialVersionUID = 5692815845396189037L; 45 | 46 | /** 47 | * hostAndPort 48 | */ 49 | private final AtomicReference hostAndPort = new AtomicReference(); 50 | /** 51 | * connectionTimeout 52 | */ 53 | private final int connectionTimeout; 54 | /** 55 | * soTimeout 56 | */ 57 | private final int soTimeout; 58 | /** 59 | * password 60 | */ 61 | private final String password; 62 | /** 63 | * database 64 | */ 65 | private final int database; 66 | /** 67 | * clientName 68 | */ 69 | private final String clientName; 70 | 71 | /** 72 | *

Title: RedisConnectionFactoryOld

73 | *

Description: 构造方法

74 | * 75 | * @param host 地址 76 | * @param port 端口 77 | * @param connectionTimeout 连接超时 78 | * @param soTimeout 超时时间 79 | * @param password 密码 80 | * @param database 数据库 81 | * @param clientName 客户端名称 82 | */ 83 | public RedisConnectionFactoryOld(final String host, final int port, final int connectionTimeout, 84 | final int soTimeout, final String password, final int database, final String clientName) { 85 | this.hostAndPort.set(new HostAndPort(host, port)); 86 | this.connectionTimeout = connectionTimeout; 87 | this.soTimeout = soTimeout; 88 | this.password = password; 89 | this.database = database; 90 | this.clientName = clientName; 91 | } 92 | 93 | /** 94 | * @param properties 参数配置 95 | * @since 1.2.1 96 | */ 97 | public RedisConnectionFactoryOld(final Properties properties) { 98 | 99 | String address = properties.getProperty(RedisConfig.ADDRESS_PROPERTY); 100 | if (address == null) 101 | throw new ConnectionException("[" + RedisConfig.ADDRESS_PROPERTY + "] is required !"); 102 | 103 | this.hostAndPort.set(new HostAndPort(address.split(":")[0], Integer.parseInt(address.split(":")[1]))); 104 | 105 | this.connectionTimeout = Integer.parseInt(properties.getProperty(RedisConfig.CONN_TIMEOUT_PROPERTY, String.valueOf(RedisConfig.DEFAULT_TIMEOUT))); 106 | this.soTimeout = Integer.parseInt(properties.getProperty(RedisConfig.SO_TIMEOUT_PROPERTY, String.valueOf(RedisConfig.DEFAULT_TIMEOUT))); 107 | this.database = Integer.parseInt(properties.getProperty(RedisConfig.DATABASE_PROPERTY, String.valueOf(RedisConfig.DEFAULT_DATABASE))); 108 | this.password = properties.getProperty(RedisConfig.PASSWORD_PROPERTY); 109 | this.clientName = properties.getProperty(RedisConfig.CLIENTNAME_PROPERTY); 110 | } 111 | 112 | /** 113 | *

Title: setHostAndPort

114 | *

Description: 设置地址和端口

115 | * 116 | * @param hostAndPort 地址和端口 117 | */ 118 | public void setHostAndPort(final HostAndPort hostAndPort) { 119 | this.hostAndPort.set(hostAndPort); 120 | } 121 | 122 | @Override 123 | public PooledObject makeObject() throws Exception { 124 | 125 | Jedis jedis = this.createConnection(); 126 | 127 | return new DefaultPooledObject(jedis); 128 | } 129 | 130 | @Override 131 | public void destroyObject(PooledObject p) throws Exception { 132 | 133 | BinaryJedis jedis = (BinaryJedis) p.getObject(); 134 | if (!(jedis.isConnected())) 135 | return; 136 | try { 137 | try { 138 | jedis.quit(); 139 | } catch (Exception e) { 140 | } 141 | jedis.disconnect(); 142 | } catch (Exception e) { 143 | } 144 | } 145 | 146 | @Override 147 | public boolean validateObject(PooledObject p) { 148 | 149 | BinaryJedis jedis = (BinaryJedis) p.getObject(); 150 | try { 151 | HostAndPort hostAndPort = (HostAndPort) this.hostAndPort.get(); 152 | 153 | String connectionHost = jedis.getClient().getHost(); 154 | int connectionPort = jedis.getClient().getPort(); 155 | 156 | return ((hostAndPort.getHost().equals(connectionHost)) 157 | && (hostAndPort.getPort() == connectionPort) 158 | && (jedis.isConnected()) && (jedis.ping().equals("PONG"))); 159 | } catch (Exception e) { 160 | } 161 | return false; 162 | } 163 | 164 | @Override 165 | public void activateObject(PooledObject p) throws Exception { 166 | 167 | BinaryJedis jedis = (BinaryJedis) p.getObject(); 168 | if (jedis.getDB().longValue() != this.database) 169 | jedis.select(this.database); 170 | } 171 | 172 | @Override 173 | public void passivateObject(PooledObject p) throws Exception { 174 | // TODO Auto-generated method stub 175 | 176 | } 177 | 178 | @Override 179 | public Jedis createConnection() throws Exception { 180 | 181 | HostAndPort hostAndPort = (HostAndPort) this.hostAndPort.get(); 182 | Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), 183 | this.connectionTimeout, this.soTimeout); 184 | 185 | try { 186 | jedis.connect(); 187 | if (null != this.password) { 188 | jedis.auth(this.password); 189 | } 190 | if (this.database != 0) { 191 | jedis.select(this.database); 192 | } 193 | if (this.clientName != null) { 194 | jedis.clientSetname(this.clientName); 195 | } 196 | } catch (Exception je) { 197 | jedis.close(); 198 | throw je; 199 | } 200 | 201 | return jedis; 202 | } 203 | 204 | } 205 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/redis/RedisConnectionPool.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.darkphoenixs.pool.ConnectionPool; 4 | import org.darkphoenixs.pool.PoolConfig; 5 | import redis.clients.jedis.Jedis; 6 | import redis.clients.jedis.JedisPool; 7 | 8 | import java.util.Properties; 9 | 10 | /** 11 | *

Title: RedisConnectionPool

12 | *

Description: Redis连接池

13 | * 14 | * @author Victor.Zxy 15 | * @version 1.2.3 16 | * @see ConnectionPool 17 | * @since 2016 /8/25 18 | */ 19 | public class RedisConnectionPool implements ConnectionPool { 20 | 21 | private final JedisPool pool; 22 | 23 | /** 24 | * Instantiates a new Redis connection pool. 25 | * 26 | * @param host the host 27 | * @param port the port 28 | */ 29 | public RedisConnectionPool(final String host, final int port) { 30 | 31 | this(new PoolConfig(), host, port); 32 | } 33 | 34 | /** 35 | * Instantiates a new Redis connection pool. 36 | * 37 | * @param poolConfig the pool config 38 | * @param host the host 39 | * @param port the port 40 | */ 41 | public RedisConnectionPool(final PoolConfig poolConfig, final String host, final int port) { 42 | 43 | this(poolConfig, host, port, RedisConfig.DEFAULT_PASSWORD); 44 | } 45 | 46 | /** 47 | * Instantiates a new Redis connection pool. 48 | * 49 | * @param poolConfig the pool config 50 | * @param host the host 51 | * @param port the port 52 | * @param password the password 53 | */ 54 | public RedisConnectionPool(final PoolConfig poolConfig, final String host, final int port, final String password) { 55 | 56 | this(poolConfig, host, port, password, 57 | RedisConfig.DEFAULT_TIMEOUT); 58 | } 59 | 60 | /** 61 | * Instantiates a new Redis connection pool. 62 | * 63 | * @param poolConfig the pool config 64 | * @param host the host 65 | * @param port the port 66 | * @param password the password 67 | * @param timeout the timeout 68 | */ 69 | public RedisConnectionPool(final PoolConfig poolConfig, final String host, final int port, final String password, final int timeout) { 70 | 71 | this(poolConfig, host, port, timeout, password, 72 | RedisConfig.DEFAULT_DATABASE, 73 | RedisConfig.DEFAULT_CLIENTNAME); 74 | } 75 | 76 | /** 77 | * Instantiates a new Redis connection pool. 78 | * 79 | * @param properties the properties 80 | */ 81 | public RedisConnectionPool(final Properties properties) { 82 | 83 | this(new PoolConfig(), properties); 84 | } 85 | 86 | /** 87 | * Instantiates a new Redis connection pool. 88 | * 89 | * @param poolConfig the pool config 90 | * @param properties the properties 91 | */ 92 | public RedisConnectionPool(final PoolConfig poolConfig, final Properties properties) { 93 | 94 | this(poolConfig, 95 | properties.getProperty(RedisConfig.ADDRESS_PROPERTY).split(":")[0], 96 | Integer.parseInt(properties.getProperty(RedisConfig.ADDRESS_PROPERTY).split(":")[1]), 97 | Integer.parseInt(properties.getProperty(RedisConfig.TIMEOUT_PROPERTY, String.valueOf(RedisConfig.DEFAULT_TIMEOUT))), 98 | properties.getProperty(RedisConfig.PASSWORD_PROPERTY), 99 | Integer.parseInt(properties.getProperty(RedisConfig.DATABASE_PROPERTY, String.valueOf(RedisConfig.DEFAULT_DATABASE))), 100 | properties.getProperty(RedisConfig.CLIENTNAME_PROPERTY)); 101 | 102 | } 103 | 104 | /** 105 | * Instantiates a new Redis connection pool. 106 | * 107 | * @param poolConfig the pool config 108 | * @param host the host 109 | * @param port the port 110 | * @param timeout the timeout 111 | * @param password the password 112 | * @param database the database 113 | * @param clientName the client name 114 | */ 115 | public RedisConnectionPool(final PoolConfig poolConfig, 116 | final String host, 117 | final int port, 118 | final int timeout, 119 | final String password, 120 | final int database, 121 | final String clientName) { 122 | 123 | this(poolConfig, host, port, timeout, timeout, password, database, clientName); 124 | } 125 | 126 | /** 127 | * Instantiates a new Redis connection pool. 128 | * 129 | * @param poolConfig the pool config 130 | * @param host the host 131 | * @param port the port 132 | * @param connectionTimeout the connection timeout 133 | * @param soTimeout the so timeout 134 | * @param password the password 135 | * @param database the database 136 | * @param clientName the client name 137 | */ 138 | public RedisConnectionPool(final PoolConfig poolConfig, 139 | final String host, 140 | final int port, 141 | final int connectionTimeout, 142 | final int soTimeout, 143 | final String password, 144 | final int database, 145 | final String clientName) { 146 | 147 | this.pool = new JedisPool(poolConfig, host, port, connectionTimeout, soTimeout, password, database, clientName, false, null, null, null); 148 | } 149 | 150 | @Override 151 | public Jedis getConnection() { 152 | 153 | return pool.getResource(); 154 | } 155 | 156 | @Override 157 | public void returnConnection(Jedis conn) { 158 | 159 | if (conn != null) 160 | 161 | conn.close(); 162 | } 163 | 164 | @Override 165 | public void invalidateConnection(Jedis conn) { 166 | 167 | if (conn != null) 168 | 169 | conn.close(); 170 | } 171 | 172 | /** 173 | * Close. 174 | */ 175 | public void close() { 176 | 177 | pool.close(); 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/redis/RedisConnectionPoolOld.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.redis; 17 | 18 | import org.darkphoenixs.pool.ConnectionPool; 19 | import org.darkphoenixs.pool.PoolBase; 20 | import org.darkphoenixs.pool.PoolConfig; 21 | import redis.clients.jedis.Client; 22 | import redis.clients.jedis.Jedis; 23 | 24 | import java.util.Properties; 25 | 26 | /** 27 | *

Title: RedisConnectionPoolOld

28 | *

Description: Redis连接池

29 | * 30 | * @author Victor 31 | * @version 1.0 32 | * @see ConnectionPool 33 | * @see PoolBase 34 | * @since 2015年9月19日 35 | */ 36 | @Deprecated 37 | public class RedisConnectionPoolOld extends PoolBase implements ConnectionPool { 38 | 39 | /** 40 | * serialVersionUID 41 | */ 42 | private static final long serialVersionUID = -7068775808649440797L; 43 | 44 | /** 45 | *

Title: RedisConnectionPoolOld

46 | *

Description: 默认构造方法

47 | */ 48 | public RedisConnectionPoolOld() { 49 | this(RedisConfig.DEFAULT_HOST, RedisConfig.DEFAULT_PORT); 50 | } 51 | 52 | /** 53 | *

Title: RedisConnectionPoolOld

54 | *

Description: 构造方法

55 | * 56 | * @param poolConfig 池配置 57 | * @param host 地址 58 | */ 59 | public RedisConnectionPoolOld(final PoolConfig poolConfig, final String host) { 60 | this(poolConfig, host, RedisConfig.DEFAULT_PORT, RedisConfig.DEFAULT_TIMEOUT, RedisConfig.DEFAULT_PASSWORD); 61 | } 62 | 63 | /** 64 | *

Title: RedisConnectionPoolOld

65 | *

Description: 构造方法

66 | * 67 | * @param host 地址 68 | * @param port 端口 69 | */ 70 | public RedisConnectionPoolOld(final String host, final int port) { 71 | this(new PoolConfig(), host, port); 72 | } 73 | 74 | /** 75 | *

Title: RedisConnectionPoolOld

76 | *

Description: 构造方法

77 | * 78 | * @param poolConfig 池配置 79 | * @param host 地址 80 | * @param port 端口 81 | * @param timeout 超时 82 | * @param password 密码 83 | */ 84 | public RedisConnectionPoolOld(final PoolConfig poolConfig, final String host, final int port, 85 | final int timeout, final String password) { 86 | this(poolConfig, host, port, timeout, password, RedisConfig.DEFAULT_DATABASE, RedisConfig.DEFAULT_CLIENTNAME); 87 | } 88 | 89 | /** 90 | *

Title: RedisConnectionPoolOld

91 | *

Description: 构造方法

92 | * 93 | * @param poolConfig 池配置 94 | * @param host 地址 95 | * @param port 端口 96 | */ 97 | public RedisConnectionPoolOld(final PoolConfig poolConfig, final String host, final int port) { 98 | this(poolConfig, host, port, RedisConfig.DEFAULT_TIMEOUT); 99 | } 100 | 101 | /** 102 | *

Title: RedisConnectionPoolOld

103 | *

Description: 构造方法

104 | * 105 | * @param poolConfig 池配置 106 | * @param host 地址 107 | * @param port 端口 108 | * @param timeout 超时 109 | */ 110 | public RedisConnectionPoolOld(final PoolConfig poolConfig, final String host, final int port, 111 | final int timeout) { 112 | this(poolConfig, host, port, timeout, RedisConfig.DEFAULT_PASSWORD, RedisConfig.DEFAULT_DATABASE); 113 | } 114 | 115 | /** 116 | *

Title: RedisConnectionPoolOld

117 | *

Description: 构造方法

118 | * 119 | * @param poolConfig 池配置 120 | * @param host 地址 121 | * @param port 端口 122 | * @param timeout 超时 123 | * @param password 密码 124 | * @param database 数据库 125 | */ 126 | public RedisConnectionPoolOld(final PoolConfig poolConfig, final String host, final int port, 127 | final int timeout, final String password, final int database) { 128 | this(poolConfig, host, port, timeout, password, database, RedisConfig.DEFAULT_CLIENTNAME); 129 | } 130 | 131 | /** 132 | *

Title: RedisConnectionPoolOld

133 | *

Description: 构造方法

134 | * 135 | * @param poolConfig 池配置 136 | * @param host 地址 137 | * @param port 端口 138 | * @param timeout 超时 139 | * @param password 密码 140 | * @param database 数据库 141 | * @param clientName 客户端名称 142 | */ 143 | public RedisConnectionPoolOld(final PoolConfig poolConfig, final String host, final int port, 144 | final int timeout, final String password, final int database, final String clientName) { 145 | this(poolConfig, host, port, timeout, timeout, password, database, 146 | clientName); 147 | } 148 | 149 | /** 150 | *

Title: RedisConnectionPoolOld

151 | *

Description: 构造方法

152 | * 153 | * @param poolConfig 池配置 154 | * @param host 地址 155 | * @param port 端口 156 | * @param connectionTimeout 连接超时 157 | * @param soTimeout 超时时间 158 | * @param password 密码 159 | * @param database 数据库 160 | * @param clientName 客户端名称 161 | */ 162 | public RedisConnectionPoolOld(final PoolConfig poolConfig, final String host, final int port, 163 | final int connectionTimeout, final int soTimeout, final String password, 164 | final int database, final String clientName) { 165 | super(poolConfig, new RedisConnectionFactoryOld(host, port, connectionTimeout, 166 | soTimeout, password, database, clientName)); 167 | } 168 | 169 | /** 170 | * @param poolConfig 池配置 171 | * @param properties 参数配置 172 | * @since 1.2.1 173 | */ 174 | public RedisConnectionPoolOld(final PoolConfig poolConfig, final Properties properties) { 175 | 176 | super(poolConfig, new RedisConnectionFactoryOld(properties)); 177 | } 178 | 179 | @Override 180 | public Jedis getConnection() { 181 | 182 | return super.getResource(); 183 | } 184 | 185 | @Override 186 | public void returnConnection(Jedis conn) { 187 | 188 | Client client = conn.getClient(); 189 | 190 | if (client.isBroken()) 191 | 192 | super.invalidateResource(conn); 193 | else 194 | super.returnResource(conn); 195 | } 196 | 197 | @Override 198 | public void invalidateConnection(Jedis conn) { 199 | 200 | super.invalidateResource(conn); 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/redis/RedisSentinelConnPool.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.darkphoenixs.pool.ConnectionPool; 4 | import org.darkphoenixs.pool.PoolConfig; 5 | import redis.clients.jedis.Jedis; 6 | import redis.clients.jedis.JedisSentinelPool; 7 | 8 | import java.util.Arrays; 9 | import java.util.HashSet; 10 | import java.util.Properties; 11 | import java.util.Set; 12 | 13 | /** 14 | *

Title: RedisSentinelConnPool

15 | *

Description: Redis哨兵连接池

16 | * 17 | * @author Victor.Zxy 18 | * @version 1.2.3 19 | * @see ConnectionPool 20 | * @since 2016 /8/25 21 | */ 22 | public class RedisSentinelConnPool implements ConnectionPool { 23 | 24 | private final JedisSentinelPool pool; 25 | 26 | /** 27 | * Instantiates a new Redis sentinel conn pool(For test). 28 | * 29 | * @param pool the pool 30 | */ 31 | protected RedisSentinelConnPool(final JedisSentinelPool pool) { 32 | this.pool = pool; 33 | } 34 | 35 | /** 36 | * Instantiates a new Redis sentinel conn pool. 37 | * 38 | * @param masterName the master name 39 | * @param sentinels the sentinels 40 | */ 41 | public RedisSentinelConnPool(final String masterName, final Set sentinels) { 42 | 43 | this(new PoolConfig(), masterName, sentinels); 44 | } 45 | 46 | /** 47 | * Instantiates a new Redis sentinel conn pool. 48 | * 49 | * @param poolConfig the pool config 50 | * @param masterName the master name 51 | * @param sentinels the sentinels 52 | */ 53 | public RedisSentinelConnPool(final PoolConfig poolConfig, 54 | final String masterName, 55 | final Set sentinels) { 56 | 57 | this(poolConfig, masterName, sentinels, RedisConfig.DEFAULT_PASSWORD); 58 | } 59 | 60 | /** 61 | * Instantiates a new Redis sentinel conn pool. 62 | * 63 | * @param poolConfig the pool config 64 | * @param masterName the master name 65 | * @param sentinels the sentinels 66 | * @param password the password 67 | */ 68 | public RedisSentinelConnPool(final PoolConfig poolConfig, 69 | final String masterName, 70 | final Set sentinels, 71 | final String password) { 72 | 73 | this(poolConfig, masterName, sentinels, password, RedisConfig.DEFAULT_TIMEOUT); 74 | } 75 | 76 | /** 77 | * Instantiates a new Redis sentinel conn pool. 78 | * 79 | * @param poolConfig the pool config 80 | * @param masterName the master name 81 | * @param sentinels the sentinels 82 | * @param password the password 83 | * @param timeout the timeout 84 | */ 85 | public RedisSentinelConnPool(final PoolConfig poolConfig, 86 | final String masterName, 87 | final Set sentinels, 88 | final String password, 89 | final int timeout) { 90 | 91 | this(poolConfig, masterName, sentinels, timeout, timeout, password, 92 | RedisConfig.DEFAULT_DATABASE, 93 | RedisConfig.DEFAULT_CLIENTNAME); 94 | } 95 | 96 | /** 97 | * Instantiates a new Redis sentinel conn pool. 98 | * 99 | * @param properties the properties 100 | */ 101 | public RedisSentinelConnPool(final Properties properties) { 102 | 103 | this(new PoolConfig(), properties); 104 | } 105 | 106 | /** 107 | * Instantiates a new Redis sentinel conn pool. 108 | * 109 | * @param poolConfig the pool config 110 | * @param properties the properties 111 | */ 112 | public RedisSentinelConnPool(final PoolConfig poolConfig, final Properties properties) { 113 | 114 | this(poolConfig, 115 | properties.getProperty(RedisConfig.MASTERNAME_PROPERTY), 116 | new HashSet(Arrays.asList(properties.getProperty(RedisConfig.SENTINELS_PROPERTY).split(","))), 117 | Integer.parseInt(properties.getProperty(RedisConfig.TIMEOUT_PROPERTY, String.valueOf(RedisConfig.DEFAULT_TIMEOUT))), 118 | Integer.parseInt(properties.getProperty(RedisConfig.TIMEOUT_PROPERTY, String.valueOf(RedisConfig.DEFAULT_TIMEOUT))), 119 | properties.getProperty(RedisConfig.PASSWORD_PROPERTY), 120 | Integer.parseInt(properties.getProperty(RedisConfig.DATABASE_PROPERTY, String.valueOf(RedisConfig.DEFAULT_DATABASE))), 121 | properties.getProperty(RedisConfig.CLIENTNAME_PROPERTY)); 122 | } 123 | 124 | /** 125 | * Instantiates a new Redis sentinel conn pool. 126 | * 127 | * @param poolConfig the pool config 128 | * @param masterName the master name 129 | * @param sentinels the sentinels 130 | * @param connectionTimeout the connection timeout 131 | * @param soTimeout the so timeout 132 | * @param password the password 133 | * @param database the database 134 | * @param clientName the client name 135 | */ 136 | public RedisSentinelConnPool(final PoolConfig poolConfig, 137 | final String masterName, 138 | final Set sentinels, 139 | final int connectionTimeout, 140 | final int soTimeout, 141 | final String password, 142 | final int database, 143 | final String clientName) { 144 | 145 | this.pool = new JedisSentinelPool(masterName, sentinels, poolConfig, connectionTimeout, soTimeout, password, database, clientName); 146 | } 147 | 148 | @Override 149 | public Jedis getConnection() { 150 | 151 | return pool.getResource(); 152 | } 153 | 154 | @Override 155 | public void returnConnection(Jedis conn) { 156 | 157 | if (conn != null) 158 | 159 | conn.close(); 160 | } 161 | 162 | @Override 163 | public void invalidateConnection(Jedis conn) { 164 | 165 | if (conn != null) 166 | 167 | conn.close(); 168 | } 169 | 170 | /** 171 | * Close. 172 | */ 173 | public void close() { 174 | 175 | pool.close(); 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/redis/RedisShardedConnPool.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.darkphoenixs.pool.ConnectionPool; 4 | import org.darkphoenixs.pool.PoolConfig; 5 | import redis.clients.jedis.JedisShardInfo; 6 | import redis.clients.jedis.ShardedJedis; 7 | import redis.clients.jedis.ShardedJedisPool; 8 | import redis.clients.util.Hashing; 9 | 10 | import java.util.List; 11 | import java.util.regex.Pattern; 12 | 13 | /** 14 | *

Title: RedisShardedConnPool

15 | *

Description: Redis分片连接池

16 | * 17 | * @author Victor.Zxy 18 | * @version 1.2.3 19 | * @see ConnectionPool 20 | * @since 2016 /8/25 21 | */ 22 | public class RedisShardedConnPool implements ConnectionPool { 23 | 24 | private final ShardedJedisPool pool; 25 | 26 | /** 27 | * Instantiates a new Redis sharded conn pool. 28 | * 29 | * @param poolConfig the pool config 30 | * @param shards the shards 31 | */ 32 | public RedisShardedConnPool(final PoolConfig poolConfig, 33 | final List shards) { 34 | 35 | this(poolConfig, shards, Hashing.MURMUR_HASH); 36 | } 37 | 38 | /** 39 | * Instantiates a new Redis sharded conn pool. 40 | * 41 | * @param poolConfig the pool config 42 | * @param shards the shards 43 | * @param algo the algo 44 | */ 45 | public RedisShardedConnPool(final PoolConfig poolConfig, 46 | final List shards, 47 | final Hashing algo) { 48 | 49 | this(poolConfig, shards, algo, null); 50 | } 51 | 52 | /** 53 | * Instantiates a new Redis sharded conn pool. 54 | * 55 | * @param poolConfig the pool config 56 | * @param shards the shards 57 | * @param keyTagPattern the key tag pattern 58 | */ 59 | public RedisShardedConnPool(final PoolConfig poolConfig, 60 | final List shards, 61 | final Pattern keyTagPattern) { 62 | 63 | this(poolConfig, shards, Hashing.MURMUR_HASH, keyTagPattern); 64 | } 65 | 66 | /** 67 | * Instantiates a new Redis sharded conn pool. 68 | * 69 | * @param poolConfig the pool config 70 | * @param shards the shards 71 | * @param algo the algo 72 | * @param keyTagPattern the key tag pattern 73 | */ 74 | public RedisShardedConnPool(final PoolConfig poolConfig, 75 | final List shards, 76 | final Hashing algo, 77 | final Pattern keyTagPattern) { 78 | 79 | this.pool = new ShardedJedisPool(poolConfig, shards, algo, keyTagPattern); 80 | } 81 | 82 | @Override 83 | public ShardedJedis getConnection() { 84 | 85 | return pool.getResource(); 86 | } 87 | 88 | @Override 89 | public void returnConnection(ShardedJedis conn) { 90 | 91 | if (conn != null) 92 | 93 | conn.close(); 94 | } 95 | 96 | @Override 97 | public void invalidateConnection(ShardedJedis conn) { 98 | 99 | if (conn != null) 100 | 101 | conn.close(); 102 | } 103 | 104 | /** 105 | * Close. 106 | */ 107 | public void close() { 108 | 109 | pool.close(); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/redis/RedisShardedConnPoolOld.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.redis; 17 | 18 | import org.apache.commons.pool2.PooledObject; 19 | import org.apache.commons.pool2.impl.DefaultPooledObject; 20 | import org.darkphoenixs.pool.ConnectionFactory; 21 | import org.darkphoenixs.pool.ConnectionPool; 22 | import org.darkphoenixs.pool.PoolBase; 23 | import org.darkphoenixs.pool.PoolConfig; 24 | import redis.clients.jedis.Jedis; 25 | import redis.clients.jedis.JedisShardInfo; 26 | import redis.clients.jedis.ShardedJedis; 27 | import redis.clients.util.Hashing; 28 | 29 | import java.util.List; 30 | import java.util.regex.Pattern; 31 | 32 | /** 33 | *

Title: RedisShardedConnPoolOld

34 | *

Description: Redis分片连接池

35 | * 36 | * @author Victor 37 | * @version 1.0 38 | * @see PoolBase 39 | * @see ConnectionPool 40 | * @since 2015年9月19日 41 | */ 42 | @Deprecated 43 | public class RedisShardedConnPoolOld extends PoolBase implements 44 | ConnectionPool { 45 | 46 | /** 47 | * serialVersionUID 48 | */ 49 | private static final long serialVersionUID = -273019562990831722L; 50 | 51 | /** 52 | *

Title: RedisShardedConnPoolOld

53 | *

Description: 构造方法

54 | * 55 | * @param poolConfig 池配置 56 | * @param shards 分片列表 57 | */ 58 | public RedisShardedConnPoolOld(final PoolConfig poolConfig, 59 | List shards) { 60 | this(poolConfig, shards, Hashing.MURMUR_HASH); 61 | } 62 | 63 | /** 64 | *

Title: RedisShardedConnPoolOld

65 | *

Description: 构造方法

66 | * 67 | * @param poolConfig 池配置 68 | * @param shards 分片列表 69 | * @param algo 数字哈希 70 | */ 71 | public RedisShardedConnPoolOld(final PoolConfig poolConfig, 72 | List shards, Hashing algo) { 73 | this(poolConfig, shards, algo, null); 74 | } 75 | 76 | /** 77 | *

Title: RedisShardedConnPoolOld

78 | *

Description: 构造方法

79 | * 80 | * @param poolConfig 池配置 81 | * @param shards 分片列表 82 | * @param keyTagPattern 键表达式 83 | */ 84 | public RedisShardedConnPoolOld(final PoolConfig poolConfig, 85 | List shards, Pattern keyTagPattern) { 86 | this(poolConfig, shards, Hashing.MURMUR_HASH, keyTagPattern); 87 | } 88 | 89 | /** 90 | *

Title: RedisShardedConnPoolOld

91 | *

Description: 构造方法

92 | * 93 | * @param poolConfig 池配置 94 | * @param shards 分片列表 95 | * @param algo 数字哈希 96 | * @param keyTagPattern 键表达式 97 | */ 98 | public RedisShardedConnPoolOld(final PoolConfig poolConfig, 99 | List shards, Hashing algo, Pattern keyTagPattern) { 100 | initPool(poolConfig, new RedisShardedConnFactory(shards, algo, 101 | keyTagPattern)); 102 | } 103 | 104 | @Override 105 | public ShardedJedis getConnection() { 106 | 107 | return super.getResource(); 108 | } 109 | 110 | @Override 111 | public void returnConnection(ShardedJedis conn) { 112 | 113 | boolean broken = false; 114 | 115 | for (Jedis jedis : conn.getAllShards()) 116 | if (jedis.getClient().isBroken()) { 117 | broken = true; 118 | break; 119 | } 120 | 121 | if (broken) 122 | super.invalidateResource(conn); 123 | else 124 | super.returnResource(conn); 125 | } 126 | 127 | @Override 128 | public void invalidateConnection(ShardedJedis conn) { 129 | 130 | super.invalidateResource(conn); 131 | } 132 | 133 | /** 134 | *

Title: RedisShardedConnFactory

135 | *

Description: Redis分片连接工厂

136 | * 137 | * @author Victor 138 | * @version 1.0 139 | * @see ConnectionFactory 140 | * @since 2015年9月19日 141 | */ 142 | protected class RedisShardedConnFactory implements 143 | ConnectionFactory { 144 | 145 | /** 146 | * serialVersionUID 147 | */ 148 | private static final long serialVersionUID = -8620587542840002906L; 149 | /** 150 | * shards 151 | */ 152 | private List shards; 153 | /** 154 | * algo 155 | */ 156 | private Hashing algo; 157 | /** 158 | * keyTagPattern 159 | */ 160 | private Pattern keyTagPattern; 161 | 162 | /** 163 | *

Title: RedisShardedConnFactory

164 | *

Description: 构造方法

165 | * 166 | * @param shards 分片列表 167 | * @param algo 数字哈希 168 | * @param keyTagPattern 键表达式 169 | */ 170 | public RedisShardedConnFactory(List shards, 171 | Hashing algo, Pattern keyTagPattern) { 172 | this.shards = shards; 173 | this.algo = algo; 174 | this.keyTagPattern = keyTagPattern; 175 | } 176 | 177 | @Override 178 | public PooledObject makeObject() throws Exception { 179 | ShardedJedis jedis = this.createConnection(); 180 | return new DefaultPooledObject(jedis); 181 | } 182 | 183 | @Override 184 | public void destroyObject(PooledObject pooledShardedJedis) 185 | throws Exception { 186 | final ShardedJedis shardedJedis = pooledShardedJedis.getObject(); 187 | for (Jedis jedis : shardedJedis.getAllShards()) { 188 | try { 189 | try { 190 | jedis.quit(); 191 | } catch (Exception e) { 192 | 193 | } 194 | jedis.disconnect(); 195 | } catch (Exception e) { 196 | 197 | } 198 | } 199 | } 200 | 201 | @Override 202 | public boolean validateObject( 203 | PooledObject pooledShardedJedis) { 204 | try { 205 | ShardedJedis jedis = pooledShardedJedis.getObject(); 206 | for (Jedis shard : jedis.getAllShards()) { 207 | if (!shard.ping().equals("PONG")) { 208 | return false; 209 | } 210 | } 211 | return true; 212 | } catch (Exception ex) { 213 | return false; 214 | } 215 | } 216 | 217 | @Override 218 | public void activateObject(PooledObject p) 219 | throws Exception { 220 | 221 | } 222 | 223 | @Override 224 | public void passivateObject(PooledObject p) 225 | throws Exception { 226 | 227 | } 228 | 229 | @Override 230 | public ShardedJedis createConnection() throws Exception { 231 | ShardedJedis jedis = new ShardedJedis(shards, algo, keyTagPattern); 232 | return jedis; 233 | } 234 | } 235 | 236 | } 237 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/socket/SocketConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.socket; 17 | 18 | /** 19 | *

SocketConfig

20 | *

Socket配置

21 | * 22 | * @author Victor 23 | * @since 1.2.1 24 | */ 25 | public interface SocketConfig { 26 | 27 | /** 28 | * DEFAULT_HOST 29 | */ 30 | public static final String DEFAULT_HOST = "localhost"; 31 | /** 32 | * DEFAULT_PORT 33 | */ 34 | public static final int DEFAULT_PORT = 1234; 35 | /** 36 | * DEFAULT_TIMEOUT 37 | */ 38 | public static final int DEFAULT_TIMEOUT = 2000; 39 | /** 40 | * DEFAULT_BUFFERSIZE 41 | */ 42 | public static final int DEFAULT_BUFFERSIZE = 3 * 1024; 43 | /** 44 | * DEFAULT_LINGER 45 | */ 46 | public static final int DEFAULT_LINGER = 0; 47 | /** 48 | * DEFAULT_KEEPALIVE 49 | */ 50 | public static final boolean DEFAULT_KEEPALIVE = false; 51 | /** 52 | * DEFAULT_TCPNODELAY 53 | */ 54 | public static final boolean DEFAULT_TCPNODELAY = false; 55 | /** 56 | * DEFAULT_PERFORMANCE 57 | */ 58 | public static final String[] DEFAULT_PERFORMANCE = null; 59 | 60 | /** 61 | * ADDRESS_PROPERTY 62 | */ 63 | public static final String ADDRESS_PROPERTY = "address"; 64 | /** 65 | * RECE_BUFFERSIZE_PROPERTY 66 | */ 67 | public static final String RECE_BUFFERSIZE_PROPERTY = "receiveBufferSize"; 68 | /** 69 | * SEND_BUFFERSIZE_PROPERTY 70 | */ 71 | public static final String SEND_BUFFERSIZE_PROPERTY = "sendBufferSize"; 72 | /** 73 | * CONN_TIMEOUT_PROPERTY 74 | */ 75 | public static final String CONN_TIMEOUT_PROPERTY = "connectionTimeout"; 76 | /** 77 | * SO_TIMEOUT_PROPERTY 78 | */ 79 | public static final String SO_TIMEOUT_PROPERTY = "soTimeout"; 80 | /** 81 | * LINGER_PROPERTY 82 | */ 83 | public static final String LINGER_PROPERTY = "linger"; 84 | /** 85 | * KEEPALIVE_PROPERTY 86 | */ 87 | public static final String KEEPALIVE_PROPERTY = "keepAlive"; 88 | /** 89 | * TCPNODELAY_PROPERTY 90 | */ 91 | public static final String TCPNODELAY_PROPERTY = "tcpNoDelay"; 92 | /** 93 | * PERFORMANCE_PROPERTY 94 | */ 95 | public static final String PERFORMANCE_PROPERTY = "performance"; 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/socket/SocketConnectionFactory.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.socket; 17 | 18 | import org.apache.commons.pool2.PooledObject; 19 | import org.apache.commons.pool2.impl.DefaultPooledObject; 20 | import org.darkphoenixs.pool.ConnectionException; 21 | import org.darkphoenixs.pool.ConnectionFactory; 22 | 23 | import java.net.InetSocketAddress; 24 | import java.net.Socket; 25 | import java.util.Properties; 26 | 27 | /** 28 | *

SocketConnectionFactory

29 | *

Socket连接工厂

30 | * 31 | * @author Victor.Zxy 32 | * @see ConnectionFactory 33 | * @since 1.2.1 34 | */ 35 | class SocketConnectionFactory implements ConnectionFactory { 36 | 37 | /** 38 | * serialVersionUID 39 | */ 40 | private static final long serialVersionUID = -727722488965747494L; 41 | 42 | /** 43 | * address 44 | */ 45 | private final InetSocketAddress socketAddress; 46 | /** 47 | * receiveBufferSize 48 | */ 49 | private final int receiveBufferSize; 50 | /** 51 | * sendBufferSize 52 | */ 53 | private final int sendBufferSize; 54 | /** 55 | * connectionTimeout 56 | */ 57 | private final int connectionTimeout; 58 | /** 59 | * soTimeout 60 | */ 61 | private final int soTimeout; 62 | /** 63 | * keepAlive 64 | */ 65 | private final boolean keepAlive; 66 | /** 67 | * tcpNoDelay 68 | */ 69 | private final boolean tcpNoDelay; 70 | /** 71 | * performance 72 | */ 73 | private final String[] performance; 74 | /** 75 | * linger 76 | */ 77 | private int linger; 78 | 79 | /** 80 | * @param properties 连接属性 81 | */ 82 | public SocketConnectionFactory(final Properties properties) { 83 | 84 | String address = properties.getProperty(SocketConfig.ADDRESS_PROPERTY); 85 | if (address == null) 86 | throw new ConnectionException("[" + SocketConfig.ADDRESS_PROPERTY + "] is required !"); 87 | 88 | this.socketAddress = new InetSocketAddress(address.split(":")[0], Integer.parseInt(address.split(":")[1])); 89 | 90 | this.receiveBufferSize = Integer.parseInt(properties.getProperty(SocketConfig.RECE_BUFFERSIZE_PROPERTY, "0")); 91 | this.sendBufferSize = Integer.parseInt(properties.getProperty(SocketConfig.SEND_BUFFERSIZE_PROPERTY, "0")); 92 | this.connectionTimeout = Integer.parseInt(properties.getProperty(SocketConfig.CONN_TIMEOUT_PROPERTY, "0")); 93 | this.soTimeout = Integer.parseInt(properties.getProperty(SocketConfig.SO_TIMEOUT_PROPERTY, "0")); 94 | this.linger = Integer.parseInt(properties.getProperty(SocketConfig.LINGER_PROPERTY, "0")); 95 | this.keepAlive = Boolean.valueOf(properties.getProperty(SocketConfig.KEEPALIVE_PROPERTY, "false")); 96 | this.tcpNoDelay = Boolean.valueOf(properties.getProperty(SocketConfig.TCPNODELAY_PROPERTY, "false")); 97 | this.performance = (properties.getProperty(SocketConfig.PERFORMANCE_PROPERTY) != null) ? properties.getProperty(SocketConfig.PERFORMANCE_PROPERTY).split(",") : null; 98 | } 99 | 100 | /** 101 | * @param host 地址 102 | * @param port 端口 103 | * @param receiveBufferSize 接收缓存大小 104 | * @param sendBufferSize 发送缓存大小 105 | * @param connectionTimeout 连接超时 106 | * @param soTimeout 接收超时 107 | * @param linger 逗留时间 108 | * @param keepAlive 保持活动 109 | * @param tcpNoDelay 不延迟 110 | * @param performance 性能属性 111 | */ 112 | public SocketConnectionFactory(final String host, final int port, 113 | final int receiveBufferSize, final int sendBufferSize, 114 | final int connectionTimeout, final int soTimeout, final int linger, 115 | final boolean keepAlive, final boolean tcpNoDelay, final String[] performance) { 116 | 117 | this.socketAddress = new InetSocketAddress(host, port); 118 | this.receiveBufferSize = receiveBufferSize; 119 | this.sendBufferSize = sendBufferSize; 120 | this.connectionTimeout = connectionTimeout; 121 | this.soTimeout = soTimeout; 122 | this.linger = linger; 123 | this.keepAlive = keepAlive; 124 | this.tcpNoDelay = tcpNoDelay; 125 | this.performance = performance; 126 | } 127 | 128 | @Override 129 | public PooledObject makeObject() throws Exception { 130 | 131 | Socket socket = this.createConnection(); 132 | 133 | return new DefaultPooledObject(socket); 134 | } 135 | 136 | @Override 137 | public void destroyObject(PooledObject p) throws Exception { 138 | 139 | Socket socket = p.getObject(); 140 | 141 | if (socket != null) 142 | 143 | socket.close(); 144 | } 145 | 146 | @Override 147 | public boolean validateObject(PooledObject p) { 148 | 149 | Socket socket = p.getObject(); 150 | 151 | if (socket != null) 152 | 153 | return (socket.isConnected()) && (!socket.isClosed()); 154 | 155 | return false; 156 | } 157 | 158 | @Override 159 | public void activateObject(PooledObject p) throws Exception { 160 | // TODO Auto-generated method stub 161 | 162 | } 163 | 164 | @Override 165 | public void passivateObject(PooledObject p) throws Exception { 166 | // TODO Auto-generated method stub 167 | 168 | } 169 | 170 | @Override 171 | public Socket createConnection() throws Exception { 172 | 173 | Socket socket = new Socket(); 174 | 175 | try { 176 | if (sendBufferSize > 0) 177 | socket.setSendBufferSize(sendBufferSize); 178 | 179 | if (receiveBufferSize > 0) 180 | socket.setReceiveBufferSize(receiveBufferSize); 181 | 182 | if (soTimeout > 0) 183 | socket.setSoTimeout(soTimeout); 184 | 185 | if (linger > 0) 186 | socket.setSoLinger(true, linger); 187 | 188 | if (keepAlive) 189 | socket.setKeepAlive(keepAlive); 190 | 191 | if (tcpNoDelay) 192 | socket.setTcpNoDelay(tcpNoDelay); 193 | 194 | if (performance != null) 195 | socket.setPerformancePreferences(Integer.parseInt(performance[0]), Integer.parseInt(performance[1]), Integer.parseInt(performance[2])); 196 | 197 | socket.connect(socketAddress, connectionTimeout); 198 | 199 | } catch (Exception se) { 200 | socket.close(); 201 | throw se; 202 | } 203 | 204 | return socket; 205 | } 206 | 207 | } 208 | -------------------------------------------------------------------------------- /src/main/java/org/darkphoenixs/pool/socket/SocketConnectionPool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.socket; 17 | 18 | import org.darkphoenixs.pool.ConnectionPool; 19 | import org.darkphoenixs.pool.PoolBase; 20 | import org.darkphoenixs.pool.PoolConfig; 21 | 22 | import java.net.Socket; 23 | import java.util.Properties; 24 | 25 | /** 26 | *

SocketConnectionPool

27 | *

Socket连接池

28 | * 29 | * @author Victor.Zxy 30 | * @see PoolBase 31 | * @see ConnectionPool 32 | * @since 1.2.1 33 | */ 34 | public class SocketConnectionPool extends PoolBase implements ConnectionPool { 35 | 36 | /** 37 | * serialVersionUID 38 | */ 39 | private static final long serialVersionUID = 4231761143726425981L; 40 | 41 | /** 42 | * 默认构造方法 43 | */ 44 | public SocketConnectionPool() { 45 | 46 | this(new PoolConfig(), SocketConfig.DEFAULT_HOST, SocketConfig.DEFAULT_PORT); 47 | } 48 | 49 | /** 50 | * @param properties 参数配置 51 | */ 52 | public SocketConnectionPool(final Properties properties) { 53 | 54 | this(new PoolConfig(), properties); 55 | } 56 | 57 | /** 58 | * @param poolConfig 池配置 59 | * @param properties 参数配置 60 | */ 61 | public SocketConnectionPool(final PoolConfig poolConfig, final Properties properties) { 62 | 63 | super(poolConfig, new SocketConnectionFactory(properties)); 64 | } 65 | 66 | /** 67 | * @param poolConfig 池配置 68 | * @param host 地址 69 | * @param port 端口 70 | */ 71 | public SocketConnectionPool(final PoolConfig poolConfig, final String host, final int port) { 72 | 73 | this(poolConfig, host, port, SocketConfig.DEFAULT_BUFFERSIZE, SocketConfig.DEFAULT_TIMEOUT); 74 | } 75 | 76 | /** 77 | * @param poolConfig 池配置 78 | * @param host 地址 79 | * @param port 端口 80 | * @param bufferSize 缓存大小 81 | * @param timeout 超时时间 82 | */ 83 | public SocketConnectionPool(final PoolConfig poolConfig, final String host, final int port, final int bufferSize, final int timeout) { 84 | 85 | this(poolConfig, host, port, bufferSize, timeout, SocketConfig.DEFAULT_LINGER); 86 | } 87 | 88 | /** 89 | * @param poolConfig 池配置 90 | * @param host 地址 91 | * @param port 端口 92 | * @param bufferSize 缓存大小 93 | * @param timeout 超时时间 94 | * @param linger 逗留时间 95 | */ 96 | public SocketConnectionPool(final PoolConfig poolConfig, final String host, final int port, final int bufferSize, final int timeout, final int linger) { 97 | 98 | this(poolConfig, host, port, bufferSize, timeout, linger, SocketConfig.DEFAULT_KEEPALIVE, SocketConfig.DEFAULT_TCPNODELAY); 99 | } 100 | 101 | /** 102 | * @param poolConfig 池配置 103 | * @param host 地址 104 | * @param port 端口 105 | * @param bufferSize 缓存大小 106 | * @param timeout 超时时间 107 | * @param linger 逗留时间 108 | * @param keepAlive 保持活动 109 | * @param tcpNoDelay 不延迟 110 | */ 111 | public SocketConnectionPool(final PoolConfig poolConfig, final String host, final int port, final int bufferSize, final int timeout, final int linger, final boolean keepAlive, final boolean tcpNoDelay) { 112 | 113 | this(poolConfig, host, port, bufferSize, timeout, linger, keepAlive, tcpNoDelay, SocketConfig.DEFAULT_PERFORMANCE); 114 | } 115 | 116 | /** 117 | * @param poolConfig 池配置 118 | * @param host 地址 119 | * @param port 端口 120 | * @param bufferSize 缓存大小 121 | * @param timeout 超时时间 122 | * @param linger 逗留时间 123 | * @param keepAlive 保持活动 124 | * @param tcpNoDelay 不延迟 125 | * @param performance 性能属性 126 | */ 127 | public SocketConnectionPool(final PoolConfig poolConfig, final String host, final int port, final int bufferSize, final int timeout, final int linger, final boolean keepAlive, final boolean tcpNoDelay, final String[] performance) { 128 | 129 | super(poolConfig, new SocketConnectionFactory(host, port, bufferSize, bufferSize, timeout, timeout, linger, keepAlive, tcpNoDelay, performance)); 130 | } 131 | 132 | @Override 133 | public Socket getConnection() { 134 | 135 | return super.getResource(); 136 | } 137 | 138 | @Override 139 | public void returnConnection(Socket conn) { 140 | 141 | super.returnResource(conn); 142 | } 143 | 144 | @Override 145 | public void invalidateConnection(Socket conn) { 146 | 147 | super.invalidateResource(conn); 148 | } 149 | 150 | } 151 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/NOTICE.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/ConnectionExceptionTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class ConnectionExceptionTest { 7 | 8 | @Test 9 | public void test() throws Exception { 10 | 11 | ConnectionException exp0 = new ConnectionException(); 12 | 13 | Assert.assertNull(exp0.getMessage()); 14 | 15 | ConnectionException exp1 = new ConnectionException("ConnectionException1"); 16 | 17 | Assert.assertEquals("ConnectionException1", exp1.getMessage()); 18 | 19 | Throwable thr2 = new Throwable("ConnectionException2"); 20 | 21 | ConnectionException exp2 = new ConnectionException(thr2); 22 | 23 | Assert.assertEquals(thr2, exp2.getCause()); 24 | 25 | Throwable thr3 = new Throwable("ConnectionException3"); 26 | 27 | ConnectionException exp3 = new ConnectionException("ConnectionException3", thr3); 28 | 29 | Assert.assertEquals("ConnectionException3", exp3.getMessage()); 30 | 31 | Assert.assertEquals(thr3, exp3.getCause()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/ConnectionFactoryTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool; 2 | 3 | import org.apache.commons.pool2.PooledObject; 4 | import org.apache.commons.pool2.impl.DefaultPooledObject; 5 | import org.junit.Test; 6 | 7 | public class ConnectionFactoryTest { 8 | 9 | @Test 10 | public void test() throws Exception { 11 | 12 | ConnFactory factory = new ConnFactory(); 13 | 14 | PooledObject p = factory.makeObject(); 15 | 16 | factory.activateObject(p); 17 | 18 | factory.validateObject(p); 19 | 20 | factory.passivateObject(p); 21 | 22 | factory.destroyObject(p); 23 | } 24 | 25 | private static class ConnFactory implements ConnectionFactory { 26 | 27 | private static final long serialVersionUID = 1L; 28 | 29 | @Override 30 | public PooledObject makeObject() throws Exception { 31 | 32 | return new DefaultPooledObject(createConnection()); 33 | } 34 | 35 | @Override 36 | public void destroyObject(PooledObject p) throws Exception { 37 | 38 | System.out.println("destroyObject " + p.getObject().getId()); 39 | } 40 | 41 | @Override 42 | public boolean validateObject(PooledObject p) { 43 | 44 | System.out.println("validateObject " + p.getObject().getId()); 45 | 46 | return true; 47 | } 48 | 49 | @Override 50 | public void activateObject(PooledObject p) throws Exception { 51 | 52 | System.out.println("activateObject " + p.getObject().getId()); 53 | } 54 | 55 | @Override 56 | public void passivateObject(PooledObject p) throws Exception { 57 | 58 | System.out.println("passivateObject " + p.getObject().getId()); 59 | } 60 | 61 | @Override 62 | public Conn createConnection() throws Exception { 63 | 64 | Conn conn = new Conn(); 65 | 66 | conn.setId(123); 67 | 68 | return conn; 69 | } 70 | 71 | } 72 | 73 | private static class Conn { 74 | 75 | private int id; 76 | 77 | public int getId() { 78 | return id; 79 | } 80 | 81 | public void setId(int id) { 82 | this.id = id; 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/ConnectionPoolTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool; 2 | 3 | import org.junit.Test; 4 | 5 | public class ConnectionPoolTest { 6 | 7 | @Test 8 | public void test() throws Exception { 9 | 10 | ConnPool pool = new ConnPool(); 11 | 12 | Conn conn = pool.getConnection(); 13 | 14 | pool.returnConnection(conn); 15 | 16 | pool.invalidateConnection(conn); 17 | } 18 | 19 | private static class ConnPool implements ConnectionPool { 20 | 21 | /** 22 | * serialVersionUID 23 | */ 24 | private static final long serialVersionUID = 1L; 25 | 26 | @Override 27 | public Conn getConnection() { 28 | 29 | Conn conn = new Conn(); 30 | 31 | conn.setId(123); 32 | 33 | return conn; 34 | } 35 | 36 | @Override 37 | public void returnConnection(Conn conn) { 38 | 39 | System.out.println("returnConnection " + conn.getId()); 40 | } 41 | 42 | @Override 43 | public void invalidateConnection(Conn conn) { 44 | 45 | System.out.println("invalidateConnection " + conn.getId()); 46 | } 47 | 48 | } 49 | 50 | private static class Conn { 51 | 52 | private int id; 53 | 54 | public int getId() { 55 | return id; 56 | } 57 | 58 | public void setId(int id) { 59 | this.id = id; 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/PoolBaseTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool; 2 | 3 | import org.apache.commons.pool2.PooledObject; 4 | import org.apache.commons.pool2.impl.DefaultPooledObject; 5 | import org.junit.Test; 6 | 7 | public class PoolBaseTest { 8 | 9 | @Test 10 | public void test_0() throws Exception { 11 | 12 | ConnPool pool = new ConnPool(new PoolConfig(), new ConnFactory()); 13 | 14 | pool.initPool(new PoolConfig(), new ConnFactory()); 15 | 16 | pool.addObjects(1); 17 | 18 | Conn conn = pool.getResource(); 19 | 20 | pool.returnResource(conn); 21 | 22 | pool.invalidateResource(conn); 23 | 24 | System.out.println("MaxBorrowWaitTimeMillis " + pool.getMaxBorrowWaitTimeMillis()); 25 | 26 | System.out.println("MeanBorrowWaitTimeMillis " + pool.getMeanBorrowWaitTimeMillis()); 27 | 28 | System.out.println("NumActive " + pool.getNumActive()); 29 | 30 | System.out.println("NumIdle " + pool.getNumIdle()); 31 | 32 | System.out.println("NumWaiters " + pool.getNumWaiters()); 33 | 34 | pool.isClosed(); 35 | 36 | pool.clear(); 37 | 38 | pool.destroy(); 39 | 40 | pool.close(); 41 | 42 | 43 | ConnPool2 pool2 = new ConnPool2(new PoolConfig(), new ConnFactory()); 44 | 45 | try { 46 | pool.getMaxBorrowWaitTimeMillis(); 47 | } catch (Exception e) { 48 | 49 | } 50 | } 51 | 52 | @Test 53 | public void test_1() throws Exception { 54 | 55 | ConnPool pool = new ConnPool(); 56 | 57 | try { 58 | pool.addObjects(1); 59 | } catch (Exception e) { 60 | } 61 | 62 | try { 63 | pool.getResource(); 64 | } catch (Exception e) { 65 | } 66 | 67 | try { 68 | pool.returnResource(new Conn()); 69 | } catch (Exception e) { 70 | } 71 | 72 | try { 73 | pool.invalidateResource(new Conn()); 74 | } catch (Exception e) { 75 | } 76 | 77 | try { 78 | pool.returnResource(null); 79 | } catch (Exception e) { 80 | } 81 | 82 | try { 83 | pool.invalidateResource(null); 84 | } catch (Exception e) { 85 | } 86 | 87 | try { 88 | System.out.println("MaxBorrowWaitTimeMillis " + pool.getMaxBorrowWaitTimeMillis()); 89 | } catch (Exception e) { 90 | } 91 | 92 | try { 93 | System.out.println("MeanBorrowWaitTimeMillis " + pool.getMeanBorrowWaitTimeMillis()); 94 | } catch (Exception e) { 95 | } 96 | 97 | try { 98 | System.out.println("NumActive " + pool.getNumActive()); 99 | } catch (Exception e) { 100 | } 101 | 102 | try { 103 | System.out.println("NumIdle " + pool.getNumIdle()); 104 | } catch (Exception e) { 105 | } 106 | 107 | try { 108 | System.out.println("NumWaiters " + pool.getNumWaiters()); 109 | } catch (Exception e) { 110 | } 111 | 112 | try { 113 | pool.isClosed(); 114 | } catch (Exception e) { 115 | } 116 | 117 | try { 118 | pool.clear(); 119 | } catch (Exception e) { 120 | } 121 | 122 | try { 123 | pool.destroy(); 124 | } catch (Exception e) { 125 | } 126 | 127 | try { 128 | pool.close(); 129 | } catch (Exception e) { 130 | } 131 | 132 | } 133 | 134 | private static class ConnFactory implements ConnectionFactory { 135 | 136 | private static final long serialVersionUID = 1L; 137 | 138 | @Override 139 | public PooledObject makeObject() throws Exception { 140 | 141 | return new DefaultPooledObject(createConnection()); 142 | } 143 | 144 | @Override 145 | public void destroyObject(PooledObject p) throws Exception { 146 | 147 | System.out.println("destroyObject " + p.getObject().getId()); 148 | } 149 | 150 | @Override 151 | public boolean validateObject(PooledObject p) { 152 | 153 | System.out.println("validateObject " + p.getObject().getId()); 154 | 155 | return true; 156 | } 157 | 158 | @Override 159 | public void activateObject(PooledObject p) throws Exception { 160 | 161 | System.out.println("activateObject " + p.getObject().getId()); 162 | } 163 | 164 | @Override 165 | public void passivateObject(PooledObject p) throws Exception { 166 | 167 | System.out.println("passivateObject " + p.getObject().getId()); 168 | } 169 | 170 | @Override 171 | public Conn createConnection() throws Exception { 172 | 173 | Conn conn = new Conn(); 174 | 175 | conn.setId(123); 176 | 177 | return conn; 178 | } 179 | } 180 | 181 | private static class ConnPool extends PoolBase { 182 | 183 | private static final long serialVersionUID = 1L; 184 | 185 | public ConnPool() { 186 | } 187 | 188 | public ConnPool(final PoolConfig poolConfig, ConnFactory factory) { 189 | 190 | super(poolConfig, factory); 191 | } 192 | } 193 | 194 | private static class ConnPool2 extends PoolBase { 195 | 196 | private static final long serialVersionUID = 1L; 197 | 198 | public ConnPool2(final PoolConfig poolConfig, ConnFactory factory) { 199 | 200 | super(poolConfig, factory); 201 | } 202 | 203 | @Override 204 | public boolean isClosed() { 205 | throw new RuntimeException(); 206 | } 207 | } 208 | 209 | private static class Conn { 210 | 211 | private int id; 212 | 213 | public int getId() { 214 | return id; 215 | } 216 | 217 | public void setId(int id) { 218 | this.id = id; 219 | } 220 | } 221 | } 222 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/PoolConfigTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool; 2 | 3 | 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | public class PoolConfigTest { 8 | 9 | @Test 10 | public void test() throws Exception { 11 | 12 | PoolConfig config = new PoolConfig(); 13 | 14 | Assert.assertNotNull(config); 15 | 16 | Assert.assertEquals(PoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS, 60000); 17 | 18 | Assert.assertEquals(PoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS, 30000); 19 | 20 | Assert.assertEquals(PoolConfig.DEFAULT_TEST_WHILE_IDLE, true); 21 | 22 | Assert.assertEquals(PoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN, -1); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/hbase/HbaseConfigTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.hbase; 17 | 18 | import org.junit.Assert; 19 | import org.junit.Test; 20 | 21 | public class HbaseConfigTest { 22 | 23 | @Test 24 | public void test() throws Exception { 25 | 26 | Assert.assertEquals(HbaseConfig.DEFAULT_HOST, "localhost"); 27 | Assert.assertEquals(HbaseConfig.DEFAULT_PORT, "2181"); 28 | Assert.assertNull(HbaseConfig.DEFAULT_MASTER); 29 | Assert.assertNull(HbaseConfig.DEFAULT_ROOTDIR); 30 | 31 | Assert.assertEquals(HbaseConfig.ZOOKEEPER_QUORUM_PROPERTY, "hbase.zookeeper.quorum"); 32 | Assert.assertEquals(HbaseConfig.ZOOKEEPER_CLIENTPORT_PROPERTY, "hbase.zookeeper.property.clientPort"); 33 | Assert.assertEquals(HbaseConfig.MASTER_PROPERTY, "hbase.master"); 34 | Assert.assertEquals(HbaseConfig.ROOTDIR_PROPERTY, "hbase.rootdir"); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/hbase/HbaseConnectionPoolTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.hbase; 17 | 18 | import org.apache.hadoop.conf.Configuration; 19 | import org.darkphoenixs.pool.PoolConfig; 20 | import org.junit.Test; 21 | 22 | import java.util.Properties; 23 | 24 | public class HbaseConnectionPoolTest { 25 | 26 | @Test 27 | public void test() throws Exception { 28 | 29 | try { 30 | HbaseConnectionPool pool = new HbaseConnectionPool( 31 | new PoolConfig(), HbaseConfig.DEFAULT_HOST, 32 | HbaseConfig.DEFAULT_PORT); 33 | 34 | pool.close(); 35 | 36 | } catch (Exception e) { 37 | } 38 | 39 | try { 40 | HbaseConnectionPool pool = new HbaseConnectionPool( 41 | HbaseConfig.DEFAULT_HOST, HbaseConfig.DEFAULT_PORT, 42 | HbaseConfig.DEFAULT_MASTER, HbaseConfig.DEFAULT_ROOTDIR); 43 | 44 | pool.close(); 45 | } catch (Exception e) { 46 | } 47 | 48 | try { 49 | Configuration configuration = new Configuration(); 50 | configuration.set(HbaseConfig.ZOOKEEPER_QUORUM_PROPERTY, 51 | HbaseConfig.DEFAULT_HOST); 52 | configuration.set(HbaseConfig.ZOOKEEPER_CLIENTPORT_PROPERTY, 53 | HbaseConfig.DEFAULT_PORT); 54 | HbaseConnectionPool pool = new HbaseConnectionPool(configuration); 55 | pool.close(); 56 | 57 | } catch (Exception e) { 58 | } 59 | 60 | try { 61 | PoolConfig poolconfig = new PoolConfig(); 62 | Properties prop = new Properties(); 63 | prop.setProperty(HbaseConfig.ZOOKEEPER_QUORUM_PROPERTY, 64 | HbaseConfig.DEFAULT_HOST); 65 | prop.setProperty(HbaseConfig.ZOOKEEPER_CLIENTPORT_PROPERTY, 66 | HbaseConfig.DEFAULT_PORT); 67 | HbaseConnectionPool pool = new HbaseConnectionPool(poolconfig, prop); 68 | 69 | pool.close(); 70 | } catch (Exception e) { 71 | } 72 | 73 | HbaseConnectionPool pool = new HbaseConnectionPool(); 74 | 75 | try { 76 | pool.getConnection(); 77 | } catch (Exception e) { 78 | } 79 | 80 | try { 81 | pool.returnConnection(null); 82 | } catch (Exception e) { 83 | } 84 | 85 | try { 86 | pool.invalidateConnection(null); 87 | } catch (Exception e) { 88 | } 89 | 90 | pool.close(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/hbase/HbaseSharedConnPoolTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.hbase; 2 | 3 | import org.apache.hadoop.conf.Configuration; 4 | import org.apache.hadoop.hbase.TableName; 5 | import org.apache.hadoop.hbase.client.*; 6 | import org.junit.Test; 7 | 8 | import java.io.IOException; 9 | import java.util.concurrent.ExecutorService; 10 | 11 | public class HbaseSharedConnPoolTest { 12 | 13 | @Test 14 | public void test() throws Exception { 15 | 16 | HbaseSharedConnPool pool = HbaseSharedConnPool.getInstance(HbaseConfig.DEFAULT_HOST, HbaseConfig.DEFAULT_PORT, HbaseConfig.DEFAULT_MASTER, HbaseConfig.DEFAULT_ROOTDIR); 17 | 18 | try { 19 | pool = HbaseSharedConnPool.getInstance(HbaseConfig.DEFAULT_HOST, HbaseConfig.DEFAULT_PORT, "localhost:60000", "hdfs://localhost:8020/hbase"); 20 | 21 | } catch (Exception e) { 22 | 23 | } 24 | 25 | try { 26 | pool = HbaseSharedConnPool.getInstance(null, HbaseConfig.DEFAULT_PORT, "localhost:60000", "hdfs://localhost:8020/hbase"); 27 | 28 | } catch (Exception e) { 29 | 30 | } 31 | 32 | try { 33 | pool = HbaseSharedConnPool.getInstance(HbaseConfig.DEFAULT_HOST, null, "localhost:60000", "hdfs://localhost:8020/hbase"); 34 | 35 | } catch (Exception e) { 36 | 37 | } 38 | 39 | Connection conn = pool.getConnection(); 40 | 41 | conn = new Connection() { 42 | @Override 43 | public Configuration getConfiguration() { 44 | return null; 45 | } 46 | 47 | @Override 48 | public Table getTable(TableName tableName) throws IOException { 49 | return null; 50 | } 51 | 52 | @Override 53 | public Table getTable(TableName tableName, ExecutorService pool) throws IOException { 54 | return null; 55 | } 56 | 57 | @Override 58 | public BufferedMutator getBufferedMutator(TableName tableName) throws IOException { 59 | return null; 60 | } 61 | 62 | @Override 63 | public BufferedMutator getBufferedMutator(BufferedMutatorParams params) throws IOException { 64 | return null; 65 | } 66 | 67 | @Override 68 | public RegionLocator getRegionLocator(TableName tableName) throws IOException { 69 | return null; 70 | } 71 | 72 | @Override 73 | public Admin getAdmin() throws IOException { 74 | return null; 75 | } 76 | 77 | @Override 78 | public void close() throws IOException { 79 | 80 | throw new IOException(); 81 | } 82 | 83 | @Override 84 | public boolean isClosed() { 85 | return false; 86 | } 87 | 88 | @Override 89 | public void abort(String why, Throwable e) { 90 | 91 | } 92 | 93 | @Override 94 | public boolean isAborted() { 95 | return false; 96 | } 97 | }; 98 | 99 | pool.returnConnection(conn); 100 | 101 | pool.returnConnection(null); 102 | 103 | pool.invalidateConnection(conn); 104 | 105 | pool.invalidateConnection(new Connection() { 106 | @Override 107 | public Configuration getConfiguration() { 108 | return null; 109 | } 110 | 111 | @Override 112 | public Table getTable(TableName tableName) throws IOException { 113 | return null; 114 | } 115 | 116 | @Override 117 | public Table getTable(TableName tableName, ExecutorService pool) throws IOException { 118 | return null; 119 | } 120 | 121 | @Override 122 | public BufferedMutator getBufferedMutator(TableName tableName) throws IOException { 123 | return null; 124 | } 125 | 126 | @Override 127 | public BufferedMutator getBufferedMutator(BufferedMutatorParams params) throws IOException { 128 | return null; 129 | } 130 | 131 | @Override 132 | public RegionLocator getRegionLocator(TableName tableName) throws IOException { 133 | return null; 134 | } 135 | 136 | @Override 137 | public Admin getAdmin() throws IOException { 138 | return null; 139 | } 140 | 141 | @Override 142 | public void close() throws IOException { 143 | } 144 | 145 | @Override 146 | public boolean isClosed() { 147 | return false; 148 | } 149 | 150 | @Override 151 | public void abort(String why, Throwable e) { 152 | 153 | } 154 | 155 | @Override 156 | public boolean isAborted() { 157 | return false; 158 | } 159 | }); 160 | 161 | pool.invalidateConnection(null); 162 | 163 | pool.close(); 164 | 165 | } 166 | 167 | } -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/hbase/HbaseTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.hbase; 2 | 3 | import org.junit.Test; 4 | 5 | public class HbaseTest { 6 | 7 | @Test 8 | public void test() throws Exception { 9 | 10 | // PoolConfig config = new PoolConfig(); 11 | // config.setMaxTotal(20); 12 | // config.setMaxIdle(5); 13 | // config.setMaxWaitMillis(1000); 14 | // config.setTestOnBorrow(true); 15 | // 16 | // Configuration hbaseConfig = new Configuration(); 17 | // hbaseConfig.set("hbase.zookeeper.quorum", "localhost"); 18 | // hbaseConfig.set("hbase.zookeeper.property.clientPort", "2181"); 19 | // hbaseConfig.set("hbase.master", "localhost:60000"); 20 | // hbaseConfig.set("hbase.rootdir", "hdfs://localhost:9000/hbase"); 21 | // 22 | // HbaseConnectionPool pool = new HbaseConnectionPool(config, hbaseConfig); 23 | // 24 | // for (int i = 0; i < 1000; i++) { 25 | // 26 | // Connection conn = pool.getConnection(); 27 | // 28 | // Table table = conn.getTable(TableName.valueOf("TableTest")); 29 | // 30 | // Put put = new Put(Bytes.toBytes("rowkey" + i)); 31 | // 32 | // put.addColumn(Bytes.toBytes("family"), Bytes.toBytes("qualifier"), 33 | // Bytes.toBytes("value" + i)); 34 | // 35 | // table.put(put); 36 | // 37 | // table.close(); 38 | // 39 | // pool.returnConnection(conn); 40 | // } 41 | // 42 | // pool.close(); 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/http/HttpConfigTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.http; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.util.Collections; 7 | 8 | public class HttpConfigTest { 9 | 10 | @Test 11 | public void test() throws Exception { 12 | 13 | Assert.assertEquals(HttpConfig.DEFAULT_URL, "https://www.baidu.com/"); 14 | Assert.assertEquals(HttpConfig.DEFAULT_METHOD, "GET"); 15 | Assert.assertEquals(HttpConfig.DEFAULT_IMEOUT, 30000); 16 | Assert.assertEquals(HttpConfig.DEFAULT_HEADER, Collections.singletonMap("Content-type", "application/json")); 17 | 18 | Assert.assertEquals(HttpConfig.PROXY_HOST_PROPERTY, "proxyHost"); 19 | Assert.assertEquals(HttpConfig.PROXY_PORT_PROPERTY, "proxyPort"); 20 | Assert.assertEquals(HttpConfig.HTTP_URL_PROPERTY, "httpUrl"); 21 | Assert.assertEquals(HttpConfig.HTTP_METHOD_PROPERTY, "httpMethod"); 22 | Assert.assertEquals(HttpConfig.CONNECT_TIMEOUT_PROPERTY, "connectTimeout"); 23 | Assert.assertEquals(HttpConfig.READ_TIMEOUT_PROPERTY, "readTimeout"); 24 | Assert.assertEquals(HttpConfig.HTTP_HEADER_PROPERTY, "httpHeader"); 25 | } 26 | } -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/http/HttpConnectionFactoryTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.http; 2 | 3 | import org.apache.commons.pool2.impl.DefaultPooledObject; 4 | import org.junit.Test; 5 | 6 | import java.io.IOException; 7 | import java.net.HttpURLConnection; 8 | import java.net.Proxy; 9 | import java.util.Properties; 10 | 11 | public class HttpConnectionFactoryTest { 12 | 13 | @Test 14 | public void test1() throws Exception { 15 | 16 | HttpConnectionFactory factory = new HttpConnectionFactory(Proxy.NO_PROXY, HttpConfig.DEFAULT_URL, HttpConfig.DEFAULT_METHOD, 5000, 5000, HttpConfig.DEFAULT_HEADER); 17 | 18 | factory.makeObject(); 19 | factory.activateObject(null); 20 | factory.passivateObject(null); 21 | 22 | try { 23 | factory.validateObject(null); 24 | } catch (Exception e) { 25 | } 26 | 27 | try { 28 | factory.validateObject(new DefaultPooledObject(null)); 29 | } catch (Exception e) { 30 | } 31 | 32 | try { 33 | factory.validateObject(new DefaultPooledObject(new HttpURLConnection(null) { 34 | @Override 35 | public void connect() throws IOException { 36 | 37 | } 38 | 39 | @Override 40 | public void disconnect() { 41 | 42 | } 43 | 44 | @Override 45 | public boolean usingProxy() { 46 | return false; 47 | } 48 | 49 | @Override 50 | public int getResponseCode() throws IOException { 51 | return 200; 52 | } 53 | })); 54 | } catch (Exception e) { 55 | } 56 | 57 | try { 58 | factory.validateObject(new DefaultPooledObject(new HttpURLConnection(null) { 59 | @Override 60 | public void connect() throws IOException { 61 | 62 | } 63 | 64 | @Override 65 | public void disconnect() { 66 | 67 | } 68 | 69 | @Override 70 | public boolean usingProxy() { 71 | return false; 72 | } 73 | 74 | @Override 75 | public int getResponseCode() throws IOException { 76 | return 201; 77 | } 78 | })); 79 | } catch (Exception e) { 80 | } 81 | 82 | try { 83 | factory.validateObject(new DefaultPooledObject(new HttpURLConnection(null) { 84 | @Override 85 | public void connect() throws IOException { 86 | 87 | } 88 | 89 | @Override 90 | public void disconnect() { 91 | 92 | } 93 | 94 | @Override 95 | public boolean usingProxy() { 96 | return false; 97 | } 98 | 99 | @Override 100 | public int getResponseCode() throws IOException { 101 | throw new IOException("test"); 102 | } 103 | })); 104 | } catch (Exception e) { 105 | } 106 | 107 | try { 108 | factory.destroyObject(null); 109 | } catch (Exception e) { 110 | } 111 | 112 | try { 113 | factory.destroyObject(new DefaultPooledObject(null)); 114 | } catch (Exception e) { 115 | } 116 | 117 | try { 118 | factory.destroyObject(new DefaultPooledObject(new HttpURLConnection(null) { 119 | @Override 120 | public void disconnect() { 121 | 122 | } 123 | 124 | @Override 125 | public boolean usingProxy() { 126 | return false; 127 | } 128 | 129 | @Override 130 | public void connect() throws IOException { 131 | 132 | } 133 | })); 134 | } catch (Exception e) { 135 | } 136 | 137 | 138 | HttpConnectionFactory factory2 = new HttpConnectionFactory(null, HttpConfig.DEFAULT_URL, "POST", 5000, 5000, HttpConfig.DEFAULT_HEADER); 139 | 140 | factory2.makeObject(); 141 | 142 | HttpConnectionFactory factory3 = new HttpConnectionFactory(null, "https://www.baidu1.com/", "POST", 5000, 5000, HttpConfig.DEFAULT_HEADER); 143 | 144 | try { 145 | factory3.makeObject(); 146 | } catch (Exception e) { 147 | } 148 | 149 | HttpConnectionFactory factory4 = new HttpConnectionFactory(null, "123", "POST", 5000, 5000, HttpConfig.DEFAULT_HEADER); 150 | 151 | try { 152 | factory4.makeObject(); 153 | } catch (Exception e) { 154 | } 155 | } 156 | 157 | @Test 158 | public void test2() throws Exception { 159 | 160 | Properties properties = new Properties(); 161 | 162 | try { 163 | new HttpConnectionFactory(properties); 164 | } catch (Exception e) { 165 | } 166 | properties.setProperty(HttpConfig.PROXY_HOST_PROPERTY, "127.0.0.1"); 167 | try { 168 | new HttpConnectionFactory(properties); 169 | } catch (Exception e) { 170 | } 171 | properties.remove(HttpConfig.PROXY_HOST_PROPERTY); 172 | properties.setProperty(HttpConfig.PROXY_PORT_PROPERTY, "1234"); 173 | try { 174 | new HttpConnectionFactory(properties); 175 | } catch (Exception e) { 176 | } 177 | properties.setProperty(HttpConfig.PROXY_HOST_PROPERTY, "127.0.0.1"); 178 | try { 179 | new HttpConnectionFactory(properties); 180 | } catch (Exception e) { 181 | } 182 | properties.setProperty(HttpConfig.HTTP_URL_PROPERTY, HttpConfig.DEFAULT_URL); 183 | try { 184 | new HttpConnectionFactory(properties); 185 | } catch (Exception e) { 186 | } 187 | properties.setProperty(HttpConfig.HTTP_METHOD_PROPERTY, HttpConfig.DEFAULT_METHOD); 188 | try { 189 | new HttpConnectionFactory(properties); 190 | } catch (Exception e) { 191 | } 192 | properties.setProperty(HttpConfig.CONNECT_TIMEOUT_PROPERTY, "1000"); 193 | try { 194 | new HttpConnectionFactory(properties); 195 | } catch (Exception e) { 196 | } 197 | properties.setProperty(HttpConfig.READ_TIMEOUT_PROPERTY, "1000"); 198 | try { 199 | new HttpConnectionFactory(properties); 200 | } catch (Exception e) { 201 | } 202 | properties.setProperty(HttpConfig.HTTP_HEADER_PROPERTY, "Content-type:application/json"); 203 | try { 204 | new HttpConnectionFactory(properties); 205 | } catch (Exception e) { 206 | } 207 | } 208 | } -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/http/HttpConnectionPoolTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.http; 2 | 3 | import org.darkphoenixs.pool.PoolConfig; 4 | import org.junit.Test; 5 | 6 | import java.net.Proxy; 7 | import java.util.Properties; 8 | 9 | public class HttpConnectionPoolTest { 10 | 11 | @Test 12 | public void test() throws Exception { 13 | 14 | PoolConfig config = new PoolConfig(); 15 | config.setMaxTotal(1); 16 | config.setMaxIdle(1); 17 | config.setMaxWaitMillis(1000); 18 | config.setTestOnBorrow(true); 19 | 20 | Properties properties = new Properties(); 21 | properties.setProperty(HttpConfig.HTTP_URL_PROPERTY, HttpConfig.DEFAULT_URL); 22 | properties.setProperty(HttpConfig.HTTP_METHOD_PROPERTY, HttpConfig.DEFAULT_METHOD); 23 | 24 | HttpConnectionPool pool = new HttpConnectionPool(); 25 | 26 | try { 27 | pool.getConnection(); 28 | } catch (Exception e) { 29 | } 30 | 31 | try { 32 | pool.returnConnection(null); 33 | } catch (Exception e) { 34 | } 35 | 36 | try { 37 | pool.invalidateConnection(null); 38 | } catch (Exception e) { 39 | } 40 | 41 | try { 42 | new HttpConnectionPool(HttpConfig.DEFAULT_URL); 43 | } catch (Exception e) { 44 | } 45 | try { 46 | new HttpConnectionPool(config, HttpConfig.DEFAULT_URL); 47 | } catch (Exception e) { 48 | } 49 | 50 | try { 51 | new HttpConnectionPool(Proxy.NO_PROXY, HttpConfig.DEFAULT_URL); 52 | } catch (Exception e) { 53 | } 54 | 55 | try { 56 | new HttpConnectionPool(config, Proxy.NO_PROXY, HttpConfig.DEFAULT_URL); 57 | } catch (Exception e) { 58 | } 59 | 60 | try { 61 | new HttpConnectionPool(config, properties); 62 | } catch (Exception e) { 63 | } 64 | 65 | 66 | } 67 | } -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/http/HttpTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.http; 2 | 3 | import org.darkphoenixs.pool.PoolConfig; 4 | import org.junit.Test; 5 | 6 | import java.io.BufferedReader; 7 | import java.io.InputStreamReader; 8 | import java.net.HttpURLConnection; 9 | 10 | public class HttpTest { 11 | 12 | @Test 13 | public void test() throws Exception { 14 | 15 | PoolConfig config = new PoolConfig(); 16 | config.setMaxTotal(20); 17 | config.setMaxIdle(5); 18 | config.setMaxWaitMillis(1000); 19 | config.setTestOnBorrow(true); 20 | 21 | HttpConnectionPool pool = new HttpConnectionPool(config, "https://www.baidu.com/"); 22 | 23 | HttpURLConnection connection = pool.getConnection(); 24 | 25 | BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 26 | 27 | String line; 28 | 29 | while ((line = reader.readLine()) != null) { 30 | 31 | System.out.println(line); 32 | } 33 | 34 | reader.close(); 35 | 36 | pool.returnConnection(connection); 37 | 38 | pool.close(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/jdbc/JdbcConfigTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.jdbc; 17 | 18 | import org.junit.Assert; 19 | import org.junit.Test; 20 | 21 | public class JdbcConfigTest { 22 | 23 | @Test 24 | public void test() throws Exception { 25 | 26 | Assert.assertEquals(JdbcConfig.DEFAULT_DRIVER_CLASS, "com.mysql.jdbc.Driver"); 27 | Assert.assertEquals(JdbcConfig.DEFAULT_JDBC_URL, "jdbc:mysql://localhost:3306/test"); 28 | Assert.assertEquals(JdbcConfig.DEFAULT_JDBC_USERNAME, "root"); 29 | Assert.assertEquals(JdbcConfig.DEFAULT_JDBC_PASSWORD, "root"); 30 | 31 | Assert.assertEquals(JdbcConfig.DRIVER_CLASS_PROPERTY, "driverClass"); 32 | Assert.assertEquals(JdbcConfig.JDBC_URL_PROPERTY, "jdbcUrl"); 33 | Assert.assertEquals(JdbcConfig.JDBC_USERNAME_PROPERTY, "username"); 34 | Assert.assertEquals(JdbcConfig.JDBC_PASSWORD_PROPERTY, "password"); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/jdbc/JdbcConnectionPoolTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.jdbc; 17 | 18 | import org.apache.commons.pool2.BasePooledObjectFactory; 19 | import org.apache.commons.pool2.PooledObject; 20 | import org.darkphoenixs.pool.PoolConfig; 21 | import org.junit.Test; 22 | 23 | import java.sql.Connection; 24 | import java.util.Properties; 25 | 26 | public class JdbcConnectionPoolTest { 27 | 28 | @Test 29 | public void test() throws Exception { 30 | 31 | try { 32 | JdbcConnectionPool pool = new JdbcConnectionPool( 33 | JdbcConfig.DEFAULT_DRIVER_CLASS, 34 | JdbcConfig.DEFAULT_JDBC_URL, 35 | JdbcConfig.DEFAULT_JDBC_USERNAME, 36 | JdbcConfig.DEFAULT_JDBC_PASSWORD); 37 | 38 | pool.close(); 39 | } catch (Exception e) { 40 | } 41 | 42 | try { 43 | JdbcConnectionPool pool = new JdbcConnectionPool(new PoolConfig(), 44 | JdbcConfig.DEFAULT_DRIVER_CLASS, 45 | JdbcConfig.DEFAULT_JDBC_URL, 46 | JdbcConfig.DEFAULT_JDBC_USERNAME, 47 | JdbcConfig.DEFAULT_JDBC_PASSWORD); 48 | 49 | pool.close(); 50 | } catch (Exception e) { 51 | } 52 | Properties prop = new Properties(); 53 | prop.setProperty(JdbcConfig.DRIVER_CLASS_PROPERTY, 54 | JdbcConfig.DEFAULT_DRIVER_CLASS); 55 | prop.setProperty(JdbcConfig.JDBC_URL_PROPERTY, 56 | JdbcConfig.DEFAULT_JDBC_URL); 57 | prop.setProperty(JdbcConfig.JDBC_USERNAME_PROPERTY, 58 | JdbcConfig.DEFAULT_JDBC_USERNAME); 59 | prop.setProperty(JdbcConfig.JDBC_PASSWORD_PROPERTY, 60 | JdbcConfig.DEFAULT_JDBC_PASSWORD); 61 | try { 62 | JdbcConnectionPool pool = new JdbcConnectionPool(prop); 63 | pool.close(); 64 | } catch (Exception e) { 65 | } 66 | 67 | try { 68 | JdbcConnectionPool pool = new JdbcConnectionPool(new PoolConfig(), 69 | prop); 70 | pool.close(); 71 | } catch (Exception e) { 72 | } 73 | 74 | JdbcConnectionPool pool = new JdbcConnectionPool(); 75 | 76 | try { 77 | pool.getConnection(); 78 | } catch (Exception e) { 79 | } 80 | 81 | try { 82 | pool.returnConnection(null); 83 | } catch (Exception e) { 84 | } 85 | 86 | try { 87 | pool.invalidateConnection(null); 88 | } catch (Exception e) { 89 | } 90 | 91 | pool.close(); 92 | 93 | JdbcConnectionPoolDemo demo = new JdbcConnectionPoolDemo(); 94 | 95 | try { 96 | demo.getConnection(); 97 | 98 | } catch (Exception e) { 99 | } 100 | 101 | demo.close(); 102 | } 103 | 104 | class JdbcConnectionPoolDemo extends JdbcConnectionPool { 105 | 106 | public JdbcConnectionPoolDemo() { 107 | 108 | initPool(new PoolConfig(), new BasePooledObjectFactory() { 109 | 110 | @Override 111 | public Connection create() throws Exception { 112 | 113 | return null; 114 | } 115 | 116 | @Override 117 | public PooledObject wrap(Connection obj) { 118 | 119 | return null; 120 | } 121 | 122 | }); 123 | 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/jdbc/JdbcTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.jdbc; 2 | 3 | import org.junit.Test; 4 | 5 | public class JdbcTest { 6 | 7 | @Test 8 | public void test() throws Exception { 9 | 10 | // PoolConfig config = new PoolConfig(); 11 | // config.setMaxTotal(20); 12 | // config.setMaxIdle(5); 13 | // config.setMaxWaitMillis(1000); 14 | // config.setTestOnBorrow(true); 15 | // 16 | // Properties props = new Properties(); 17 | // props.setProperty("driverClass", "oracle.jdbc.OracleDriver"); 18 | // props.setProperty("jdbcUrl", "jdbc:oracle:thin:@localhost:1521:orcl"); 19 | // props.setProperty("username", "root"); 20 | // props.setProperty("password", "root"); 21 | // 22 | // JdbcConnectionPool pool = new JdbcConnectionPool(config, props); 23 | // 24 | // Connection conn = pool.getConnection(); 25 | // 26 | // Statement stat = conn.createStatement(); 27 | // 28 | // ResultSet rs = stat.executeQuery("select ID from T_TEST"); 29 | // 30 | // while(rs.next()) { 31 | // 32 | // rs.getInt("ID"); 33 | // } 34 | // 35 | // pool.returnConnection(conn); 36 | // 37 | // pool.close(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/kafka/KafkaConfigTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.kafka; 17 | 18 | import org.junit.Assert; 19 | import org.junit.Test; 20 | 21 | public class KafkaConfigTest { 22 | 23 | @Test 24 | public void test() throws Exception { 25 | 26 | Assert.assertEquals(KafkaConfig.DEFAULT_BROKERS, "localhost:9092"); 27 | Assert.assertEquals(KafkaConfig.DEFAULT_TYPE, "sync"); 28 | Assert.assertEquals(KafkaConfig.DEFAULT_ACKS, "0"); 29 | Assert.assertEquals(KafkaConfig.DEFAULT_CODEC, "none"); 30 | Assert.assertEquals(KafkaConfig.DEFAULT_BATCH, "200"); 31 | 32 | Assert.assertEquals(KafkaConfig.BROKERS_LIST_PROPERTY, "metadata.broker.list"); 33 | Assert.assertEquals(KafkaConfig.PRODUCER_TYPE_PROPERTY, "producer.type"); 34 | Assert.assertEquals(KafkaConfig.REQUEST_ACKS_PROPERTY, "request.required.acks"); 35 | Assert.assertEquals(KafkaConfig.COMPRESSION_CODEC_PROPERTY, "compression.codec"); 36 | Assert.assertEquals(KafkaConfig.BATCH_NUMBER_PROPERTY, "batch.num.messages"); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/kafka/KafkaConnectionFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.kafka; 17 | 18 | import kafka.javaapi.producer.Producer; 19 | import kafka.producer.ProducerConfig; 20 | import org.apache.commons.pool2.impl.DefaultPooledObject; 21 | import org.junit.Test; 22 | 23 | import java.util.Properties; 24 | 25 | public class KafkaConnectionFactoryTest { 26 | 27 | @Test 28 | public void test() throws Exception { 29 | 30 | try { 31 | Properties prop = new Properties(); 32 | KafkaConnectionFactory factory = new KafkaConnectionFactory(prop); 33 | factory.toString(); 34 | } catch (Exception e) { 35 | } 36 | 37 | Properties prop = new Properties(); 38 | 39 | prop.setProperty(KafkaConfig.BROKERS_LIST_PROPERTY, 40 | KafkaConfig.DEFAULT_BROKERS); 41 | prop.setProperty(KafkaConfig.PRODUCER_TYPE_PROPERTY, 42 | KafkaConfig.DEFAULT_TYPE); 43 | prop.setProperty(KafkaConfig.REQUEST_ACKS_PROPERTY, 44 | KafkaConfig.DEFAULT_ACKS); 45 | prop.setProperty(KafkaConfig.COMPRESSION_CODEC_PROPERTY, 46 | KafkaConfig.DEFAULT_CODEC); 47 | prop.setProperty(KafkaConfig.BATCH_NUMBER_PROPERTY, 48 | KafkaConfig.DEFAULT_BATCH); 49 | 50 | ProducerConfig config = new ProducerConfig(prop); 51 | 52 | try { 53 | KafkaConnectionFactory factory = new KafkaConnectionFactory(prop); 54 | factory.toString(); 55 | } catch (Exception e) { 56 | } 57 | 58 | try { 59 | KafkaConnectionFactory factory = new KafkaConnectionFactory(config); 60 | factory.toString(); 61 | } catch (Exception e) { 62 | } 63 | 64 | KafkaConnectionFactory factory = new KafkaConnectionFactory( 65 | KafkaConfig.DEFAULT_BROKERS, KafkaConfig.DEFAULT_TYPE, 66 | KafkaConfig.DEFAULT_ACKS, KafkaConfig.DEFAULT_CODEC, 67 | KafkaConfig.DEFAULT_BATCH); 68 | 69 | try { 70 | factory.makeObject(); 71 | 72 | } catch (Exception e) { 73 | } 74 | 75 | factory.activateObject(new DefaultPooledObject>( 76 | new Producer(config))); 77 | try { 78 | factory.activateObject(new DefaultPooledObject>( 79 | null)); 80 | } catch (Exception e) { 81 | } 82 | 83 | factory.validateObject(new DefaultPooledObject>( 84 | new Producer(config))); 85 | try { 86 | factory.validateObject(new DefaultPooledObject>( 87 | null)); 88 | } catch (Exception e) { 89 | } 90 | 91 | factory.passivateObject(new DefaultPooledObject>( 92 | new Producer(config))); 93 | try { 94 | factory.passivateObject(new DefaultPooledObject>( 95 | null)); 96 | } catch (Exception e) { 97 | } 98 | 99 | factory.destroyObject(new DefaultPooledObject>( 100 | new Producer(config))); 101 | try { 102 | factory.destroyObject(new DefaultPooledObject>( 103 | null)); 104 | } catch (Exception e) { 105 | } 106 | 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/kafka/KafkaConnectionPoolTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.kafka; 17 | 18 | import kafka.producer.ProducerConfig; 19 | import org.darkphoenixs.pool.PoolConfig; 20 | import org.junit.Test; 21 | 22 | import java.util.Properties; 23 | 24 | public class KafkaConnectionPoolTest { 25 | 26 | @Test 27 | public void test() throws Exception { 28 | 29 | Properties prop = new Properties(); 30 | 31 | prop.setProperty(KafkaConfig.BROKERS_LIST_PROPERTY, 32 | KafkaConfig.DEFAULT_BROKERS); 33 | prop.setProperty(KafkaConfig.PRODUCER_TYPE_PROPERTY, 34 | KafkaConfig.DEFAULT_TYPE); 35 | prop.setProperty(KafkaConfig.REQUEST_ACKS_PROPERTY, 36 | KafkaConfig.DEFAULT_ACKS); 37 | prop.setProperty(KafkaConfig.COMPRESSION_CODEC_PROPERTY, 38 | KafkaConfig.DEFAULT_CODEC); 39 | prop.setProperty(KafkaConfig.BATCH_NUMBER_PROPERTY, 40 | KafkaConfig.DEFAULT_BATCH); 41 | 42 | ProducerConfig config = new ProducerConfig(prop); 43 | 44 | try { 45 | KafkaConnectionPool pool = new KafkaConnectionPool(KafkaConfig.DEFAULT_BROKERS); 46 | pool.close(); 47 | } catch (Exception e) { 48 | } 49 | 50 | try { 51 | KafkaConnectionPool pool = new KafkaConnectionPool(prop); 52 | pool.close(); 53 | } catch (Exception e) { 54 | } 55 | 56 | try { 57 | KafkaConnectionPool pool = new KafkaConnectionPool(config); 58 | pool.close(); 59 | } catch (Exception e) { 60 | } 61 | 62 | try { 63 | KafkaConnectionPool pool = new KafkaConnectionPool(new PoolConfig(), prop); 64 | pool.close(); 65 | } catch (Exception e) { 66 | } 67 | 68 | try { 69 | KafkaConnectionPool pool = new KafkaConnectionPool(new PoolConfig(), config); 70 | pool.close(); 71 | } catch (Exception e) { 72 | } 73 | 74 | try { 75 | KafkaConnectionPool pool = new KafkaConnectionPool(new PoolConfig(), KafkaConfig.DEFAULT_BROKERS, KafkaConfig.DEFAULT_TYPE); 76 | pool.close(); 77 | } catch (Exception e) { 78 | } 79 | 80 | KafkaConnectionPool pool = new KafkaConnectionPool(); 81 | 82 | try { 83 | pool.getConnection(); 84 | } catch (Exception e) { 85 | } 86 | 87 | try { 88 | pool.returnConnection(null); 89 | } catch (Exception e) { 90 | } 91 | 92 | try { 93 | pool.invalidateConnection(null); 94 | } catch (Exception e) { 95 | } 96 | 97 | pool.close(); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/kafka/KafkaSharedConnPoolTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.kafka; 2 | 3 | import org.apache.kafka.clients.producer.Producer; 4 | import org.junit.Test; 5 | 6 | public class KafkaSharedConnPoolTest { 7 | 8 | @Test 9 | public void test() throws Exception { 10 | 11 | KafkaSharedConnPool pool = KafkaSharedConnPool.getInstance(KafkaConfig.DEFAULT_BROKERS, 12 | KafkaConfig.DEFAULT_CODEC, KafkaConfig.DEFAULT_KEY_SERIALIZER, KafkaConfig.DEFAULT_VAL_SERIALIZER); 13 | 14 | pool = KafkaSharedConnPool.getInstance(KafkaConfig.DEFAULT_BROKERS, 15 | KafkaConfig.DEFAULT_CODEC, KafkaConfig.DEFAULT_KEY_SERIALIZER, KafkaConfig.DEFAULT_VAL_SERIALIZER); 16 | 17 | Producer producer = pool.getConnection(); 18 | 19 | pool.returnConnection(producer); 20 | 21 | pool.returnConnection(null); 22 | 23 | pool.invalidateConnection(producer); 24 | 25 | pool.invalidateConnection(null); 26 | 27 | pool.close(); 28 | } 29 | } -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/kafka/KafkaTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.kafka; 2 | 3 | import org.junit.Test; 4 | 5 | public class KafkaTest { 6 | 7 | @Test 8 | public void test() throws Exception { 9 | 10 | // PoolConfig config = new PoolConfig(); 11 | // config.setMaxTotal(20); 12 | // config.setMaxIdle(5); 13 | // config.setMaxWaitMillis(1000); 14 | // config.setTestOnBorrow(true); 15 | // 16 | // Properties props = new Properties(); 17 | // props.setProperty("metadata.broker.list", "localhost:9092"); 18 | // props.setProperty("producer.type", "async"); 19 | // props.setProperty("request.required.acks", "0"); 20 | // props.setProperty("compression.codec", "snappy"); 21 | // props.setProperty("batch.num.messages", "200"); 22 | // 23 | // KafkaConnectionPool pool = new KafkaConnectionPool(config, props); 24 | // 25 | // for (int i = 0; i < 1000; i++) { 26 | // 27 | // Producer producer = pool.getConnection(); 28 | // 29 | // KeyedMessage message = new KeyedMessage( 30 | // "QUEUE.TEST", String.valueOf(i).getBytes(), String.valueOf( 31 | // "Test" + i).getBytes()); 32 | // 33 | // producer.send(message); 34 | // 35 | // pool.returnConnection(producer); 36 | // } 37 | // 38 | // pool.close(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/JedisConn.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | public class JedisConn extends Jedis { 6 | 7 | public JedisConn() { 8 | } 9 | 10 | public JedisConn(String arg0, int arg1) { 11 | super(arg0, arg1); 12 | } 13 | 14 | @Override 15 | public String select(int index) { 16 | 17 | return String.valueOf(index); 18 | } 19 | 20 | @Override 21 | public boolean isConnected() { 22 | 23 | return true; 24 | } 25 | 26 | @Override 27 | public String ping() { 28 | 29 | return "PONG"; 30 | } 31 | 32 | @Override 33 | public String quit() { 34 | 35 | throw new RuntimeException("quit"); 36 | } 37 | 38 | @Override 39 | public void disconnect() { 40 | 41 | throw new RuntimeException("disconnect"); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/JedisConn2.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | public class JedisConn2 extends Jedis { 6 | 7 | public JedisConn2() { 8 | } 9 | 10 | public JedisConn2(String arg0, int arg1) { 11 | super(arg0, arg1); 12 | } 13 | 14 | @Override 15 | public String select(int index) { 16 | 17 | return String.valueOf(index); 18 | } 19 | 20 | @Override 21 | public boolean isConnected() { 22 | 23 | return true; 24 | } 25 | 26 | @Override 27 | public String ping() { 28 | 29 | return "PONG"; 30 | } 31 | 32 | @Override 33 | public String quit() { 34 | 35 | return "quit"; 36 | } 37 | 38 | @Override 39 | public void disconnect() { 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/JedisConn3.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | public class JedisConn3 extends Jedis { 6 | 7 | public JedisConn3() { 8 | } 9 | 10 | public JedisConn3(String arg0, int arg1) { 11 | super(arg0, arg1); 12 | } 13 | 14 | @Override 15 | public String select(int index) { 16 | 17 | return String.valueOf(index); 18 | } 19 | 20 | @Override 21 | public boolean isConnected() { 22 | 23 | return false; 24 | } 25 | 26 | @Override 27 | public String ping() { 28 | 29 | return "PONG1"; 30 | } 31 | 32 | @Override 33 | public String quit() { 34 | 35 | return "quit"; 36 | } 37 | 38 | @Override 39 | public void disconnect() { 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/JedisConn4.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import redis.clients.jedis.Jedis; 4 | 5 | public class JedisConn4 extends Jedis { 6 | 7 | public JedisConn4() { 8 | } 9 | 10 | public JedisConn4(String arg0, int arg1) { 11 | super(arg0, arg1); 12 | } 13 | 14 | @Override 15 | public String select(int index) { 16 | 17 | return String.valueOf(index); 18 | } 19 | 20 | @Override 21 | public boolean isConnected() { 22 | 23 | return true; 24 | } 25 | 26 | @Override 27 | public String ping() { 28 | 29 | return "PONG1"; 30 | } 31 | 32 | @Override 33 | public String quit() { 34 | 35 | return "quit"; 36 | } 37 | 38 | @Override 39 | public void disconnect() { 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/RedisClusterConnPoolTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.darkphoenixs.pool.PoolConfig; 4 | import org.junit.Assert; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import redis.clients.jedis.HostAndPort; 8 | import redis.clients.jedis.JedisCluster; 9 | 10 | import java.io.IOException; 11 | import java.net.ServerSocket; 12 | import java.util.HashSet; 13 | import java.util.Properties; 14 | import java.util.Set; 15 | 16 | 17 | public class RedisClusterConnPoolTest { 18 | 19 | @Before 20 | public void before() throws Exception { 21 | 22 | Thread th = new Thread(new Runnable() { 23 | 24 | private ServerSocket serverSocket; 25 | 26 | @Override 27 | public void run() { 28 | 29 | try { 30 | serverSocket = new ServerSocket(RedisConfig.DEFAULT_PORT); 31 | 32 | serverSocket.accept(); 33 | 34 | } catch (IOException e) { 35 | e.printStackTrace(); 36 | } 37 | } 38 | }); 39 | 40 | th.setDaemon(true); 41 | th.start(); 42 | } 43 | 44 | @Test 45 | public void test() throws Exception { 46 | 47 | Set jedisClusterNodes = new HashSet(); 48 | jedisClusterNodes.add(new HostAndPort(RedisConfig.DEFAULT_HOST, RedisConfig.DEFAULT_PORT)); 49 | RedisClusterConnPool pool = new RedisClusterConnPool(jedisClusterNodes); 50 | 51 | JedisCluster cluster = pool.getConnection(); 52 | Assert.assertNotNull(cluster); 53 | 54 | pool.returnConnection(cluster); 55 | pool.invalidateConnection(cluster); 56 | 57 | Properties properties = new Properties(); 58 | properties.setProperty(RedisConfig.CLUSTER_PROPERTY, RedisConfig.DEFAULT_HOST + ":" + RedisConfig.DEFAULT_PORT); 59 | pool = new RedisClusterConnPool(properties); 60 | cluster = pool.getConnection(); 61 | Assert.assertNotNull(cluster); 62 | 63 | pool.close(); 64 | 65 | pool = new RedisClusterConnPool(new PoolConfig(), jedisClusterNodes); 66 | 67 | pool.close(); 68 | 69 | } 70 | 71 | @Test 72 | public void test1() throws Exception { 73 | 74 | Set jedisClusterNodes = new HashSet(); 75 | jedisClusterNodes.add(new HostAndPort(RedisConfig.DEFAULT_HOST, RedisConfig.DEFAULT_PORT)); 76 | RedisClusterConnPool pool = new RedisClusterConnPool(jedisClusterNodes); 77 | 78 | JedisCluster cluster = pool.getConnection(); 79 | Assert.assertNotNull(cluster); 80 | 81 | pool.returnConnection(cluster); 82 | pool.invalidateConnection(cluster); 83 | 84 | Properties properties = new Properties(); 85 | properties.setProperty(RedisConfig.CLUSTER_PROPERTY, RedisConfig.DEFAULT_HOST + ":" + RedisConfig.DEFAULT_PORT); 86 | pool = new RedisClusterConnPool(properties); 87 | cluster = pool.getConnection(); 88 | Assert.assertNotNull(cluster); 89 | 90 | pool.close(); 91 | 92 | pool = new RedisClusterConnPool(jedisClusterNodes, RedisConfig.DEFAULT_TIMEOUT, RedisConfig.DEFAULT_MAXATTE, new PoolConfig()); 93 | 94 | pool.close(); 95 | 96 | } 97 | } -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/RedisConfigTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | public class RedisConfigTest { 7 | 8 | @Test 9 | public void test() throws Exception { 10 | 11 | Assert.assertEquals(RedisConfig.DEFAULT_HOST, "localhost"); 12 | Assert.assertEquals(RedisConfig.DEFAULT_PORT, 6379); 13 | Assert.assertEquals(RedisConfig.DEFAULT_TIMEOUT, 2000); 14 | Assert.assertEquals(RedisConfig.DEFAULT_DATABASE, 0); 15 | Assert.assertNull(RedisConfig.DEFAULT_PASSWORD); 16 | Assert.assertNull(RedisConfig.DEFAULT_CLIENTNAME); 17 | 18 | Assert.assertEquals(RedisConfig.ADDRESS_PROPERTY, "address"); 19 | Assert.assertEquals(RedisConfig.CONN_TIMEOUT_PROPERTY, "connectionTimeout"); 20 | Assert.assertEquals(RedisConfig.SO_TIMEOUT_PROPERTY, "soTimeout"); 21 | Assert.assertEquals(RedisConfig.DATABASE_PROPERTY, "database"); 22 | Assert.assertEquals(RedisConfig.PASSWORD_PROPERTY, "password"); 23 | Assert.assertEquals(RedisConfig.CLIENTNAME_PROPERTY, "clientName"); 24 | Assert.assertEquals(RedisConfig.MASTERNAME_PROPERTY, "masterName"); 25 | Assert.assertEquals(RedisConfig.SENTINELS_PROPERTY, "sentinels"); 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/RedisConnectionPoolOldTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.darkphoenixs.pool.PoolConfig; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import redis.clients.jedis.Jedis; 7 | 8 | import java.io.IOException; 9 | import java.net.ServerSocket; 10 | import java.util.Properties; 11 | 12 | public class RedisConnectionPoolOldTest { 13 | 14 | @Before 15 | public void before() throws Exception { 16 | 17 | Thread th = new Thread(new Runnable() { 18 | 19 | private ServerSocket serverSocket; 20 | 21 | @Override 22 | public void run() { 23 | 24 | try { 25 | serverSocket = new ServerSocket(RedisConfig.DEFAULT_PORT); 26 | 27 | serverSocket.accept(); 28 | 29 | } catch (IOException e) { 30 | e.printStackTrace(); 31 | } 32 | } 33 | }); 34 | 35 | th.setDaemon(true); 36 | th.start(); 37 | } 38 | 39 | @Test 40 | public void test() throws Exception { 41 | 42 | try { 43 | RedisConnectionPoolOld pool = new RedisConnectionPoolOld( 44 | RedisConfig.DEFAULT_HOST, RedisConfig.DEFAULT_PORT); 45 | pool.close(); 46 | } catch (Exception e) { 47 | } 48 | 49 | try { 50 | Properties prop = new Properties(); 51 | prop.setProperty(RedisConfig.ADDRESS_PROPERTY, 52 | RedisConfig.DEFAULT_HOST + ":" + RedisConfig.DEFAULT_PORT); 53 | prop.setProperty(RedisConfig.CONN_TIMEOUT_PROPERTY, 54 | RedisConfig.DEFAULT_TIMEOUT + ""); 55 | prop.setProperty(RedisConfig.SO_TIMEOUT_PROPERTY, 56 | RedisConfig.DEFAULT_TIMEOUT + ""); 57 | prop.setProperty(RedisConfig.DATABASE_PROPERTY, 58 | RedisConfig.DEFAULT_DATABASE + ""); 59 | 60 | RedisConnectionPoolOld pool = new RedisConnectionPoolOld( 61 | new PoolConfig(), prop); 62 | pool.close(); 63 | } catch (Exception e) { 64 | } 65 | 66 | try { 67 | RedisConnectionPoolOld pool = new RedisConnectionPoolOld( 68 | new PoolConfig(), RedisConfig.DEFAULT_HOST); 69 | pool.close(); 70 | } catch (Exception e) { 71 | } 72 | 73 | RedisConnectionPoolOld pool = new RedisConnectionPoolOld(); 74 | 75 | try { 76 | pool.getConnection(); 77 | } catch (Exception e) { 78 | } 79 | 80 | try { 81 | 82 | Jedis jedis = pool.getConnection(); 83 | 84 | pool.returnConnection(jedis); 85 | 86 | jedis.disconnect(); 87 | 88 | try { 89 | jedis.auth(""); 90 | } catch (Exception e) { 91 | } 92 | 93 | pool.returnConnection(jedis); 94 | 95 | } catch (Exception e) { 96 | } 97 | 98 | try { 99 | pool.invalidateConnection(null); 100 | } catch (Exception e) { 101 | } 102 | 103 | pool.close(); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/RedisConnectionPoolTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.darkphoenixs.pool.PoolConfig; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import redis.clients.jedis.Jedis; 7 | 8 | import java.io.IOException; 9 | import java.net.ServerSocket; 10 | import java.util.Properties; 11 | 12 | public class RedisConnectionPoolTest { 13 | 14 | @Before 15 | public void before() throws Exception { 16 | 17 | Thread th = new Thread(new Runnable() { 18 | 19 | private ServerSocket serverSocket; 20 | 21 | @Override 22 | public void run() { 23 | 24 | try { 25 | serverSocket = new ServerSocket(RedisConfig.DEFAULT_PORT); 26 | 27 | serverSocket.accept(); 28 | 29 | } catch (IOException e) { 30 | e.printStackTrace(); 31 | } 32 | } 33 | }); 34 | 35 | th.setDaemon(true); 36 | th.start(); 37 | } 38 | 39 | @Test 40 | public void test() throws Exception { 41 | 42 | Properties prop = new Properties(); 43 | prop.setProperty(RedisConfig.ADDRESS_PROPERTY, 44 | RedisConfig.DEFAULT_HOST + ":" + RedisConfig.DEFAULT_PORT); 45 | prop.setProperty(RedisConfig.CONN_TIMEOUT_PROPERTY, 46 | RedisConfig.DEFAULT_TIMEOUT + ""); 47 | prop.setProperty(RedisConfig.SO_TIMEOUT_PROPERTY, 48 | RedisConfig.DEFAULT_TIMEOUT + ""); 49 | prop.setProperty(RedisConfig.DATABASE_PROPERTY, 50 | RedisConfig.DEFAULT_DATABASE + ""); 51 | 52 | try { 53 | RedisConnectionPool pool = new RedisConnectionPool( 54 | RedisConfig.DEFAULT_HOST, RedisConfig.DEFAULT_PORT); 55 | pool.close(); 56 | } catch (Exception e) { 57 | } 58 | 59 | try { 60 | RedisConnectionPool pool = new RedisConnectionPool( 61 | new PoolConfig(), prop); 62 | pool.close(); 63 | } catch (Exception e) { 64 | } 65 | 66 | try { 67 | RedisConnectionPool pool = new RedisConnectionPool(prop); 68 | 69 | pool.close(); 70 | } catch (Exception e) { 71 | } 72 | 73 | RedisConnectionPool pool = new RedisConnectionPool( 74 | RedisConfig.DEFAULT_HOST, RedisConfig.DEFAULT_PORT); 75 | 76 | try { 77 | pool.getConnection(); 78 | } catch (Exception e) { 79 | } 80 | 81 | try { 82 | 83 | Jedis jedis = pool.getConnection(); 84 | 85 | pool.returnConnection(jedis); 86 | 87 | jedis.disconnect(); 88 | 89 | try { 90 | jedis.auth(""); 91 | } catch (Exception e) { 92 | } 93 | 94 | pool.returnConnection(jedis); 95 | 96 | } catch (Exception e) { 97 | } 98 | 99 | try { 100 | Jedis jedis = pool.getConnection(); 101 | 102 | pool.invalidateConnection(jedis); 103 | 104 | } catch (Exception e) { 105 | 106 | } 107 | 108 | pool.returnConnection(null); 109 | 110 | pool.invalidateConnection(null); 111 | 112 | pool.close(); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/RedisSentinelConnPoolTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.darkphoenixs.pool.PoolConfig; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import redis.clients.jedis.Jedis; 7 | import redis.clients.jedis.JedisSentinelPool; 8 | 9 | import java.io.IOException; 10 | import java.net.ServerSocket; 11 | import java.util.Arrays; 12 | import java.util.HashSet; 13 | import java.util.Properties; 14 | 15 | public class RedisSentinelConnPoolTest { 16 | 17 | @Before 18 | public void before() throws Exception { 19 | 20 | Thread th = new Thread(new Runnable() { 21 | 22 | private ServerSocket serverSocket; 23 | 24 | @Override 25 | public void run() { 26 | 27 | try { 28 | serverSocket = new ServerSocket(6379); 29 | 30 | serverSocket.accept(); 31 | 32 | } catch (IOException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | }); 37 | 38 | th.setDaemon(true); 39 | th.start(); 40 | } 41 | 42 | @Test 43 | public void test() throws Exception { 44 | 45 | try { 46 | RedisSentinelConnPool pool = new RedisSentinelConnPool("localhost", 47 | new HashSet( 48 | Arrays.asList(new String[]{"localhost:6379"}))); 49 | pool.close(); 50 | } catch (Exception e) { 51 | 52 | } 53 | 54 | try { 55 | RedisSentinelConnPool pool = new RedisSentinelConnPool("localhost", 56 | new HashSet(Arrays 57 | .asList(new String[]{"localhost:6379"}))); 58 | pool.close(); 59 | } catch (Exception e) { 60 | } 61 | 62 | Properties properties = new Properties(); 63 | 64 | try { 65 | RedisSentinelConnPool pool = new RedisSentinelConnPool( 66 | new PoolConfig(), properties); 67 | pool.close(); 68 | } catch (Exception e) { 69 | } 70 | 71 | properties.setProperty(RedisConfig.MASTERNAME_PROPERTY, "mymaster"); 72 | 73 | try { 74 | RedisSentinelConnPool pool = new RedisSentinelConnPool( 75 | new PoolConfig(), properties); 76 | pool.close(); 77 | } catch (Exception e) { 78 | } 79 | 80 | properties 81 | .setProperty(RedisConfig.SENTINELS_PROPERTY, "localhost:6379"); 82 | 83 | properties.setProperty(RedisConfig.CONN_TIMEOUT_PROPERTY, 84 | RedisConfig.DEFAULT_TIMEOUT + ""); 85 | properties.setProperty(RedisConfig.SO_TIMEOUT_PROPERTY, 86 | RedisConfig.DEFAULT_TIMEOUT + ""); 87 | properties.setProperty(RedisConfig.DATABASE_PROPERTY, 88 | RedisConfig.DEFAULT_DATABASE + ""); 89 | properties.setProperty(RedisConfig.CLIENTNAME_PROPERTY, "Test"); 90 | properties.setProperty(RedisConfig.PASSWORD_PROPERTY, "Test"); 91 | 92 | try { 93 | RedisSentinelConnPool pool = new RedisSentinelConnPool( 94 | new PoolConfig(), properties); 95 | 96 | pool.close(); 97 | } catch (Exception e) { 98 | } 99 | 100 | try { 101 | RedisSentinelConnPool pool = new RedisSentinelConnPool(new Properties()); 102 | 103 | pool.close(); 104 | } catch (Exception e) { 105 | } 106 | 107 | try { 108 | RedisSentinelConnPool pool = new RedisSentinelConnPool(properties); 109 | 110 | pool.close(); 111 | } catch (Exception e) { 112 | } 113 | 114 | JedisSentinelPool jsp = null; 115 | 116 | RedisSentinelConnPool pool = new RedisSentinelConnPool(jsp); 117 | 118 | try { 119 | pool.getConnection(); 120 | 121 | } catch (Exception e){ 122 | 123 | } 124 | 125 | try { 126 | pool.close(); 127 | 128 | } catch (Exception e){ 129 | 130 | } 131 | 132 | try { 133 | 134 | pool.returnConnection(null); 135 | 136 | pool.invalidateConnection(null); 137 | 138 | RedisConnectionPool spool = new RedisConnectionPool( 139 | RedisConfig.DEFAULT_HOST, RedisConfig.DEFAULT_PORT); 140 | 141 | Jedis jedis1 = spool.getConnection(); 142 | 143 | pool.returnConnection(jedis1); 144 | 145 | Jedis jedis2 = spool.getConnection(); 146 | 147 | pool.invalidateConnection(jedis2); 148 | 149 | } catch (Exception e) { 150 | 151 | } 152 | } 153 | 154 | } 155 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/RedisShardedConnPoolOldTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.apache.commons.pool2.impl.DefaultPooledObject; 4 | import org.darkphoenixs.pool.PoolConfig; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | import redis.clients.jedis.JedisShardInfo; 8 | import redis.clients.jedis.ShardedJedis; 9 | import redis.clients.util.Hashing; 10 | 11 | import java.io.IOException; 12 | import java.net.ServerSocket; 13 | import java.util.Arrays; 14 | import java.util.regex.Pattern; 15 | 16 | public class RedisShardedConnPoolOldTest { 17 | 18 | 19 | @Before 20 | public void before() throws Exception { 21 | 22 | Thread th = new Thread(new Runnable() { 23 | 24 | private ServerSocket serverSocket; 25 | 26 | @Override 27 | public void run() { 28 | 29 | try { 30 | serverSocket = new ServerSocket(RedisConfig.DEFAULT_PORT); 31 | 32 | serverSocket.accept(); 33 | 34 | } catch (IOException e) { 35 | e.printStackTrace(); 36 | } 37 | } 38 | }); 39 | 40 | th.setDaemon(true); 41 | th.start(); 42 | } 43 | 44 | @Test 45 | public void test_0() throws Exception { 46 | 47 | JedisShardInfo info = new JedisShardInfo(RedisConfig.DEFAULT_HOST, 48 | RedisConfig.DEFAULT_PORT); 49 | 50 | RedisShardedConnPoolOld pool = new RedisShardedConnPoolOld(new PoolConfig(), 51 | Arrays.asList(new JedisShardInfo[]{info})); 52 | 53 | pool.close(); 54 | 55 | RedisShardedConnPoolOld.RedisShardedConnFactory factory = pool.new RedisShardedConnFactory( 56 | Arrays.asList(new JedisShardInfo[]{info}), 57 | Hashing.MURMUR_HASH, null); 58 | 59 | factory.activateObject(factory.makeObject()); 60 | 61 | factory.validateObject(factory.makeObject()); 62 | 63 | factory.passivateObject(factory.makeObject()); 64 | 65 | factory.destroyObject(factory.makeObject()); 66 | 67 | try { 68 | factory.activateObject(null); 69 | } catch (Exception e) { 70 | } 71 | 72 | try { 73 | factory.validateObject(null); 74 | } catch (Exception e) { 75 | } 76 | 77 | try { 78 | factory.passivateObject(null); 79 | } catch (Exception e) { 80 | } 81 | 82 | try { 83 | factory.destroyObject(null); 84 | } catch (Exception e) { 85 | } 86 | 87 | try { 88 | factory.validateObject(new DefaultPooledObject( 89 | new ShardedJedis(Arrays 90 | .asList(new JedisShardInfo[]{info})))); 91 | } catch (Exception e) { 92 | } 93 | 94 | try { 95 | factory.validateObject(new DefaultPooledObject( 96 | new ShardedJedisConn(Arrays 97 | .asList(new JedisShardInfo[]{info})))); 98 | } catch (Exception e) { 99 | } 100 | 101 | try { 102 | factory.validateObject(new DefaultPooledObject( 103 | new ShardedJedisConn2(Arrays 104 | .asList(new JedisShardInfo[]{info})))); 105 | } catch (Exception e) { 106 | } 107 | 108 | try { 109 | factory.validateObject(new DefaultPooledObject( 110 | new ShardedJedisConn3(Arrays 111 | .asList(new JedisShardInfo[]{info})))); 112 | } catch (Exception e) { 113 | } 114 | 115 | try { 116 | factory.validateObject(new DefaultPooledObject( 117 | new ShardedJedisConn4(Arrays 118 | .asList(new JedisShardInfo[]{info})))); 119 | } catch (Exception e) { 120 | } 121 | 122 | try { 123 | factory.destroyObject(new DefaultPooledObject( 124 | new ShardedJedisConn(Arrays 125 | .asList(new JedisShardInfo[]{info})))); 126 | } catch (Exception e) { 127 | } 128 | 129 | try { 130 | factory.destroyObject(new DefaultPooledObject( 131 | new ShardedJedisConn2(Arrays 132 | .asList(new JedisShardInfo[]{info})))); 133 | } catch (Exception e) { 134 | } 135 | 136 | try { 137 | factory.destroyObject(new DefaultPooledObject( 138 | new ShardedJedisConn3(Arrays 139 | .asList(new JedisShardInfo[]{info})))); 140 | } catch (Exception e) { 141 | } 142 | 143 | try { 144 | factory.destroyObject(new DefaultPooledObject( 145 | new ShardedJedisConn4(Arrays 146 | .asList(new JedisShardInfo[]{info})))); 147 | } catch (Exception e) { 148 | } 149 | } 150 | 151 | @Test 152 | public void test_1() throws Exception { 153 | 154 | JedisShardInfo info = new JedisShardInfo(RedisConfig.DEFAULT_HOST, 155 | RedisConfig.DEFAULT_PORT); 156 | 157 | RedisShardedConnPoolOld pool = new RedisShardedConnPoolOld(new PoolConfig(), 158 | Arrays.asList(new JedisShardInfo[]{info}), 159 | Pattern.compile("")); 160 | 161 | try { 162 | pool.getConnection(); 163 | } catch (Exception e) { 164 | } 165 | 166 | ShardedJedis shardedJedis = pool.getConnection(); 167 | 168 | try { 169 | pool.returnConnection(shardedJedis); 170 | } catch (Exception e) { 171 | } 172 | 173 | shardedJedis.close(); 174 | 175 | try { 176 | pool.returnConnection(shardedJedis); 177 | } catch (Exception e) { 178 | } 179 | 180 | try { 181 | pool.returnConnection(new ShardedJedisConn5(Arrays 182 | .asList(new JedisShardInfo[]{info}))); 183 | } catch (Exception e) { 184 | } 185 | 186 | try { 187 | pool.invalidateConnection(null); 188 | } catch (Exception e) { 189 | } 190 | 191 | pool.close(); 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/RedisShardedConnPoolTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.darkphoenixs.pool.PoolConfig; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import redis.clients.jedis.JedisShardInfo; 7 | import redis.clients.jedis.ShardedJedis; 8 | 9 | import java.io.IOException; 10 | import java.net.ServerSocket; 11 | import java.util.Arrays; 12 | import java.util.regex.Pattern; 13 | 14 | public class RedisShardedConnPoolTest { 15 | 16 | @Before 17 | public void before() throws Exception { 18 | 19 | Thread th = new Thread(new Runnable() { 20 | 21 | private ServerSocket serverSocket; 22 | 23 | @Override 24 | public void run() { 25 | 26 | try { 27 | serverSocket = new ServerSocket(RedisConfig.DEFAULT_PORT); 28 | 29 | serverSocket.accept(); 30 | 31 | } catch (IOException e) { 32 | e.printStackTrace(); 33 | } 34 | } 35 | }); 36 | 37 | th.setDaemon(true); 38 | th.start(); 39 | } 40 | 41 | @Test 42 | public void test() throws Exception { 43 | 44 | JedisShardInfo info1 = new JedisShardInfo(RedisConfig.DEFAULT_HOST, 45 | RedisConfig.DEFAULT_PORT); 46 | 47 | RedisShardedConnPool pool1 = new RedisShardedConnPool(new PoolConfig(), 48 | Arrays.asList(new JedisShardInfo[]{info1})); 49 | pool1.close(); 50 | 51 | JedisShardInfo info = new JedisShardInfo(RedisConfig.DEFAULT_HOST, 52 | RedisConfig.DEFAULT_PORT); 53 | 54 | RedisShardedConnPool pool = new RedisShardedConnPool(new PoolConfig(), 55 | Arrays.asList(new JedisShardInfo[]{info}), 56 | Pattern.compile("")); 57 | 58 | try { 59 | pool.getConnection(); 60 | } catch (Exception e) { 61 | } 62 | 63 | ShardedJedis shardedJedis = pool.getConnection(); 64 | 65 | try { 66 | pool.returnConnection(shardedJedis); 67 | } catch (Exception e) { 68 | } 69 | 70 | try { 71 | shardedJedis.close(); 72 | } catch (Exception e) { 73 | } 74 | 75 | try { 76 | pool.returnConnection(shardedJedis); 77 | } catch (Exception e) { 78 | } 79 | 80 | try { 81 | pool.returnConnection(new ShardedJedisConn5(Arrays 82 | .asList(new JedisShardInfo[]{info}))); 83 | } catch (Exception e) { 84 | } 85 | 86 | try { 87 | pool.returnConnection(null); 88 | } catch (Exception e) { 89 | } 90 | 91 | try { 92 | pool.invalidateConnection(new ShardedJedisConn5(Arrays 93 | .asList(new JedisShardInfo[]{info}))); 94 | } catch (Exception e) { 95 | } 96 | 97 | try { 98 | pool.invalidateConnection(null); 99 | } catch (Exception e) { 100 | } 101 | 102 | pool.close(); 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/RedisTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import org.junit.Test; 4 | 5 | public class RedisTest { 6 | 7 | @Test 8 | public void test() throws Exception { 9 | 10 | // PoolConfig config = new PoolConfig(); 11 | // config.setMaxTotal(20); 12 | // config.setMaxIdle(5); 13 | // config.setMaxWaitMillis(1000); 14 | // config.setTestOnBorrow(true); 15 | // 16 | // RedisConnectionPool pool = new RedisConnectionPool(config, "localhost", 6379); 17 | // 18 | // for (int i = 0; i < 1000; i++) { 19 | // 20 | // Jedis jedis = pool.getConnection(); 21 | // 22 | // jedis.set("key" + i, "value" + i); 23 | // 24 | // pool.returnConnection(jedis); 25 | // } 26 | // 27 | // pool.close(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/ShardedJedisConn.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import redis.clients.jedis.Jedis; 4 | import redis.clients.jedis.JedisShardInfo; 5 | import redis.clients.jedis.ShardedJedis; 6 | 7 | import java.util.Collection; 8 | import java.util.Collections; 9 | import java.util.List; 10 | 11 | public class ShardedJedisConn extends ShardedJedis { 12 | 13 | public ShardedJedisConn(List shards) { 14 | super(shards); 15 | } 16 | 17 | @Override 18 | public Collection getAllShards() { 19 | 20 | Jedis jedis = new JedisConn(); 21 | 22 | return Collections.singletonList(jedis); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/ShardedJedisConn2.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import redis.clients.jedis.Jedis; 4 | import redis.clients.jedis.JedisShardInfo; 5 | import redis.clients.jedis.ShardedJedis; 6 | 7 | import java.util.Collection; 8 | import java.util.Collections; 9 | import java.util.List; 10 | 11 | public class ShardedJedisConn2 extends ShardedJedis { 12 | 13 | public ShardedJedisConn2(List shards) { 14 | super(shards); 15 | } 16 | 17 | @Override 18 | public Collection getAllShards() { 19 | 20 | Jedis jedis = new JedisConn2(); 21 | 22 | return Collections.singletonList(jedis); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/ShardedJedisConn3.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import redis.clients.jedis.Jedis; 4 | import redis.clients.jedis.JedisShardInfo; 5 | import redis.clients.jedis.ShardedJedis; 6 | 7 | import java.util.Collection; 8 | import java.util.Collections; 9 | import java.util.List; 10 | 11 | public class ShardedJedisConn3 extends ShardedJedis { 12 | 13 | public ShardedJedisConn3(List shards) { 14 | super(shards); 15 | } 16 | 17 | @Override 18 | public Collection getAllShards() { 19 | 20 | Jedis jedis = new JedisConn3(); 21 | 22 | return Collections.singletonList(jedis); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/ShardedJedisConn4.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import redis.clients.jedis.Jedis; 4 | import redis.clients.jedis.JedisShardInfo; 5 | import redis.clients.jedis.ShardedJedis; 6 | 7 | import java.util.Collection; 8 | import java.util.Collections; 9 | import java.util.List; 10 | 11 | public class ShardedJedisConn4 extends ShardedJedis { 12 | 13 | public ShardedJedisConn4(List shards) { 14 | super(shards); 15 | } 16 | 17 | @Override 18 | public Collection getAllShards() { 19 | 20 | Jedis jedis = new JedisConn4(); 21 | 22 | return Collections.singletonList(jedis); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/redis/ShardedJedisConn5.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.redis; 2 | 3 | import redis.clients.jedis.Client; 4 | import redis.clients.jedis.Jedis; 5 | import redis.clients.jedis.JedisShardInfo; 6 | import redis.clients.jedis.ShardedJedis; 7 | 8 | import java.util.Collection; 9 | import java.util.Collections; 10 | import java.util.List; 11 | 12 | public class ShardedJedisConn5 extends ShardedJedis { 13 | 14 | public ShardedJedisConn5(List shards) { 15 | super(shards); 16 | } 17 | 18 | @Override 19 | public Collection getAllShards() { 20 | 21 | Jedis jedis = new Jedis() { 22 | 23 | @Override 24 | public Client getClient() { 25 | 26 | return new Client() { 27 | 28 | @Override 29 | public boolean isBroken() { 30 | 31 | return true; 32 | } 33 | }; 34 | } 35 | }; 36 | 37 | return Collections.singletonList(jedis); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/socket/SocketConfigTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.socket; 17 | 18 | 19 | import org.junit.Assert; 20 | import org.junit.Test; 21 | 22 | public class SocketConfigTest { 23 | 24 | @Test 25 | public void test() throws Exception { 26 | 27 | Assert.assertEquals(SocketConfig.DEFAULT_HOST, "localhost"); 28 | Assert.assertEquals(SocketConfig.DEFAULT_PORT, 1234); 29 | Assert.assertEquals(SocketConfig.DEFAULT_TIMEOUT, 2000); 30 | Assert.assertEquals(SocketConfig.DEFAULT_BUFFERSIZE, 3072); 31 | Assert.assertEquals(SocketConfig.DEFAULT_LINGER, 0); 32 | Assert.assertFalse(SocketConfig.DEFAULT_KEEPALIVE); 33 | Assert.assertFalse(SocketConfig.DEFAULT_TCPNODELAY); 34 | Assert.assertNull(SocketConfig.DEFAULT_PERFORMANCE); 35 | 36 | Assert.assertEquals(SocketConfig.ADDRESS_PROPERTY, "address"); 37 | Assert.assertEquals(SocketConfig.RECE_BUFFERSIZE_PROPERTY, "receiveBufferSize"); 38 | Assert.assertEquals(SocketConfig.SEND_BUFFERSIZE_PROPERTY, "sendBufferSize"); 39 | Assert.assertEquals(SocketConfig.CONN_TIMEOUT_PROPERTY, "connectionTimeout"); 40 | Assert.assertEquals(SocketConfig.SO_TIMEOUT_PROPERTY, "soTimeout"); 41 | Assert.assertEquals(SocketConfig.LINGER_PROPERTY, "linger"); 42 | Assert.assertEquals(SocketConfig.KEEPALIVE_PROPERTY, "keepAlive"); 43 | Assert.assertEquals(SocketConfig.TCPNODELAY_PROPERTY, "tcpNoDelay"); 44 | Assert.assertEquals(SocketConfig.PERFORMANCE_PROPERTY, "performance"); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/socket/SocketConnectionFactoryTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.socket; 17 | 18 | import org.apache.commons.pool2.PooledObject; 19 | import org.apache.commons.pool2.impl.DefaultPooledObject; 20 | import org.junit.Before; 21 | import org.junit.Test; 22 | 23 | import java.io.IOException; 24 | import java.net.ServerSocket; 25 | import java.net.Socket; 26 | import java.util.Properties; 27 | 28 | public class SocketConnectionFactoryTest { 29 | 30 | 31 | @Before 32 | public void before() throws Exception { 33 | 34 | Thread th = new Thread(new Runnable() { 35 | 36 | private ServerSocket serverSocket; 37 | 38 | @Override 39 | public void run() { 40 | 41 | try { 42 | serverSocket = new ServerSocket(1234); 43 | 44 | serverSocket.accept(); 45 | 46 | } catch (IOException e) { 47 | e.printStackTrace(); 48 | } 49 | } 50 | }); 51 | 52 | th.setDaemon(true); 53 | th.start(); 54 | } 55 | 56 | @Test 57 | public void test_0() throws Exception { 58 | 59 | SocketConnectionFactory factory = new SocketConnectionFactory( 60 | SocketConfig.DEFAULT_HOST, SocketConfig.DEFAULT_PORT, 61 | SocketConfig.DEFAULT_BUFFERSIZE, 62 | SocketConfig.DEFAULT_BUFFERSIZE, SocketConfig.DEFAULT_TIMEOUT, 63 | SocketConfig.DEFAULT_TIMEOUT, 1, true, true, new String[]{ 64 | "0", "1", "2"}); 65 | 66 | PooledObject pooledObject = null; 67 | try { 68 | pooledObject = factory.makeObject(); 69 | } catch (Exception e) { 70 | } 71 | 72 | factory.activateObject(new DefaultPooledObject(new Socket())); 73 | try { 74 | factory.activateObject(new DefaultPooledObject(null)); 75 | } catch (Exception e) { 76 | } 77 | 78 | factory.validateObject(new DefaultPooledObject(new Socket())); 79 | try { 80 | factory.validateObject(new DefaultPooledObject(null)); 81 | } catch (Exception e) { 82 | } 83 | 84 | try { 85 | factory.validateObject(pooledObject); 86 | } catch (Exception e) { 87 | } 88 | 89 | try { 90 | pooledObject.getObject().close(); 91 | factory.validateObject(pooledObject); 92 | } catch (Exception e) { 93 | } 94 | 95 | factory.passivateObject(new DefaultPooledObject(new Socket())); 96 | try { 97 | factory.passivateObject(new DefaultPooledObject(null)); 98 | } catch (Exception e) { 99 | } 100 | 101 | factory.destroyObject(new DefaultPooledObject(new Socket())); 102 | try { 103 | factory.destroyObject(new DefaultPooledObject(null)); 104 | } catch (Exception e) { 105 | } 106 | } 107 | 108 | @Test 109 | public void test_1() throws Exception { 110 | 111 | Properties pro = new Properties(); 112 | 113 | try { 114 | new SocketConnectionFactory(pro); 115 | } catch (Exception e) { 116 | } 117 | 118 | pro.setProperty(SocketConfig.ADDRESS_PROPERTY, 119 | SocketConfig.DEFAULT_HOST + ":" + SocketConfig.DEFAULT_PORT); 120 | pro.setProperty(SocketConfig.RECE_BUFFERSIZE_PROPERTY, 121 | SocketConfig.DEFAULT_BUFFERSIZE + ""); 122 | pro.setProperty(SocketConfig.SEND_BUFFERSIZE_PROPERTY, 123 | SocketConfig.DEFAULT_BUFFERSIZE + ""); 124 | pro.setProperty(SocketConfig.CONN_TIMEOUT_PROPERTY, 125 | SocketConfig.DEFAULT_TIMEOUT + ""); 126 | pro.setProperty(SocketConfig.SO_TIMEOUT_PROPERTY, 127 | SocketConfig.DEFAULT_TIMEOUT + ""); 128 | pro.setProperty(SocketConfig.LINGER_PROPERTY, 129 | SocketConfig.DEFAULT_LINGER + ""); 130 | pro.setProperty(SocketConfig.KEEPALIVE_PROPERTY, 131 | SocketConfig.DEFAULT_KEEPALIVE + ""); 132 | pro.setProperty(SocketConfig.TCPNODELAY_PROPERTY, 133 | SocketConfig.TCPNODELAY_PROPERTY + ""); 134 | 135 | new SocketConnectionFactory(pro); 136 | 137 | Properties pro2 = new Properties(); 138 | pro2.setProperty(SocketConfig.ADDRESS_PROPERTY, 139 | SocketConfig.DEFAULT_HOST + ":" + SocketConfig.DEFAULT_PORT); 140 | 141 | try { 142 | new SocketConnectionFactory(pro2).createConnection(); 143 | } catch (Exception e) { 144 | } 145 | 146 | Properties pro3 = new Properties(); 147 | pro3.setProperty(SocketConfig.ADDRESS_PROPERTY, 148 | SocketConfig.DEFAULT_HOST + ":" + 1233); 149 | 150 | try { 151 | new SocketConnectionFactory(pro3).createConnection(); 152 | } catch (Exception e) { 153 | } 154 | 155 | Properties pro4 = new Properties(); 156 | pro4.setProperty(SocketConfig.ADDRESS_PROPERTY, 157 | SocketConfig.DEFAULT_HOST + ":" + 1234); 158 | pro4.setProperty(SocketConfig.PERFORMANCE_PROPERTY, 159 | "0,1,2"); 160 | try { 161 | new SocketConnectionFactory(pro4).createConnection(); 162 | } catch (Exception e) { 163 | } 164 | } 165 | } 166 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/socket/SocketConnectionPoolTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015-2016 Dark Phoenixs (Open-Source Organization). 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package org.darkphoenixs.pool.socket; 17 | 18 | import org.junit.Before; 19 | import org.junit.Test; 20 | 21 | import java.io.IOException; 22 | import java.net.ServerSocket; 23 | import java.util.Properties; 24 | 25 | public class SocketConnectionPoolTest { 26 | 27 | @Before 28 | public void before() throws Exception { 29 | 30 | Thread th = new Thread(new Runnable() { 31 | 32 | private ServerSocket serverSocket; 33 | 34 | @Override 35 | public void run() { 36 | 37 | try { 38 | serverSocket = new ServerSocket(1234); 39 | 40 | serverSocket.accept(); 41 | 42 | } catch (IOException e) { 43 | e.printStackTrace(); 44 | } 45 | } 46 | }); 47 | 48 | th.setDaemon(true); 49 | th.start(); 50 | } 51 | 52 | @Test 53 | public void test_0() throws Exception { 54 | 55 | Properties prop = new Properties(); 56 | 57 | prop.setProperty("address", "localhost:1234"); 58 | 59 | SocketConnectionPool pool0 = new SocketConnectionPool(prop); 60 | 61 | pool0.close(); 62 | 63 | SocketConnectionPool pool = new SocketConnectionPool(); 64 | 65 | try { 66 | pool.getConnection(); 67 | } catch (Exception e) { 68 | } 69 | 70 | try { 71 | pool.returnConnection(null); 72 | } catch (Exception e) { 73 | } 74 | 75 | try { 76 | pool.invalidateConnection(null); 77 | } catch (Exception e) { 78 | } 79 | 80 | pool.close(); 81 | 82 | } 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/test/java/org/darkphoenixs/pool/socket/SocketTest.java: -------------------------------------------------------------------------------- 1 | package org.darkphoenixs.pool.socket; 2 | 3 | import org.junit.Test; 4 | 5 | public class SocketTest { 6 | 7 | @Test 8 | public void test() throws Exception { 9 | 10 | // PoolConfig config = new PoolConfig(); 11 | // config.setMaxTotal(20); 12 | // config.setMaxIdle(5); 13 | // config.setMaxWaitMillis(1000); 14 | // config.setTestOnBorrow(true); 15 | // 16 | // SocketConnectionPool pool = new SocketConnectionPool(config, 17 | // "localhost", 1234); 18 | // 19 | // Socket conn = pool.getConnection(); 20 | // 21 | // PrintWriter out = new PrintWriter(conn.getOutputStream()); 22 | // 23 | // out.write("哈哈"); 24 | // 25 | // out.flush(); 26 | // 27 | // out.close(); 28 | // 29 | // pool.returnConnection(conn); 30 | // 31 | // pool.close(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/test/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | # 2 | # Log4J Settings for log4j 1.2.x (via jakarta-commons-logging) 3 | # 4 | # The five logging levels used by Log are (in order): 5 | # 6 | # 1. DEBUG (the least serious) 7 | # 2. INFO 8 | # 3. WARN 9 | # 4. ERROR 10 | # 5. FATAL (the most serious) 11 | # Set root logger level to WARN and append to stdout 12 | log4j.rootLogger=DEBUG,debugAppender,infoAppender,warnAppender,errorAppender,stdout 13 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender 14 | #log4j.appender.stdout=org.apache.log4j.RollingFileAppender 15 | log4j.appender.stdout.Target=System.out 16 | #log4j.appender.stdout.File=${user.dir}/logs/info.log 17 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 18 | # Pattern to output the caller's file name and line number. 19 | log4j.appender.stdout.layout.ConversionPattern=%d%5p(%c\:%L)-%m%n 20 | # Print only messages of level ERROR or above in the package noModule. 21 | log4j.logger.noModule=FATAL 22 | # OpenSymphony Stuff 23 | log4j.logger.freemarker=INFO 24 | log4j.logger.com.opensymphony=INFO 25 | log4j.appender.debugAppender=org.apache.log4j.RollingFileAppender 26 | log4j.appender.debugAppender.layout=org.apache.log4j.PatternLayout 27 | log4j.appender.debugAppender.MaxFileSize=5MB 28 | log4j.appender.debugAppender.layout.ConversionPattern=%d%5p(%c\:%L)-%m%n 29 | log4j.appender.debugAppender.Threshold=DEBUG 30 | log4j.appender.debugAppender.append=true 31 | log4j.appender.debugAppender.File=logs/debug.log 32 | log4j.appender.infoAppender=org.apache.log4j.RollingFileAppender 33 | log4j.appender.infoAppender.layout=org.apache.log4j.PatternLayout 34 | log4j.appender.infoAppender.MaxFileSize=5MB 35 | log4j.appender.infoAppender.layout.ConversionPattern=%d%5p(%c\:%L)-%m%n 36 | log4j.appender.infoAppender.Threshold=INFO 37 | log4j.appender.infoAppender.append=true 38 | log4j.appender.infoAppender.File=logs/info.log 39 | log4j.appender.warnAppender=org.apache.log4j.RollingFileAppender 40 | log4j.appender.warnAppender.layout=org.apache.log4j.PatternLayout 41 | log4j.appender.warnAppender.MaxFileSize=5MB 42 | log4j.appender.warnAppender.layout.ConversionPattern=%d%5p(%c\:%L)-%m%n 43 | log4j.appender.warnAppender.Threshold=WARN 44 | log4j.appender.warnAppender.append=true 45 | log4j.appender.warnAppender.File=logs/warn.log 46 | log4j.appender.errorAppender=org.apache.log4j.RollingFileAppender 47 | log4j.appender.errorAppender.layout=org.apache.log4j.PatternLayout 48 | log4j.appender.errorAppender.MaxFileSize=5MB 49 | log4j.appender.errorAppender.layout.ConversionPattern=%d%5p(%c\:%L)-%m%n 50 | log4j.appender.errorAppender.Threshold=ERROR 51 | log4j.appender.errorAppender.append=true 52 | log4j.appender.errorAppender.File=logs/error.log 53 | --------------------------------------------------------------------------------