Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,21 @@ For SNAPSHOT version
}
implementation group: 'com.github.samyuan1990', name:'FabricJavaPool', version: '0.0.1-SNAPSHOT'
```

## Pool config
Fabric network config file in json format and file path as configNetworkPath.
```
configNetworkPath=./src/test/resources/Networkconfig.json
maxTotal=10
maxIdle=8
minIdle=2
maxWaitMillis=1000
```

## Get Connection
```
ObjectPool<FabricConnection> fabricConnectionPool = new FabricJavaPool(TestUtil.netWorkConfig, TestUtil.getUser(), TestUtil.myChannel);
ObjectPool<FabricConnection> fabricConnectionPool = new FabricJavaPool(TestUtil.getUser(), TestUtil.myChannel);
// By Default it will read pool config from ./resources/FabricJavaPool.properties
try {
FabricConnection fabricConnection = fabricConnectionPool.borrowObject();
FabricConnection fabricConnection2 = fabricConnectionPool.borrowObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,14 @@

public class FabricJavaPool extends GenericObjectPool<FabricConnection> {

public FabricJavaPool(String configNetworkPath, User appUser, String channel, GenericObjectPoolConfig config) {
super(new ChannelPoolFactory(configNetworkPath, appUser, channel), config);
public FabricJavaPool(User appUser, String channel) {
super(new ChannelPoolFactory(new FabricJavaPoolConfig().getConfigNetworkPath(), appUser, channel), new FabricJavaPoolConfig());
}

/*public FabricJavaPool(String configNetworkPath, User appUser, String channel, GenericObjectPoolConfig config, AbandonedConfig abandonedConfig) {
super(new ChannelPoolFactory(configNetworkPath, appUser, channel), config, abandonedConfig);
}*/

public FabricJavaPool(String configNetworkPath, User appUser, String channel) {
super(new ChannelPoolFactory(configNetworkPath, appUser, channel));
public FabricJavaPool(User appUser, String channel, FabricJavaPoolConfig config) {
super(new ChannelPoolFactory(config.getConfigNetworkPath(), appUser, channel), config);
}


private static class ChannelPoolFactory extends BasePooledObjectFactory<FabricConnection> {

private String config_network_path = "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.samyuan1990.FabricJavaPool;

import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public class FabricJavaPoolConfig extends GenericObjectPoolConfig {

private static String configFile = "/FabricJavaPool.properties";

public void setConfigNetworkPath(String configNetworkPath) {
this.configNetworkPath = configNetworkPath;
}

public String getConfigNetworkPath() {
return configNetworkPath;
}

private String configNetworkPath;
private Properties properties;

public FabricJavaPoolConfig(String configNetworkPath) {
super();
this.setConfigNetworkPath(configNetworkPath);
}

public FabricJavaPoolConfig() {
loadConfig(configFile);
}

public void loadConfig(String file) {
try {
InputStream in = this.getClass().getResourceAsStream(file);
Properties properties = new Properties();
properties.load(in);
this.setConfigNetworkPath(properties.getProperty("configNetworkPath"));
this.setMaxTotal(Integer.valueOf(properties.getProperty("maxTotal")).intValue());
this.setMaxIdle(Integer.valueOf(properties.getProperty("maxIdle")).intValue());
this.setMinIdle(Integer.valueOf(properties.getProperty("minIdle")).intValue());
this.setMaxWaitMillis(Integer.valueOf(properties.getProperty("maxWaitMillis")).intValue());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.samyuan1990.FabricJavaPool;

import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;

public class FabricJavaPoolConfigTest {

@Test
public void setConfigNetworkPath() {
FabricJavaPoolConfig fJPC = new FabricJavaPoolConfig("AAA");
Assert.assertEquals("AAA", fJPC.getConfigNetworkPath());
}

@Test
public void loadConfig() {
FabricJavaPoolConfig fJPC = new FabricJavaPoolConfig();
Assert.assertEquals("./src/test/resources/Networkconfig.json", fJPC.getConfigNetworkPath());
Assert.assertEquals(10, fJPC.getMaxTotal());
Assert.assertEquals(8, fJPC.getMaxIdle());
Assert.assertEquals(2, fJPC.getMinIdle());
Assert.assertEquals(1000, fJPC.getMaxWaitMillis());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class FabricJavaPoolTest {

@Test public void testChannelPool() {
ObjectPool<FabricConnection> fabricConnectionPool = new FabricJavaPool(TestUtil.netWorkConfig, TestUtil.getUser(), TestUtil.myChannel);
ObjectPool<FabricConnection> fabricConnectionPool = new FabricJavaPool(TestUtil.getUser(), TestUtil.myChannel);
try {
FabricConnection fabricConnection = fabricConnectionPool.borrowObject();
assertNotEquals("Test borrow item channel not null", fabricConnection, null);
Expand All @@ -38,7 +38,7 @@ public class FabricJavaPoolTest {
}

@Test public void testChannelPoolException() {
ObjectPool<FabricConnection> fabricConnectionPool = new FabricJavaPool("./src/test/resources/Networkconfig.json", null, TestUtil.myChannel);
ObjectPool<FabricConnection> fabricConnectionPool = new FabricJavaPool(null, TestUtil.myChannel);
try {
fabricConnectionPool.borrowObject();
} catch (Exception e) {
Expand All @@ -47,10 +47,10 @@ public class FabricJavaPoolTest {
}

@Test public void testChannelPoolBorrow() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
FabricJavaPoolConfig config = new FabricJavaPoolConfig();
config.setMaxTotal(5);
config.setMaxWaitMillis(1000);
ObjectPool<FabricConnection> fabricConnectionPool = new FabricJavaPool("./src/test/resources/Networkconfig.json", TestUtil.getUser(), TestUtil.myChannel, config);
ObjectPool<FabricConnection> fabricConnectionPool = new FabricJavaPool(TestUtil.getUser(), TestUtil.myChannel, config);
try {
for (int i = 0; i < 5; i++) {
FabricConnection o = fabricConnectionPool.borrowObject();
Expand Down
5 changes: 5 additions & 0 deletions src/test/resources/FabricJavaPool.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
configNetworkPath=./src/test/resources/Networkconfig.json
maxTotal=10
maxIdle=8
minIdle=2
maxWaitMillis=1000