├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── pom.xml
└── src
├── example
├── java
│ └── com
│ │ └── wmz7year
│ │ └── thrift
│ │ └── pool
│ │ └── example
│ │ ├── Example.java
│ │ ├── ExampleServer.java
│ │ └── Other.java
└── thrift
│ ├── example.thrift
│ └── other.thrift
├── main
└── java
│ └── com
│ └── wmz7year
│ └── thrift
│ └── pool
│ ├── AbstractThriftConnectionStrategy.java
│ ├── AcquireFailConfig.java
│ ├── CustomThreadFactory.java
│ ├── DefaultThriftConnectionStrategy.java
│ ├── PoolWatchThread.java
│ ├── TaskEngine.java
│ ├── ThriftConnectionHandle.java
│ ├── ThriftConnectionMaxAgeThread.java
│ ├── ThriftConnectionPartition.java
│ ├── ThriftConnectionPool.java
│ ├── ThriftConnectionStrategy.java
│ ├── ThriftConnectionTesterThread.java
│ ├── config
│ ├── ThriftConnectionPoolConfig.java
│ └── ThriftServerInfo.java
│ ├── connection
│ ├── DefaultThriftConnection.java
│ ├── MulitServiceThriftConnecion.java
│ └── ThriftConnection.java
│ └── exception
│ └── ThriftConnectionPoolException.java
└── test
└── java
└── com
└── wmz7year
└── thrift
└── pool
├── BasicAbstractTest.java
├── CheckConnectionAlive.java
├── DynamicServersTest.java
├── GetConnectionFromServerFailedCounterTest.java
├── MultiplexedServiceTest.java
├── NoThriftServerStartPoolTest.java
├── ThrfitConnectionPoolRemoveServerStopThreadTest.java
├── ThriftServerBindIDTest.java
├── TimeoutConnectionTest.java
└── example
├── Example.java
└── Other.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 |
3 | # Mobile Tools for Java (J2ME)
4 | .mtj.tmp/
5 |
6 | #eclipse
7 | .classpath
8 | .project
9 | .settings/
10 |
11 | #maven
12 | target/
13 |
14 | # Package Files #
15 | *.jar
16 | *.war
17 | *.ear
18 |
19 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
20 | hs_err_pid*
21 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | jdk:
3 | - oraclejdk8
4 | - oraclejdk7
5 |
6 | script:
7 | - mvn clean package install
8 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #Thrift连接池实现
2 |
3 | [](https://gitter.im/wmz7year/Thrift-Connection-Pool?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4 | [](https://travis-ci.org/wmz7year/Thrift-Connection-Pool)
5 |
6 | ##特性
7 | * 1、支持服务器之间的负载均衡
8 | * 2、每个服务器拥有一个独立的连接分区 所有的连接分区合并一起为整个连接池
9 | * 3、连接池支持自动创建连接、管理超时连接、管理失效连接
10 | * 4、支持服务器列表动态增加或者移除
11 | * 5、支持自动调取ping方法(在thrift描述文件添加方法void ping(),)检测连接可用性
12 | * 6、支持当服务不可用时自动将对应的服务器剔除连接池的功能
13 | * 7、添加多服务接口支持
14 |
15 | ###下载
16 | ><dependency>
17 | > <groupId>com.github.wmz7year</groupId>
18 | > <artifactId>ThriftConnectionPool</artifactId>
19 | > <version>1.0.6-RELEASE</version>
20 | ></dependency>
21 |
22 |
23 | ###示例
24 | ####单服务示例
25 | ThriftConnectionPoolConfig config = new ThriftConnectionPoolConfig();
26 | config.setConnectTimeout(3000);
27 | config.setThriftProtocol(TProtocolType.BINARY);
28 | config.setClientClass(Example.Client.class);
29 | config.addThriftServer("127.0.0.1", 9119);
30 | config.setMaxConnectionPerServer(2);
31 | config.setMinConnectionPerServer(1);
32 | config.setIdleMaxAge(2, TimeUnit.SECONDS);
33 | config.setMaxConnectionAge(2);
34 | config.setLazyInit(false);
35 | try {
36 | ThriftConnectionPool pool = new ThriftConnectionPool(config);
37 | Example.Client client = pool.getConnection().getClient();
38 | client.ping();
39 | pool.close();
40 | } catch (ThriftConnectionPoolException e) {
41 | e.printStackTrace();
42 | } catch (TException e) {
43 | e.printStackTrace();
44 | } catch (IOException e) {
45 | e.printStackTrace();
46 | }
47 |
48 | ####多接口服务示例
49 | ThriftConnectionPoolConfig config = new ThriftConnectionPoolConfig(ThriftServiceType.MULTIPLEXED_INTERFACE);
50 | config.setConnectTimeout(3000);
51 | config.setThriftProtocol(TProtocolType.BINARY);
52 | config.addThriftServer("127.0.0.1", 9119);
53 | config.addThriftClientClass("other", Other.Client.class);
54 | config.addThriftClientClass("example", Example.Client.class);
55 |
56 | config.setMaxConnectionPerServer(2);
57 | config.setMinConnectionPerServer(1);
58 | config.setIdleMaxAge(2, TimeUnit.SECONDS);
59 | config.setMaxConnectionAge(2);
60 | config.setLazyInit(false);
61 | config.setAcquireIncrement(2);
62 | config.setAcquireRetryDelay(2000);
63 |
64 | config.setAcquireRetryAttempts(1);
65 | config.setMaxConnectionCreateFailedCount(1);
66 | config.setConnectionTimeoutInMs(5000);
67 |
68 | config.check();
69 |
70 | ThriftConnectionPool pool = new ThriftConnectionPool(config);
71 | ThriftConnection connection = pool.getConnection();
72 | // example service
73 | com.wmz7year.thrift.pool.example.Example.Client exampleServiceClient = connection.getClient("example",
74 | Example.Client.class);
75 | exampleServiceClient.ping();
76 |
77 | // other service
78 | com.wmz7year.thrift.pool.example.Other.Client otherServiceClient = connection.getClient("other",
79 | Other.Client.class);
80 | otherServiceClient.ping();
81 | pool.close();
82 |
83 |
84 |
85 | ####接下来需要完善内容:
86 | 1、补充文档
87 | 2、补充性能测试
88 | 3、完善使用例子
89 | 4、操作重试机制?
90 |
91 |
92 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | com.github.wmz7year
6 | ThriftConnectionPool
7 | 1.0.6-RELEASE
8 | jar
9 |
10 | ThriftConnectionPool
11 | https://github.com/wmz7year/Thrift-Connection-Pool/
12 |
13 | Apache Thrift Client connection pool
14 | 2016
15 |
16 |
17 |
18 | Jiang wei
19 | ydswcy513@gmail.com
20 |
21 |
22 | George Kankava
23 | george.kankava@devfactory.com
24 |
25 |
26 |
27 |
28 |
29 | scm:git:https://github.com/wmz7year/Thrift-Connection-Pool.git
30 |
31 |
32 | scm:git:https://github.com/wmz7year/Thrift-Connection-Pool.git
33 |
34 | https://github.com/wmz7year/Thrift-Connection-Pool/
35 | HEAD
36 |
37 |
38 |
39 | GitHub Issues
40 | https://github.com/wmz7year/Thrift-Connection-Pool/issues
41 |
42 |
43 |
44 |
45 | The Apache License, Version 2.0
46 | http://www.apache.org/licenses/LICENSE-2.0.txt
47 |
48 |
49 |
50 |
51 |
52 | UTF-8
53 | 1.7
54 | 1.7
55 | 1.7
56 |
57 | 0.9.3
58 | 3.8.1
59 | 1.1.3
60 | 1.7.12
61 | 16.0
62 |
63 | 2.2.1
64 | 2.9.1
65 | 1.5
66 |
67 |
68 |
69 |
70 | com.google.guava
71 | guava
72 | ${guava.version}
73 |
74 |
75 | org.apache.thrift
76 | libthrift
77 | ${thrift.version}
78 |
79 |
80 | ch.qos.logback
81 | logback-core
82 | ${logback.version}
83 | compile
84 |
85 |
86 | ch.qos.logback
87 | logback-classic
88 | ${logback.version}
89 |
90 |
91 | org.slf4j
92 | slf4j-api
93 | ${slf4j.version}
94 | compile
95 |
96 |
97 | junit
98 | junit
99 | ${junit.version}
100 | test
101 |
102 |
103 |
104 |
105 |
106 | ossrh
107 | https://oss.sonatype.org/content/repositories/snapshots
108 |
109 |
110 | ossrh
111 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
112 |
113 |
114 |
115 |
116 |
117 |
118 | org.apache.maven.plugins
119 | maven-source-plugin
120 | ${maven.source.plugin}
121 |
122 |
123 | attach-sources
124 |
125 | jar-no-fork
126 |
127 |
128 |
129 |
130 |
131 | org.apache.maven.plugins
132 | maven-javadoc-plugin
133 | ${maven.javadoc.plugin}
134 |
135 |
136 | attach-javadocs
137 |
138 | jar
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 | release
149 |
150 |
151 | ossrh
152 | https://oss.sonatype.org/content/repositories/snapshots/
153 |
154 |
155 | ossrh
156 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
157 |
158 |
159 |
160 |
161 |
162 | org.sonatype.plugins
163 | nexus-staging-maven-plugin
164 | 1.6.3
165 | true
166 |
167 | ossrh
168 | https://oss.sonatype.org/
169 | true
170 |
171 |
172 |
173 | org.apache.maven.plugins
174 | maven-release-plugin
175 | 2.5
176 |
177 | true
178 | false
179 | release
180 | deploy
181 |
182 |
183 |
184 | org.apache.maven.plugins
185 | maven-compiler-plugin
186 | 3.0
187 |
188 | 1.7
189 | 1.7
190 |
191 |
192 |
193 | org.apache.maven.plugins
194 | maven-gpg-plugin
195 | 1.5
196 |
197 |
198 | sign-artifacts
199 | verify
200 |
201 | sign
202 |
203 |
204 |
205 |
206 |
207 | org.apache.maven.plugins
208 | maven-source-plugin
209 | 2.2.1
210 |
211 |
212 | attach-sources
213 |
214 | jar-no-fork
215 |
216 |
217 |
218 |
219 |
220 | org.apache.maven.plugins
221 | maven-javadoc-plugin
222 | 2.9
223 |
224 |
225 | attach-javadocs
226 |
227 | jar
228 |
229 |
230 |
231 |
232 |
233 |
234 |
235 |
236 |
237 |
238 |
--------------------------------------------------------------------------------
/src/example/java/com/wmz7year/thrift/pool/example/ExampleServer.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Jiang Wei
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 |
17 | package com.wmz7year.thrift.pool.example;
18 |
19 | import org.apache.thrift.TException;
20 | import org.apache.thrift.protocol.TBinaryProtocol;
21 | import org.apache.thrift.protocol.TBinaryProtocol.Factory;
22 | import org.apache.thrift.server.TServer;
23 | import org.apache.thrift.server.TThreadPoolServer;
24 | import org.apache.thrift.server.TThreadPoolServer.Args;
25 | import org.apache.thrift.transport.TServerSocket;
26 | import org.apache.thrift.transport.TServerTransport;
27 | import org.apache.thrift.transport.TTransportException;
28 |
29 | import com.wmz7year.thrift.pool.example.Example.Iface;
30 | import com.wmz7year.thrift.pool.example.Example.Processor;
31 |
32 | /**
33 | * 测试thrift服务器
34 | *
35 | * @Title: ExampleServer.java
36 | * @Package com.wmz7year.thrift.pool.example
37 | * @author jiangwei (ydswcy513@gmail.com)
38 | * @date 2015年11月18日 上午9:48:15
39 | * @version V1.0
40 | */
41 | public class ExampleServer {
42 | public static void main(String[] args) {
43 | try {
44 | int port = 9119;
45 | TServerTransport serverTransport = new TServerSocket(port);
46 | Factory proFactory = new TBinaryProtocol.Factory();
47 | Processor processor = new Example.Processor(new Example.Iface() {
48 |
49 | @Override
50 | public void pong() throws TException {
51 | System.out.println("pong");
52 | }
53 |
54 | @Override
55 | public void ping() throws TException {
56 | System.out.println("ping");
57 | }
58 | });
59 | Args thriftArgs = new Args(serverTransport);
60 | thriftArgs.processor(processor);
61 | thriftArgs.protocolFactory(proFactory);
62 | TServer tserver = new TThreadPoolServer(thriftArgs);
63 | System.out.println("启动监听:" + port);
64 | tserver.serve();
65 | } catch (TTransportException e) {
66 | e.printStackTrace();
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/example/thrift/example.thrift:
--------------------------------------------------------------------------------
1 | namespace java com.wmz7year.thrift.pool.example
2 |
3 | service Example {
4 | void ping(),
5 | void pong(),
6 | }
--------------------------------------------------------------------------------
/src/example/thrift/other.thrift:
--------------------------------------------------------------------------------
1 | namespace java com.wmz7year.thrift.pool.example
2 |
3 | service Other {
4 | void ping(),
5 | void pong(),
6 | }
--------------------------------------------------------------------------------
/src/main/java/com/wmz7year/thrift/pool/AbstractThriftConnectionStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Jiang Wei
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 |
17 | package com.wmz7year.thrift.pool;
18 |
19 | import java.io.Serializable;
20 | import java.util.concurrent.locks.Lock;
21 | import java.util.concurrent.locks.ReentrantLock;
22 |
23 | import org.apache.thrift.TServiceClient;
24 |
25 | import com.wmz7year.thrift.pool.connection.ThriftConnection;
26 | import com.wmz7year.thrift.pool.exception.ThriftConnectionPoolException;
27 |
28 | /**
29 | * 连接操作策略接口抽象实现类
30 | *
31 | * @author jiangwei (ydswcy513@gmail.com)
32 | * @version V1.0
33 | */
34 | public abstract class AbstractThriftConnectionStrategy
35 | implements ThriftConnectionStrategy, Serializable {
36 | private static final long serialVersionUID = 8901255269360914953L;
37 | /**
38 | * 连接池对象
39 | */
40 | protected ThriftConnectionPool pool;
41 |
42 | /**
43 | * 关闭锁,防止一次性关闭所有连接时出现问题
44 | */
45 | protected Lock terminationLock = new ReentrantLock();
46 |
47 | /**
48 | * 准备一个新连接的方法
49 | *
50 | * @return 如果启动统计 返回获取连接的时间
51 | * @throws ThriftConnectionPoolException
52 | * 当获取连接出现问题时抛出该异常
53 | */
54 | protected long preConnection() throws ThriftConnectionPoolException {
55 | long statsObtainTime = 0;
56 |
57 | if (this.pool.poolShuttingDown) {
58 | throw new ThriftConnectionPoolException(this.pool.shutdownStackTrace);
59 | }
60 |
61 | return statsObtainTime;
62 | }
63 |
64 | /**
65 | * 获取连接后执行其他任务
66 | *
67 | * @param handle
68 | * 连接代理类
69 | * @param statsObtainTime
70 | * 准备获取连接的时间
71 | */
72 | protected void postConnection(ThriftConnectionHandle handle, long statsObtainTime) {
73 |
74 | handle.renewConnection(); // 重置连接代理类中的信息 变成新连接
75 |
76 | }
77 |
78 | /*
79 | * @see com.wmz7year.thrift.pool.ThriftConnectionStrategy#getConnection()
80 | */
81 | @Override
82 | public ThriftConnection getConnection() throws ThriftConnectionPoolException {
83 | long statsObtainTime = preConnection();
84 |
85 | ThriftConnectionHandle result = (ThriftConnectionHandle) getConnectionInternal();
86 | if (result != null) {
87 | postConnection(result, statsObtainTime);
88 | }
89 |
90 | return result;
91 | }
92 |
93 | /*
94 | * @see
95 | * com.wmz7year.thrift.pool.ThriftConnectionStrategy#getConnection(byte[])
96 | */
97 | @Override
98 | public ThriftConnection getConnection(byte[] nodeID) throws ThriftConnectionPoolException {
99 | long statsObtainTime = preConnection();
100 |
101 | ThriftConnectionHandle result = (ThriftConnectionHandle) getConnectionInternal(nodeID);
102 | if (result != null) {
103 | postConnection(result, statsObtainTime);
104 | }
105 |
106 | return result;
107 | }
108 |
109 | /**
110 | * 获取连接代理对象的方法
111 | *
112 | * @return thrift服务器连接对象
113 | * @throws ThriftConnectionPoolException
114 | * 当获取连接出现问题时抛出该异常
115 | */
116 | protected abstract ThriftConnection getConnectionInternal() throws ThriftConnectionPoolException;
117 |
118 | /**
119 | * 使用指定服务器节点获取连接代理对象的方法
120 | *
121 | * @param nodeID
122 | * thrift服务器节点ID
123 | * @return thrift服务器连接对象
124 | * @throws ThriftConnectionPoolException
125 | * 当获取连接出现问题时抛出该异常
126 | */
127 | protected abstract ThriftConnection getConnectionInternal(byte[] nodeID) throws ThriftConnectionPoolException;
128 |
129 | /*
130 | * @see
131 | * com.wmz7year.thrift.pool.ThriftConnectionStrategy#cleanupConnection(com.
132 | * wmz7year.thrift.pool.ThriftConnectionHandle,
133 | * com.wmz7year.thrift.pool.ThriftConnectionHandle)
134 | */
135 | @Override
136 | public void cleanupConnection(ThriftConnectionHandle oldHandler, ThriftConnectionHandle newHandler) {
137 | // do nothing
138 | }
139 |
140 | }
141 |
--------------------------------------------------------------------------------
/src/main/java/com/wmz7year/thrift/pool/AcquireFailConfig.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Jiang Wei
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 |
17 | package com.wmz7year.thrift.pool;
18 |
19 | import java.util.concurrent.atomic.AtomicInteger;
20 |
21 | /**
22 | * 连接获取失败时重试配置
23 | *
24 | * @author jiangwei (ydswcy513@gmail.com)
25 | * @version V1.0
26 | */
27 | public class AcquireFailConfig {
28 | /**
29 | * 记录日志的内容
30 | */
31 | private String logMessage = "";
32 | /**
33 | * 重试次数
34 | */
35 | private AtomicInteger acquireRetryAttempts = new AtomicInteger();
36 | /**
37 | * 重试等待时间
38 | */
39 | private long acquireRetryDelayInMs;
40 |
41 | public void setAcquireRetryAttempts(AtomicInteger acquireRetryAttempts) {
42 | this.acquireRetryAttempts = acquireRetryAttempts;
43 | }
44 |
45 | public void setAcquireRetryDelayInMs(long acquireRetryDelayInMs) {
46 | this.acquireRetryDelayInMs = acquireRetryDelayInMs;
47 | }
48 |
49 | public void setLogMessage(String logMessage) {
50 | this.logMessage = logMessage;
51 | }
52 |
53 | public String getLogMessage() {
54 | return logMessage;
55 | }
56 |
57 | public AtomicInteger getAcquireRetryAttempts() {
58 | return acquireRetryAttempts;
59 | }
60 |
61 | public long getAcquireRetryDelayInMs() {
62 | return acquireRetryDelayInMs;
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/src/main/java/com/wmz7year/thrift/pool/CustomThreadFactory.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Jiang Wei
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 |
17 | package com.wmz7year.thrift.pool;
18 |
19 | import java.lang.Thread.UncaughtExceptionHandler;
20 | import java.util.concurrent.ThreadFactory;
21 |
22 | import org.slf4j.Logger;
23 | import org.slf4j.LoggerFactory;
24 |
25 | /**
26 | * 负责创建线程的工厂类
27 | * 用于设置线程名称以及异常捕获
28 | *
29 | * @author jiangwei (ydswcy513@gmail.com)
30 | * @version V1.0
31 | */
32 | public class CustomThreadFactory implements ThreadFactory, UncaughtExceptionHandler {
33 | private static final Logger logger = LoggerFactory.getLogger(CustomThreadFactory.class);
34 |
35 | /** 是否是守护线程的表识位 */
36 | private boolean daemon;
37 | /** 线程名称 */
38 | private String threadName;
39 |
40 | public CustomThreadFactory(String threadName, boolean daemon) {
41 | this.threadName = threadName;
42 | this.daemon = daemon;
43 | }
44 |
45 | /**
46 | * {@inheritDoc}
47 | *
48 | * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
49 | */
50 | @Override
51 | public Thread newThread(Runnable r) {
52 | Thread t = new Thread(r, this.threadName);
53 | t.setDaemon(this.daemon);
54 | t.setUncaughtExceptionHandler(this);
55 | return t;
56 | }
57 |
58 | /**
59 | * {@inheritDoc}
60 | *
61 | * @see java.lang.Thread.UncaughtExceptionHandler#uncaughtException(java.lang.Thread,
62 | * java.lang.Throwable)
63 | */
64 | public void uncaughtException(Thread thread, Throwable throwable) {
65 | logger.error("Uncaught Exception in thread " + thread.getName(), throwable);
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/com/wmz7year/thrift/pool/DefaultThriftConnectionStrategy.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Jiang Wei
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 |
17 | package com.wmz7year.thrift.pool;
18 |
19 | import java.util.Arrays;
20 | import java.util.Collections;
21 | import java.util.LinkedList;
22 | import java.util.List;
23 | import java.util.concurrent.TimeUnit;
24 |
25 | import org.apache.thrift.TServiceClient;
26 |
27 | import com.wmz7year.thrift.pool.config.ThriftServerInfo;
28 | import com.wmz7year.thrift.pool.connection.ThriftConnection;
29 | import com.wmz7year.thrift.pool.exception.ThriftConnectionPoolException;
30 |
31 | /**
32 | * 默认连接池获取策略实现类
33 | *
34 | * @author jiangwei (ydswcy513@gmail.com)
35 | * @version V1.0
36 | */
37 | public class DefaultThriftConnectionStrategy extends AbstractThriftConnectionStrategy {
38 | private static final long serialVersionUID = 142121086900189271L;
39 |
40 | public DefaultThriftConnectionStrategy(ThriftConnectionPool pool) {
41 | this.pool = pool;
42 | }
43 |
44 | /*
45 | * @see com.wmz7year.thrift.pool.AbstractThriftConnectionStrategy#
46 | * getConnectionInternal()
47 | */
48 | @Override
49 | protected ThriftConnection getConnectionInternal() throws ThriftConnectionPoolException {
50 | ThriftConnection result = pollConnection();
51 | // 如果立即获取连接失败 则换一个分区继续获取
52 | // TODO 设置当连接获取超时返回null?
53 | if (result == null) {
54 | if (this.pool.getThriftServerCount() == 0) {
55 | throw new ThriftConnectionPoolException("当前没有可用的服务器 无法获取连接");
56 | }
57 | int partition = (int) (Thread.currentThread().getId() % this.pool.thriftServerCount);
58 |
59 | ThriftConnectionPartition thriftConnectionPartition = this.pool.partitions.get(partition);
60 |
61 | try {
62 | result = thriftConnectionPartition.poolFreeConnection(this.pool.connectionTimeoutInMs,
63 | TimeUnit.MILLISECONDS);
64 | if (result == null) {
65 |
66 | throw new ThriftConnectionPoolException("Timed out waiting for a free available connection.");
67 | }
68 | } catch (InterruptedException e) {
69 | throw new ThriftConnectionPoolException(e);
70 | }
71 | }
72 | return result;
73 | }
74 |
75 | /*
76 | * @see com.wmz7year.thrift.pool.AbstractThriftConnectionStrategy#
77 | * getConnectionInternal(byte[])
78 | */
79 | @Override
80 | protected ThriftConnection getConnectionInternal(byte[] nodeID) throws ThriftConnectionPoolException {
81 | if (nodeID == null) {
82 | throw new NullPointerException();
83 | }
84 | if (this.pool.getThriftServerCount() == 0) {
85 | throw new ThriftConnectionPoolException("当前没有可用的服务器 无法获取连接");
86 | }
87 |
88 | List> partitions = Collections.unmodifiableList(this.pool.partitions);
89 | ThriftConnectionPartition thriftConnectionPartition = null;
90 | for (ThriftConnectionPartition tempPartition : partitions) {
91 | ThriftServerInfo thriftServerInfo = tempPartition.getThriftServerInfo();
92 | if (Arrays.equals(thriftServerInfo.getServerID(), nodeID)) {
93 | thriftConnectionPartition = tempPartition;
94 | break;
95 | }
96 | }
97 | if (thriftConnectionPartition == null) {
98 | throw new ThriftConnectionPoolException("没有找到对应服务器节点:" + Arrays.toString(nodeID));
99 | }
100 |
101 | ThriftConnection result = null;
102 | try {
103 | result = thriftConnectionPartition.poolFreeConnection(this.pool.connectionTimeoutInMs,
104 | TimeUnit.MILLISECONDS);
105 | if (result == null) {
106 |
107 | throw new ThriftConnectionPoolException("Timed out waiting for a free available connection.");
108 | }
109 | } catch (InterruptedException e) {
110 | throw new ThriftConnectionPoolException(e);
111 | }
112 | return result;
113 | }
114 |
115 | /*
116 | * @see
117 | * com.wmz7year.thrift.pool.ThriftConnectionStrategy#terminateAllConnections
118 | * ()
119 | */
120 | @Override
121 | public void terminateAllConnections() {
122 | this.terminationLock.lock();
123 | try {
124 | for (int i = 0; i < this.pool.thriftServerCount; i++) {
125 | this.pool.partitions.get(i).setUnableToCreateMoreTransactions(false);
126 | List> clist = new LinkedList<>();
127 | this.pool.partitions.get(i).getFreeConnections().drainTo(clist);
128 | for (ThriftConnectionHandle c : clist) {
129 | this.pool.destroyConnection(c);
130 | }
131 |
132 | }
133 | } finally {
134 | this.terminationLock.unlock();
135 | }
136 | }
137 |
138 | /*
139 | * @see com.wmz7year.thrift.pool.ThriftConnectionStrategy#pollConnection()
140 | */
141 | @Override
142 | public ThriftConnection pollConnection() {
143 | ThriftConnection result;
144 | if (pool.getThriftServerCount() == 0) {
145 | throw new IllegalStateException("当前无可用连接服务器");
146 | }
147 | int partition = (int) (Thread.currentThread().getId() % this.pool.thriftServerCount);
148 |
149 | ThriftConnectionPartition thriftConnectionPartition = this.pool.partitions.get(partition);
150 |
151 | result = thriftConnectionPartition.poolFreeConnection();
152 | if (result == null) {
153 | for (int i = 0; i < this.pool.thriftServerCount; i++) {
154 | if (i == partition) {
155 | continue;
156 | }
157 | result = this.pool.partitions.get(i).poolFreeConnection();
158 | if (result != null) {
159 | thriftConnectionPartition = this.pool.partitions.get(i);
160 | break;
161 | }
162 | }
163 | }
164 |
165 | if (!thriftConnectionPartition.isUnableToCreateMoreTransactions()) {
166 | this.pool.maybeSignalForMoreConnections(thriftConnectionPartition);
167 | }
168 | return result;
169 | }
170 |
171 | }
172 |
--------------------------------------------------------------------------------
/src/main/java/com/wmz7year/thrift/pool/PoolWatchThread.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Jiang Wei
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 |
17 | package com.wmz7year.thrift.pool;
18 |
19 | import org.apache.thrift.TServiceClient;
20 | import org.slf4j.Logger;
21 | import org.slf4j.LoggerFactory;
22 |
23 | /**
24 | * 连接池内部连接数量监控线程
25 | *
26 | * @author jiangwei (ydswcy513@gmail.com)
27 | * @version V1.0
28 | */
29 | public class PoolWatchThread implements Runnable {
30 | private static final Logger logger = LoggerFactory.getLogger(PoolWatchThread.class);
31 |
32 | /**
33 | * 连接池对象
34 | */
35 | private ThriftConnectionPool thriftConnectionPool;
36 |
37 | /**
38 | * 连接分区对象
39 | */
40 | private ThriftConnectionPartition thriftConnectionPartition;
41 |
42 | /**
43 | * 是否为懒加载
44 | */
45 | protected boolean lazyInit;
46 |
47 | /**
48 | * 连接池连接数比例阈值
49 | */
50 | private int poolAvailabilityThreshold;
51 |
52 | /**
53 | * 获取连接失败时的重试间隔事件
54 | */
55 | private long acquireRetryDelayInMs = 1000L;
56 |
57 | /**
58 | * 最大连接获取失效次数
59 | */
60 | private int maxConnectionCreateFailedCount;
61 |
62 | /**
63 | * 持续获取连接失败的次数
64 | */
65 | private int currentConnectionFailedCount;
66 | /**
67 | * 是否运行的表识位
68 | */
69 | private boolean run;
70 |
71 | public PoolWatchThread(ThriftConnectionPartition thriftConnectionPartition,
72 | ThriftConnectionPool thriftConnectionPool) {
73 | this.thriftConnectionPool = thriftConnectionPool;
74 | this.thriftConnectionPartition = thriftConnectionPartition;
75 | this.lazyInit = this.thriftConnectionPool.getConfig().isLazyInit();
76 | this.acquireRetryDelayInMs = this.thriftConnectionPool.getConfig().getAcquireRetryDelayInMs();
77 | this.poolAvailabilityThreshold = this.thriftConnectionPool.getConfig().getPoolAvailabilityThreshold();
78 | this.maxConnectionCreateFailedCount = this.thriftConnectionPool.getConfig().getMaxConnectionCreateFailedCount();
79 | this.run = true;
80 | this.thriftConnectionPartition.registPoolWatchThread(this);
81 | }
82 |
83 | /*
84 | * @see java.lang.Runnable#run()
85 | */
86 | @Override
87 | public void run() {
88 | int maxNewConnections;
89 |
90 | while (run) {
91 | maxNewConnections = 0;
92 | try {
93 | if (this.lazyInit) { // 无视第一次的信号量
94 | this.thriftConnectionPartition.getPoolWatchThreadSignalQueue().take();
95 | }
96 |
97 | maxNewConnections = this.thriftConnectionPartition.getMaxConnections()
98 | - this.thriftConnectionPartition.getCreatedConnections();
99 |
100 | while (maxNewConnections == 0 || (this.thriftConnectionPartition.getAvailableConnections() * 100
101 | / this.thriftConnectionPartition.getMaxConnections() > this.poolAvailabilityThreshold)) {
102 | if (maxNewConnections == 0) {
103 | this.thriftConnectionPartition.setUnableToCreateMoreTransactions(true);
104 | }
105 |
106 | this.thriftConnectionPartition.getPoolWatchThreadSignalQueue().take();
107 | if (!run) {
108 | break;
109 | }
110 | maxNewConnections = this.thriftConnectionPartition.getMaxConnections()
111 | - this.thriftConnectionPartition.getCreatedConnections();
112 |
113 | }
114 |
115 | if (maxNewConnections > 0 && !this.thriftConnectionPool.poolShuttingDown) {
116 | fillConnections(Math.min(maxNewConnections, this.thriftConnectionPartition.getAcquireIncrement()));
117 |
118 | if (this.thriftConnectionPartition.getCreatedConnections() < this.thriftConnectionPartition
119 | .getMinConnections()) {
120 | fillConnections(this.thriftConnectionPartition.getMinConnections()
121 | - this.thriftConnectionPartition.getCreatedConnections());
122 |
123 | }
124 | }
125 |
126 | if (this.thriftConnectionPool.poolShuttingDown) {
127 | return;
128 | }
129 |
130 | } catch (InterruptedException e) {
131 | // 发生中断事件 停止运行监听器
132 | if (logger.isDebugEnabled()) {
133 | logger.debug("Terminating pool watch thread");
134 | }
135 | return;
136 | }
137 | }
138 | }
139 |
140 | /**
141 | * 触发创建连接的方法
142 | * 新的连接会添加到分区中
143 | *
144 | * @param connectionsToCreate
145 | * 需要创建的连接数
146 | * @throws InterruptedException
147 | * 当添加操作被中断的时候抛出该异常
148 | */
149 | private void fillConnections(int connectionsToCreate) throws InterruptedException {
150 | try {
151 | for (int i = 0; i < connectionsToCreate; i++) {
152 | if (this.thriftConnectionPool.poolShuttingDown) {
153 | break;
154 | }
155 | if (!run) {
156 | break;
157 | }
158 | this.thriftConnectionPartition.addFreeConnection(new ThriftConnectionHandle(null,
159 | this.thriftConnectionPartition, this.thriftConnectionPool, false));
160 | }
161 | // 如果正确的获取连接则失败次数重置为0
162 | currentConnectionFailedCount = 0;
163 | } catch (Exception e) {
164 | currentConnectionFailedCount++;
165 | // 判断连续次数是否是超限了
166 | if (currentConnectionFailedCount == maxConnectionCreateFailedCount) {
167 | logger.error("Error in trying to obtain a connection in " + currentConnectionFailedCount
168 | + " count will remove the server", e);
169 | thriftConnectionPool.destroyThriftConnectionPartition(thriftConnectionPartition);
170 | } else {
171 | logger.error("Error in trying to obtain a connection. Retrying in " + this.acquireRetryDelayInMs + "ms",
172 | e);
173 | Thread.sleep(this.acquireRetryDelayInMs);
174 | }
175 | }
176 | }
177 |
178 | /**
179 | * 停止检测线程的方法
180 | */
181 | public void stop() {
182 | this.run = false;
183 | }
184 |
185 | }
186 |
--------------------------------------------------------------------------------
/src/main/java/com/wmz7year/thrift/pool/TaskEngine.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Jiang Wei
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 |
17 | package com.wmz7year.thrift.pool;
18 |
19 | import java.util.Date;
20 | import java.util.Map;
21 | import java.util.Timer;
22 | import java.util.TimerTask;
23 | import java.util.concurrent.*;
24 | import java.util.concurrent.atomic.AtomicInteger;
25 |
26 | /**
27 | * 线程调度任务引擎
28 | * 负责执行线程或者定时执行线程等操作
29 | *
30 | * @author jiangwei (ydswcy513@gmail.com)
31 | * @version V1.0
32 | */
33 | public class TaskEngine {
34 | private static TaskEngine instance = null;
35 |
36 | private Timer timer;
37 | private ExecutorService executor;
38 | private Map wrappedTasks = new ConcurrentHashMap<>();
39 |
40 | private TaskEngine() {
41 | timer = new Timer("ThriftConnectionPool-TaskEngine-timer", true);
42 | executor = Executors.newCachedThreadPool(new ThreadFactory() {
43 |
44 | final AtomicInteger threadNumber = new AtomicInteger(1);
45 |
46 | @Override
47 | public Thread newThread(Runnable runnable) {
48 | // Use our own naming scheme for the threads.
49 | Thread thread = new Thread(Thread.currentThread().getThreadGroup(), runnable,
50 | "ThriftConnectionPool-TaskEngine-pool-" + threadNumber.getAndIncrement(), 0);
51 | // Make workers daemon threads.
52 | thread.setDaemon(true);
53 | if (thread.getPriority() != Thread.NORM_PRIORITY) {
54 | thread.setPriority(Thread.NORM_PRIORITY);
55 | }
56 | return thread;
57 | }
58 | });
59 | }
60 |
61 | /**
62 | * Returns a task engine instance (singleton).
63 | *
64 | * @return a task engine.
65 | */
66 | public static synchronized TaskEngine getInstance() {
67 | if (instance == null) {
68 | instance = new TaskEngine();
69 | }
70 | return instance;
71 | }
72 |
73 | /**
74 | * Submits a Runnable task for execution and returns a Future representing
75 | * that task.
76 | *
77 | * @param task
78 | * the task to submit.
79 | * @return a Future representing pending completion of the task, and whose
80 | * get() method will return null upon completion.
81 | * @throws java.util.concurrent.RejectedExecutionException
82 | * if task cannot be scheduled for execution.
83 | * @throws NullPointerException
84 | * if task null.
85 | */
86 | public Future> submit(Runnable task) {
87 | return executor.submit(task);
88 | }
89 |
90 | /**
91 | * Schedules the specified task for execution after the specified delay.
92 | *
93 | * @param task
94 | * task to be scheduled.
95 | * @param delay
96 | * delay in milliseconds before task is to be executed.
97 | * @throws IllegalArgumentException
98 | * if delay is negative, or
99 | * delay + System.currentTimeMillis() is negative.
100 | * @throws IllegalStateException
101 | * if task was already scheduled or cancelled, or timer was
102 | * cancelled.
103 | */
104 | public void schedule(TimerTask task, long delay) {
105 | timer.schedule(new TimerTaskWrapper(task), delay);
106 | }
107 |
108 | /**
109 | * Schedules the specified task for execution at the specified time. If the
110 | * time is in the past, the task is scheduled for immediate execution.
111 | *
112 | * @param task
113 | * task to be scheduled.
114 | * @param time
115 | * time at which task is to be executed.
116 | * @throws IllegalArgumentException
117 | * if time.getTime() is negative.
118 | * @throws IllegalStateException
119 | * if task was already scheduled or cancelled, timer was
120 | * cancelled, or timer thread terminated.
121 | */
122 | public void schedule(TimerTask task, Date time) {
123 | timer.schedule(new TimerTaskWrapper(task), time);
124 | }
125 |
126 | /**
127 | * Schedules the specified task for repeated fixed-delay execution,
128 | * beginning after the specified delay. Subsequent executions take place at
129 | * approximately regular intervals separated by the specified period.
130 | *
131 | *
132 | * In fixed-delay execution, each execution is scheduled relative to the
133 | * actual execution time of the previous execution. If an execution is
134 | * delayed for any reason (such as garbage collection or other background
135 | * activity), subsequent executions will be delayed as well. In the long
136 | * run, the frequency of execution will generally be slightly lower than the
137 | * reciprocal of the specified period (assuming the system clock underlying
138 | * Object.wait(long) is accurate).
139 | *
140 | *
141 | * Fixed-delay execution is appropriate for recurring activities that
142 | * require "smoothness." In other words, it is appropriate for activities
143 | * where it is more important to keep the frequency accurate in the short
144 | * run than in the long run. This includes most animation tasks, such as
145 | * blinking a cursor at regular intervals. It also includes tasks wherein
146 | * regular activity is performed in response to human input, such as
147 | * automatically repeating a character as long as a key is held down.
148 | *
149 | * @param task
150 | * task to be scheduled.
151 | * @param delay
152 | * delay in milliseconds before task is to be executed.
153 | * @param period
154 | * time in milliseconds between successive task executions.
155 | * @throws IllegalArgumentException
156 | * if delay is negative, or
157 | * delay + System.currentTimeMillis() is negative.
158 | * @throws IllegalStateException
159 | * if task was already scheduled or cancelled, timer was
160 | * cancelled, or timer thread terminated.
161 | */
162 | public void schedule(TimerTask task, long delay, long period) {
163 | TimerTaskWrapper taskWrapper = new TimerTaskWrapper(task);
164 | wrappedTasks.put(task, taskWrapper);
165 | timer.schedule(taskWrapper, delay, period);
166 | }
167 |
168 | /**
169 | * Schedules the specified task for repeated fixed-delay execution,
170 | * beginning at the specified time. Subsequent executions take place at
171 | * approximately regular intervals, separated by the specified period.
172 | *
173 | *
174 | * In fixed-delay execution, each execution is scheduled relative to the
175 | * actual execution time of the previous execution. If an execution is
176 | * delayed for any reason (such as garbage collection or other background
177 | * activity), subsequent executions will be delayed as well. In the long
178 | * run, the frequency of execution will generally be slightly lower than the
179 | * reciprocal of the specified period (assuming the system clock underlying
180 | * Object.wait(long) is accurate).
181 | *
182 | *
183 | * Fixed-delay execution is appropriate for recurring activities that
184 | * require "smoothness." In other words, it is appropriate for activities
185 | * where it is more important to keep the frequency accurate in the short
186 | * run than in the long run. This includes most animation tasks, such as
187 | * blinking a cursor at regular intervals. It also includes tasks wherein
188 | * regular activity is performed in response to human input, such as
189 | * automatically repeating a character as long as a key is held down.
190 | *
191 | * @param task
192 | * task to be scheduled.
193 | * @param firstTime
194 | * First time at which task is to be executed.
195 | * @param period
196 | * time in milliseconds between successive task executions.
197 | * @throws IllegalArgumentException
198 | * if time.getTime() is negative.
199 | * @throws IllegalStateException
200 | * if task was already scheduled or cancelled, timer was
201 | * cancelled, or timer thread terminated.
202 | */
203 | public void schedule(TimerTask task, Date firstTime, long period) {
204 | TimerTaskWrapper taskWrapper = new TimerTaskWrapper(task);
205 | wrappedTasks.put(task, taskWrapper);
206 | timer.schedule(taskWrapper, firstTime, period);
207 | }
208 |
209 | /**
210 | * Schedules the specified task for repeated fixed-rate execution,
211 | * beginning after the specified delay. Subsequent executions take place at
212 | * approximately regular intervals, separated by the specified period.
213 | *
214 | *
215 | * In fixed-rate execution, each execution is scheduled relative to the
216 | * scheduled execution time of the initial execution. If an execution is
217 | * delayed for any reason (such as garbage collection or other background
218 | * activity), two or more executions will occur in rapid succession to
219 | * "catch up." In the long run, the frequency of execution will be exactly
220 | * the reciprocal of the specified period (assuming the system clock
221 | * underlying Object.wait(long) is accurate).
222 | *
223 | *
224 | * Fixed-rate execution is appropriate for recurring activities that are
225 | * sensitive to absolute time, such as ringing a chime every hour on
226 | * the hour, or running scheduled maintenance every day at a particular
227 | * time. It is also appropriate for recurring activities where the total
228 | * time to perform a fixed number of executions is important, such as a
229 | * countdown timer that ticks once every second for ten seconds. Finally,
230 | * fixed-rate execution is appropriate for scheduling multiple repeating
231 | * timer tasks that must remain synchronized with respect to one another.
232 | *
233 | * @param task
234 | * task to be scheduled.
235 | * @param delay
236 | * delay in milliseconds before task is to be executed.
237 | * @param period
238 | * time in milliseconds between successive task executions.
239 | * @throws IllegalArgumentException
240 | * if delay is negative, or
241 | * delay + System.currentTimeMillis() is negative.
242 | * @throws IllegalStateException
243 | * if task was already scheduled or cancelled, timer was
244 | * cancelled, or timer thread terminated.
245 | */
246 | public void scheduleAtFixedRate(TimerTask task, long delay, long period) {
247 | TimerTaskWrapper taskWrapper = new TimerTaskWrapper(task);
248 | wrappedTasks.put(task, taskWrapper);
249 | timer.scheduleAtFixedRate(taskWrapper, delay, period);
250 | }
251 |
252 | /**
253 | * Schedules the specified task for repeated fixed-rate execution,
254 | * beginning at the specified time. Subsequent executions take place at
255 | * approximately regular intervals, separated by the specified period.
256 | *
257 | *
258 | * In fixed-rate execution, each execution is scheduled relative to the
259 | * scheduled execution time of the initial execution. If an execution is
260 | * delayed for any reason (such as garbage collection or other background
261 | * activity), two or more executions will occur in rapid succession to
262 | * "catch up." In the long run, the frequency of execution will be exactly
263 | * the reciprocal of the specified period (assuming the system clock
264 | * underlying Object.wait(long) is accurate).
265 | *
266 | *
267 | * Fixed-rate execution is appropriate for recurring activities that are
268 | * sensitive to absolute time, such as ringing a chime every hour on
269 | * the hour, or running scheduled maintenance every day at a particular
270 | * time. It is also appropriate for recurring activities where the total
271 | * time to perform a fixed number of executions is important, such as a
272 | * countdown timer that ticks once every second for ten seconds. Finally,
273 | * fixed-rate execution is appropriate for scheduling multiple repeating
274 | * timer tasks that must remain synchronized with respect to one another.
275 | *
276 | * @param task
277 | * task to be scheduled.
278 | * @param firstTime
279 | * First time at which task is to be executed.
280 | * @param period
281 | * time in milliseconds between successive task executions.
282 | * @throws IllegalArgumentException
283 | * if time.getTime() is negative.
284 | * @throws IllegalStateException
285 | * if task was already scheduled or cancelled, timer was
286 | * cancelled, or timer thread terminated.
287 | */
288 | public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) {
289 | TimerTaskWrapper taskWrapper = new TimerTaskWrapper(task);
290 | wrappedTasks.put(task, taskWrapper);
291 | timer.scheduleAtFixedRate(taskWrapper, firstTime, period);
292 | }
293 |
294 | /**
295 | * Cancels the execution of a scheduled task.
296 | * {@link java.util.TimerTask#cancel()}
297 | *
298 | * @param task
299 | * the scheduled task to cancel.
300 | */
301 | public void cancelScheduledTask(TimerTask task) {
302 | TaskEngine.TimerTaskWrapper taskWrapper = wrappedTasks.remove(task);
303 | if (taskWrapper != null) {
304 | taskWrapper.cancel();
305 | }
306 | }
307 |
308 | /**
309 | * Shuts down the task engine service.
310 | */
311 | public void shutdown() {
312 | if (executor != null) {
313 | executor.shutdown();
314 | executor = null;
315 | }
316 |
317 | if (timer != null) {
318 | timer.cancel();
319 | timer = null;
320 | }
321 |
322 | instance = null;
323 | }
324 |
325 | /**
326 | * Wrapper class for a standard TimerTask. It simply executes the TimerTask
327 | * using the executor's thread pool.
328 | */
329 | private class TimerTaskWrapper extends TimerTask {
330 |
331 | private TimerTask task;
332 |
333 | public TimerTaskWrapper(TimerTask task) {
334 | this.task = task;
335 | }
336 |
337 | @Override
338 | public void run() {
339 | executor.submit(task);
340 | }
341 | }
342 | }
--------------------------------------------------------------------------------
/src/main/java/com/wmz7year/thrift/pool/ThriftConnectionHandle.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Jiang Wei
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 |
17 | package com.wmz7year.thrift.pool;
18 |
19 | import java.io.IOException;
20 | import java.io.Serializable;
21 | import java.util.Map;
22 | import java.util.concurrent.TimeUnit;
23 | import java.util.concurrent.atomic.AtomicBoolean;
24 |
25 | import org.apache.thrift.TServiceClient;
26 | import org.slf4j.Logger;
27 | import org.slf4j.LoggerFactory;
28 |
29 | import com.wmz7year.thrift.pool.config.ThriftServerInfo;
30 | import com.wmz7year.thrift.pool.connection.MulitServiceThriftConnecion;
31 | import com.wmz7year.thrift.pool.connection.ThriftConnection;
32 | import com.wmz7year.thrift.pool.exception.ThriftConnectionPoolException;
33 |
34 | /**
35 | * thrift连接代理类
36 | *
37 | *
38 | * @author jiangwei (ydswcy513@gmail.com)
39 | * @version V1.0
40 | */
41 | public class ThriftConnectionHandle implements ThriftConnection, Serializable {
42 | protected static Logger logger = LoggerFactory.getLogger(ThriftConnectionHandle.class);
43 | private static final long serialVersionUID = 8927450495285911268L;
44 |
45 | /**
46 | * 连接所在的分区
47 | */
48 | private ThriftConnectionPartition thriftConnectionPartition;
49 | /**
50 | * thrift服务器信息
51 | */
52 | private ThriftServerInfo thriftServerInfo;
53 | /**
54 | * 连接超时时间
55 | */
56 | private long connectionTimeout;
57 | /**
58 | * 连接最大存活时间
59 | */
60 | protected long maxConnectionAgeInMs;
61 | /**
62 | * 连接代理类持有的真实连接对象
63 | */
64 | private ThriftConnection thriftConnection;
65 |
66 | /** 是否调用关闭连接方法的表识位 */
67 | protected AtomicBoolean logicallyClosed = new AtomicBoolean();
68 |
69 | /**
70 | * 连接池对象
71 | */
72 | private ThriftConnectionPool thriftConnectionPool;
73 |
74 | /** 连接最后使用时间 */
75 | private long connectionLastUsedInMs;
76 | /** 连接最后重置时间 */
77 | private long connectionLastResetInMs;
78 | /** 连接创建时间 */
79 | protected long connectionCreationTimeInMs;
80 | /** 连接是否损坏的标识位 */
81 | protected boolean possiblyBroken;
82 | /** 使用该连接的线程 */
83 | protected Thread threadUsingConnection;
84 | /** 连接是否出现问题需要关闭的标识位 */
85 | private boolean available = true;
86 |
87 | /**
88 | * 连接监控线程
89 | */
90 | private volatile Thread threadWatch;
91 |
92 | public ThriftConnectionHandle(ThriftConnection thriftConnection,
93 | ThriftConnectionPartition thriftConnectionPartition, ThriftConnectionPool thriftConnectionPool,
94 | boolean recreating) throws ThriftConnectionPoolException {
95 | // 判断是否是新连接
96 | boolean newConnection = thriftConnection == null;
97 |
98 | if (!recreating) {
99 | connectionLastUsedInMs = System.currentTimeMillis();
100 | connectionLastResetInMs = System.currentTimeMillis();
101 | connectionCreationTimeInMs = System.currentTimeMillis();
102 | }
103 |
104 | this.thriftConnectionPool = thriftConnectionPool;
105 | this.thriftConnectionPartition = thriftConnectionPartition;
106 | this.thriftServerInfo = thriftConnectionPartition.getThriftServerInfo();
107 | this.connectionTimeout = thriftConnectionPool.getConfig().getConnectTimeout();
108 | this.connectionTimeout = thriftConnectionPool.getConfig().getMaxConnectionAge(TimeUnit.MILLISECONDS);
109 |
110 | this.maxConnectionAgeInMs = thriftConnectionPool.getConfig().getMaxConnectionAge(TimeUnit.MILLISECONDS);
111 |
112 | try {
113 | this.thriftConnection = newConnection ? thriftConnectionPool.obtainInternalConnection(this)
114 | : thriftConnection;
115 | } catch (ThriftConnectionPoolException e) {
116 | possiblyBroken = true;
117 | throw e;
118 | }
119 | }
120 |
121 | /*
122 | * @see java.io.Closeable#close()
123 | */
124 | @Override
125 | public void close() throws IOException {
126 | try {
127 | if (this.logicallyClosed.compareAndSet(false, true) && available) {
128 |
129 | // 中断连接监视线程
130 | if (this.threadWatch != null) {
131 | this.threadWatch.interrupt();
132 | this.threadWatch = null;
133 | }
134 |
135 | ThriftConnectionHandle handle = null;
136 | try {
137 | handle = this.recreateConnectionHandle();
138 | this.thriftConnectionPool.connectionStrategy.cleanupConnection(this, handle);
139 | this.thriftConnectionPool.releaseConnection(handle);
140 | } catch (ThriftConnectionPoolException e) {
141 | possiblyBroken = true;
142 | // 检查连接是否关闭了
143 | if (!isClosed()) {
144 | this.thriftConnectionPool.connectionStrategy.cleanupConnection(this, handle);
145 | this.thriftConnectionPool.releaseConnection(this);
146 | }
147 | throw new IOException(e);
148 | }
149 | } else {
150 | this.thriftConnectionPool.postDestroyConnection(this);
151 | }
152 | } catch (Exception e) {
153 | possiblyBroken = true;
154 | throw new IOException(e);
155 | }
156 | if (thriftConnection != null) {
157 | thriftConnection.close();
158 | }
159 | }
160 |
161 | /**
162 | * 创建新的连接代理对象
163 | * 用于清理可能存在的奇奇怪怪配置
164 | *
165 | * @return ConnectionHandle 新创建的连接对象
166 | * @throws ThriftConnectionPoolException
167 | * 当产生错误时抛出该异常
168 | */
169 | public ThriftConnectionHandle recreateConnectionHandle() throws ThriftConnectionPoolException {
170 | ThriftConnectionHandle handle = new ThriftConnectionHandle<>(this.thriftConnection,
171 | this.thriftConnectionPartition, this.thriftConnectionPool, true);
172 | handle.thriftConnectionPartition = this.thriftConnectionPartition;
173 | handle.connectionCreationTimeInMs = this.connectionCreationTimeInMs;
174 | handle.connectionLastResetInMs = this.connectionLastResetInMs;
175 | handle.connectionLastUsedInMs = this.connectionLastUsedInMs;
176 | handle.possiblyBroken = this.possiblyBroken;
177 | this.thriftConnection = null;
178 |
179 | return handle;
180 | }
181 |
182 | /*
183 | * @see com.wmz7year.thrift.pool.connection.ThriftConnection#getClient()
184 | */
185 | @Override
186 | public T getClient() {
187 | if (thriftConnection != null) {
188 | return thriftConnection.getClient();
189 | }
190 | throw new IllegalStateException("连接代理类没有绑定的原始连接信息");
191 | }
192 |
193 | /*
194 | * @see
195 | * com.wmz7year.thrift.pool.connection.ThriftConnection#getClient(java.lang.
196 | * String, java.lang.Class)
197 | */
198 | @Override
199 | public K getClient(String serviceName, Class clazz) {
200 | if (thriftConnection != null) {
201 | return thriftConnection.getClient(serviceName, clazz);
202 | }
203 | throw new IllegalStateException("连接代理类没有绑定的原始连接信息");
204 | }
205 |
206 | /**
207 | * 获取连接代理对象绑定的原始连接的方法
208 | *
209 | * @return 原始连接对象
210 | */
211 | public ThriftConnection getInternalConnection() {
212 | return this.thriftConnection;
213 | }
214 |
215 | /**
216 | * 设置代理对象绑定的原始连接的方法
217 | *
218 | * @param thriftConnection
219 | * 原始连接对象
220 | */
221 | public void setInternalConnection(ThriftConnection thriftConnection) {
222 | this.thriftConnection = thriftConnection;
223 | }
224 |
225 | /**
226 | * 获取连接代理对象所对应的thrift服务器信息对象
227 | *
228 | * @return thrift服务器信息对象
229 | */
230 | public ThriftServerInfo getThriftServerInfo() {
231 | return this.thriftServerInfo;
232 | }
233 |
234 | /**
235 | * 获取连接代理类所在的分区对象
236 | *
237 | * @return 分区对象
238 | */
239 | public ThriftConnectionPartition getConnectionPartition() {
240 | return this.thriftConnectionPartition;
241 | }
242 |
243 | /**
244 | * 设置连接代理类所属的连接分区的方法
245 | *
246 | * @param thriftConnectionPartition
247 | * 连接分区对象
248 | */
249 | public void setOriginatingPartition(ThriftConnectionPartition thriftConnectionPartition) {
250 | this.thriftConnectionPartition = thriftConnectionPartition;
251 | }
252 |
253 | /**
254 | * 获取连接超时时间的方法
255 | *
256 | * @return 连接超时时间
257 | */
258 | public long getConnectionTimeout() {
259 | return this.connectionTimeout;
260 | }
261 |
262 | /**
263 | * 关闭代理对象中的原始连接的方法
264 | *
265 | * @throws ThriftConnectionPoolException
266 | * 当关闭连接出现问题抛出该异常
267 | */
268 | public void internalClose() throws ThriftConnectionPoolException {
269 | try {
270 | if (this.thriftConnection != null) {
271 | this.thriftConnection.close();
272 | }
273 | logicallyClosed.set(true);
274 | } catch (IOException e) {
275 | throw new ThriftConnectionPoolException(e);
276 | }
277 | }
278 |
279 | /**
280 | * 获取多服务模式下所有thrift客户端的方法
281 | *
282 | * @return 多服务下所有thrift客户端集合
283 | */
284 | public Map getMuiltServiceClients() {
285 | if (this.thriftConnection instanceof MulitServiceThriftConnecion) {
286 | MulitServiceThriftConnecion connection = (MulitServiceThriftConnecion) thriftConnection;
287 | return connection.getMuiltServiceClients();
288 | }
289 | throw new IllegalStateException("单服务运行模式下不允许调用该方法");
290 | }
291 |
292 | /*
293 | * @see com.wmz7year.thrift.pool.connection.ThriftConnection#isClosed()
294 | */
295 | @Override
296 | public boolean isClosed() {
297 | return this.logicallyClosed.get();
298 | }
299 |
300 | /**
301 | * 判断连接是否已经过期的方法
302 | *
303 | * @return true为已过期 false为未过期
304 | */
305 | public boolean isExpired() {
306 | return this.maxConnectionAgeInMs > 0 && isExpired(System.currentTimeMillis());
307 | }
308 |
309 | /**
310 | * 使用指定的时间判断连接是否过期的方法
311 | *
312 | * @param currentTime
313 | * 指定的时间戳
314 | * @return true为已过期 false为未过期
315 | */
316 | protected boolean isExpired(long currentTime) {
317 | return this.maxConnectionAgeInMs > 0
318 | && (currentTime - this.connectionCreationTimeInMs) > this.maxConnectionAgeInMs;
319 | }
320 |
321 | /**
322 | * 判断连接是否出现问题的方法
323 | *
324 | * @return true为发生过异常 false为未发生异常
325 | */
326 | public boolean isPossiblyBroken() {
327 | return this.possiblyBroken;
328 | }
329 |
330 | /**
331 | * 更新最后重置时间的方法
332 | *
333 | * @param connectionLastReset
334 | * 最后的重置时间
335 | */
336 | public void setConnectionLastResetInMs(long connectionLastReset) {
337 | this.connectionLastResetInMs = connectionLastReset;
338 | }
339 |
340 | /**
341 | * 设置连接最后使用的时间
342 | *
343 | * @param connectionLastUsed
344 | * 连接最后使用的时间
345 | */
346 | public void setConnectionLastUsedInMs(long connectionLastUsed) {
347 | this.connectionLastUsedInMs = connectionLastUsed;
348 | }
349 |
350 | /**
351 | * 逻辑上重新打开连接
352 | */
353 | public void renewConnection() {
354 | this.logicallyClosed.set(false);
355 | this.threadUsingConnection = Thread.currentThread();
356 | }
357 |
358 | /**
359 | * 获取连接的创建时间
360 | *
361 | * @return 连接的创建时间
362 | */
363 | public long getConnectionCreationTimeInMs() {
364 | return this.connectionCreationTimeInMs;
365 | }
366 |
367 | /**
368 | * 获取连接最后的使用时间
369 | *
370 | * @return 连接最后的使用时间
371 | */
372 | public long getConnectionLastUsedInMs() {
373 | return this.connectionLastUsedInMs;
374 | }
375 |
376 | /**
377 | * 获取连接最后重置时间的方法
378 | *
379 | * @return 连接最后重置时间
380 | */
381 | public long getConnectionLastResetInMs() {
382 | return this.connectionLastResetInMs;
383 | }
384 |
385 | /*
386 | * @see com.wmz7year.thrift.pool.connection.ThriftConnection#setAvailable(boolean)
387 | */
388 | @Override
389 | public void setAvailable(boolean available) {
390 | this.available = available;
391 | }
392 |
393 | }
394 |
--------------------------------------------------------------------------------
/src/main/java/com/wmz7year/thrift/pool/ThriftConnectionMaxAgeThread.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Jiang Wei
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 |
17 | package com.wmz7year.thrift.pool;
18 |
19 | import java.util.TimerTask;
20 |
21 | import org.apache.thrift.TServiceClient;
22 | import org.slf4j.Logger;
23 | import org.slf4j.LoggerFactory;
24 |
25 | import com.wmz7year.thrift.pool.config.ThriftConnectionPoolConfig.ServiceOrder;
26 |
27 | /**
28 | * thrift连接最大时间检测线程
29 | *
30 | * @author jiangwei (ydswcy513@gmail.com)
31 | * @version V1.0
32 | */
33 | public class ThriftConnectionMaxAgeThread extends TimerTask {
34 | private static final Logger logger = LoggerFactory.getLogger(ThriftConnectionMaxAgeThread.class);
35 |
36 | /**
37 | * 连接分区对象
38 | */
39 | private ThriftConnectionPartition thriftConnectionPartition;
40 | /**
41 | * 连接池对象
42 | */
43 | private ThriftConnectionPool thriftConnectionPool;
44 | /**
45 | * lifo模式
46 | */
47 | private ServiceOrder lifoMode;
48 | /**
49 | * 最大生存时间
50 | */
51 | private long maxConnectionAge;
52 |
53 | public ThriftConnectionMaxAgeThread(ThriftConnectionPartition thriftConnectionPartition,
54 | ThriftConnectionPool thriftConnectionPool, long maxConnectionAge, ServiceOrder lifoMode) {
55 | this.thriftConnectionPartition = thriftConnectionPartition;
56 | this.maxConnectionAge = maxConnectionAge;
57 | this.lifoMode = lifoMode;
58 | this.thriftConnectionPool = thriftConnectionPool;
59 | this.thriftConnectionPartition.registConnectionMaxAgeThread(this);
60 | }
61 |
62 | /*
63 | * @see java.lang.Runnable#run()
64 | */
65 | @Override
66 | public void run() {
67 | ThriftConnectionHandle connection;
68 | long tmp;
69 | long nextCheckInMs = this.maxConnectionAge;
70 |
71 | int partitionSize = this.thriftConnectionPartition.getAvailableConnections();
72 | long currentTime = System.currentTimeMillis();
73 | for (int i = 0; i < partitionSize; i++) {
74 | try {
75 | connection = this.thriftConnectionPartition.poolFreeConnection();
76 |
77 | if (connection != null) {
78 | connection.setOriginatingPartition(this.thriftConnectionPartition);
79 |
80 | tmp = this.maxConnectionAge - (currentTime - connection.getConnectionCreationTimeInMs());
81 |
82 | if (tmp < nextCheckInMs) {
83 | nextCheckInMs = tmp;
84 | }
85 |
86 | // 判断连接是否过期
87 | if (connection.isExpired(currentTime)) {
88 | // 关闭连接
89 | closeConnection(connection);
90 | continue;
91 | }
92 |
93 | if (lifoMode == ServiceOrder.LIFO) {
94 | if (!(connection.getConnectionPartition().getFreeConnections().offer(connection))) {
95 | connection.internalClose();
96 | }
97 | } else {
98 | this.thriftConnectionPool.putConnectionBackInPartition(connection);
99 | }
100 | // 避免cpu使用率过高 进行一段时间休眠
101 | Thread.sleep(20L);
102 | }
103 | } catch (Throwable e) {
104 | logger.error("Connection max age thread exception.", e);
105 | }
106 | }
107 |
108 | }
109 |
110 | /**
111 | * 关闭thrift超时连接的方法
112 | *
113 | * @param connection
114 | * 需要关闭的超时连接
115 | */
116 | protected void closeConnection(ThriftConnectionHandle connection) {
117 | if (connection != null) {
118 | try {
119 | connection.internalClose();
120 | } catch (Throwable t) {
121 | logger.error("Destroy connection exception", t);
122 | } finally {
123 | this.thriftConnectionPool.postDestroyConnection(connection);
124 | }
125 | }
126 | }
127 |
128 | }
129 |
--------------------------------------------------------------------------------
/src/main/java/com/wmz7year/thrift/pool/ThriftConnectionPartition.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright 2015 Jiang Wei
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 |
17 | package com.wmz7year.thrift.pool;
18 |
19 | import java.io.Serializable;
20 | import java.util.concurrent.ArrayBlockingQueue;
21 | import java.util.concurrent.BlockingQueue;
22 | import java.util.concurrent.TimeUnit;
23 | import java.util.concurrent.atomic.AtomicBoolean;
24 | import java.util.concurrent.locks.ReentrantReadWriteLock;
25 |
26 | import org.apache.thrift.TServiceClient;
27 |
28 | import com.wmz7year.thrift.pool.config.ThriftConnectionPoolConfig;
29 | import com.wmz7year.thrift.pool.config.ThriftServerInfo;
30 | import com.wmz7year.thrift.pool.connection.ThriftConnection;
31 | import com.wmz7year.thrift.pool.exception.ThriftConnectionPoolException;
32 |
33 | /**
34 | * 连接分区实体类
35 | * 每台服务器都意味着一个连接分区
36 | * 不同服务器之间的分区是各自独立的
37 | *
38 | * @author jiangwei (ydswcy513@gmail.com)
39 | * @version V1.0
40 | */
41 | public class ThriftConnectionPartition implements Serializable {
42 | private static final long serialVersionUID = 1575062547601396682L;
43 |
44 | /**
45 | * 空闲连接队列
46 | */
47 | private BlockingQueue> freeConnections;
48 |
49 | /**
50 | * thrift服务器是否关闭的表识位
51 | */
52 | private AtomicBoolean serverIsDown = new AtomicBoolean();
53 |
54 | /**
55 | * 分区所绑定的服务器信息
56 | */
57 | private ThriftServerInfo thriftServerInfo;
58 |
59 | /**
60 | * 连接创建统计锁
61 | */
62 | protected ReentrantReadWriteLock statsLock = new ReentrantReadWriteLock();
63 |
64 | /**
65 | * 创建的连接数量
66 | */
67 | private int createdConnections = 0;
68 | /**
69 | * 分区支持的最大连接数
70 | */
71 | private final int maxConnections;
72 | /**
73 | * 分区支持的最小连接数
74 | */
75 | private final int minConnections;
76 | /**
77 | * 每批连接创建的数量
78 | */
79 | private final int acquireIncrement;
80 | /**
81 | * 连接检测操作信号处理队列
82 | */
83 | private BlockingQueue