Skip to content

Commit 74b4260

Browse files
committed
优化连接池
1 parent 02e4adf commit 74b4260

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

gRPC-Demo/src/main/java/cn/mrdear/grpc/hellopool/HelloWorldClientPool.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class HelloWorldClientPool {
3737
/**
3838
* 从连接池获取对象
3939
*/
40-
public static HelloWorldClientSingle borrowObject(){
40+
private static HelloWorldClientSingle borrowObject(){
4141
try {
4242
HelloWorldClientSingle clientSingle = objectPool.borrowObject();
4343
System.out.println("总创建线程数"+objectPool.getCreatedCount());
@@ -49,13 +49,6 @@ public static HelloWorldClientSingle borrowObject(){
4949
return createClient();
5050
}
5151

52-
/**
53-
* 还送对象
54-
*/
55-
public static void returnObject(HelloWorldClientSingle clientSingle){
56-
objectPool.returnObject(clientSingle);
57-
}
58-
5952
/**
6053
* 当连接池异常,则主动创建对象
6154
*/
@@ -74,7 +67,7 @@ public static Runnable execute(WorkCallBack<HelloWorldClientSingle> workCallBack
7467
workCallBack.callback(client);
7568
} finally {
7669
/** 将连接对象返回给连接池 */
77-
returnObject(client);
70+
objectPool.returnObject(client);
7871
}
7972
};
8073
}

gRPC-Demo/src/main/java/cn/mrdear/grpc/hellopool/HelloWorldClientTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,4 @@ public static void main(String[] args) {
1313
})).start();
1414
}
1515
}
16-
17-
18-
1916
}

gRPC-Demo/src/main/java/cn/mrdear/grpc/hellopool/HelloWorldFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@ public HelloWorldClientSingle create() throws Exception {
2424
public PooledObject<HelloWorldClientSingle> wrap(HelloWorldClientSingle helloWorldClientSingle) {
2525
return new DefaultPooledObject<>(helloWorldClientSingle);
2626
}
27+
28+
@Override
29+
public void destroyObject(PooledObject<HelloWorldClientSingle> p) throws Exception {
30+
p.getObject().shutdown();
31+
super.destroyObject(p);
32+
}
2733
}

0 commit comments

Comments
 (0)