连接池
This commit is contained in:
parent
24161e2ad5
commit
fdadf11c93
@ -1,6 +1,6 @@
|
|||||||
package com.sunyard;
|
package com.sunyard;
|
||||||
|
|
||||||
|
import com.sunyard.inf.cryption.*;
|
||||||
import com.sunyard.inf.*;
|
import com.sunyard.inf.*;
|
||||||
import com.sunyard.inf.customize.Syd4DigitalEnvelope;
|
import com.sunyard.inf.customize.Syd4DigitalEnvelope;
|
||||||
import com.sunyard.inf.customize.SydHAApi;
|
import com.sunyard.inf.customize.SydHAApi;
|
||||||
@ -13,7 +13,7 @@ import racal.sunyard.main.SydApiBuilder;
|
|||||||
* 程序调用接口
|
* 程序调用接口
|
||||||
*/
|
*/
|
||||||
public interface SydApi extends
|
public interface SydApi extends
|
||||||
SydRawApi, SydCertApi, SydCryptionApi, SydGenApi,
|
SydRawApi, SydCertApi, SydGenApi, SydSYMApi, SydASYMApi, SydHashApi,
|
||||||
SydSVApi, SydHAApi, SydQNUCApi, SydPledge, SydPINOpt,
|
SydSVApi, SydHAApi, SydQNUCApi, SydPledge, SydPINOpt,
|
||||||
SydManufactureApi, SydZYOption, SydUnionPayApi ,Syd4DigitalEnvelope{
|
SydManufactureApi, SydZYOption, SydUnionPayApi ,Syd4DigitalEnvelope{
|
||||||
|
|
||||||
|
|||||||
@ -126,14 +126,6 @@ public interface SydCertApi {
|
|||||||
RetWrap importCert(byte[] certData, byte[] organizationId);
|
RetWrap importCert(byte[] certData, byte[] organizationId);
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 从加密设备中删除指定 DN 的公钥证书
|
|
||||||
* @param dn:传入的证书
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
boolean deleteCert(String dn);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 从加密设备中删除指定 DN 的公钥证书
|
* 从加密设备中删除指定 DN 的公钥证书
|
||||||
* @param dn:传入的证书
|
* @param dn:传入的证书
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
public class ThreadBasedConnectPool<T> {
|
public class ThreadBasedConnectPool<T> {
|
||||||
|
|
||||||
// ThreadLocal
|
// ThreadLocal
|
||||||
private final Map<Thread, ObjectInPool<T>> map = new HashMap<>();
|
private final Map<Thread, ObjectInPool<T>> map = new HashMap<Thread, ObjectInPool<T>>();
|
||||||
|
|
||||||
// 守护
|
// 守护
|
||||||
private final ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(1);
|
private final ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(1);
|
||||||
@ -18,7 +18,7 @@ public class ThreadBasedConnectPool<T> {
|
|||||||
// 配置
|
// 配置
|
||||||
private PoolConfig config;
|
private PoolConfig config;
|
||||||
|
|
||||||
public ThreadBasedConnectPool(PoolConfig config) {
|
public ThreadBasedConnectPool(final PoolConfig config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
threadPool.scheduleAtFixedRate(new Runnable() {
|
threadPool.scheduleAtFixedRate(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@ -103,7 +103,7 @@ public class ThreadBasedConnectPool<T> {
|
|||||||
|
|
||||||
// 设置
|
// 设置
|
||||||
public void set(T o) {
|
public void set(T o) {
|
||||||
map.put(Thread.currentThread(), new ObjectInPool<>(o));
|
map.put(Thread.currentThread(), new ObjectInPool<T>(o));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清除
|
// 清除
|
||||||
@ -117,7 +117,7 @@ public class ThreadBasedConnectPool<T> {
|
|||||||
|
|
||||||
private T setInitialValue() {
|
private T setInitialValue() {
|
||||||
T value = initialValue();
|
T value = initialValue();
|
||||||
ObjectInPool opi = new ObjectInPool<>(value);
|
ObjectInPool opi = new ObjectInPool<T>(value);
|
||||||
map.put(Thread.currentThread(), opi);
|
map.put(Thread.currentThread(), opi);
|
||||||
afterInit( opi , value);
|
afterInit( opi , value);
|
||||||
return value;
|
return value;
|
||||||
|
|||||||
@ -2,6 +2,8 @@ package com.sunyard.proxy;
|
|||||||
|
|
||||||
import com.sunyard.SydApi;
|
import com.sunyard.SydApi;
|
||||||
import com.sunyard.SydApiException;
|
import com.sunyard.SydApiException;
|
||||||
|
import com.sunyard.log.ILogFactory;
|
||||||
|
import com.sunyard.log.ILogger;
|
||||||
import com.sunyard.pool.HsmLinkInfo;
|
import com.sunyard.pool.HsmLinkInfo;
|
||||||
import racal.sunyard.main.SydApi4j;
|
import racal.sunyard.main.SydApi4j;
|
||||||
import racal.sunyard.main.SydApiMuti;
|
import racal.sunyard.main.SydApiMuti;
|
||||||
@ -144,6 +146,13 @@ public class ProxyInvocationHandler implements InvocationHandler {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
|
} catch ( Exception e ) {
|
||||||
|
ILogFactory factory = SydApi4j.getLogFactory();
|
||||||
|
if ( null != factory ) {
|
||||||
|
ILogger logger = factory.getLogger(ProxyInvocationHandler.class);
|
||||||
|
logger.error( "调用方法 {} 失败", method.getName() );
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -69,6 +69,8 @@ public class SydApi4j implements SydApi {
|
|||||||
private int readTimeout;
|
private int readTimeout;
|
||||||
private int retryWhenNetError = 0;
|
private int retryWhenNetError = 0;
|
||||||
|
|
||||||
|
private Object bindObject = null;
|
||||||
|
|
||||||
//初始化变量
|
//初始化变量
|
||||||
static {
|
static {
|
||||||
|
|
||||||
@ -10473,6 +10475,16 @@ public class SydApi4j implements SydApi {
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getBindObject() {
|
||||||
|
return bindObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBindObject(Object bindObject) {
|
||||||
|
this.bindObject = bindObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String SYMEncrytByKeyIndex(String index, String intPutData) {
|
public String SYMEncrytByKeyIndex(String index, String intPutData) {
|
||||||
|
|||||||
@ -2,11 +2,8 @@ package sample;
|
|||||||
|
|
||||||
import com.sunyard.SydApi;
|
import com.sunyard.SydApi;
|
||||||
import com.sunyard.proto.Util;
|
import com.sunyard.proto.Util;
|
||||||
import org.junit.Assert;
|
|
||||||
import racal.sunyard.main.SydApiPoolBuilder;
|
import racal.sunyard.main.SydApiPoolBuilder;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 集群模式 - 长连接
|
* 集群模式 - 长连接
|
||||||
@ -19,33 +16,35 @@ public class HSMPoolLongLink {
|
|||||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
|
|
||||||
// 建立链接
|
// 建立链接
|
||||||
String[] pcipList = {"172.16.18.59","192.168.0.200"};
|
final String[] pcipList = {"172.16.18.59", "192.168.0.200"};
|
||||||
int[] iportList = {8889,8889};
|
final int[] iportList = {8889, 8889};
|
||||||
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
int tmp = i;
|
int tmp = i;
|
||||||
new Thread(() -> {
|
new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
SydApiPoolBuilder builder = new SydApiPoolBuilder(pcipList, iportList, 10, 1000);
|
SydApiPoolBuilder builder = new SydApiPoolBuilder(pcipList, iportList, 10, 1000);
|
||||||
SydApi api = builder.build();
|
SydApi api = builder.build();
|
||||||
try {
|
try {
|
||||||
for (int j = 0; j < 20; j++) {
|
for (int j = 0; j < 20; j++) {
|
||||||
byte[] data = api.SM3Hash("test".getBytes(StandardCharsets.UTF_8));
|
byte[] data = api.SM3Hash("test".getBytes());
|
||||||
System.out.println("hex=" + Util.bytes2HexString(data));
|
System.out.println("hex=" + Util.bytes2HexString(data));
|
||||||
// boolean sign = api.deleteCert("C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1");
|
// boolean sign = api.deleteCert("C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1");
|
||||||
// System.out.println(sign);
|
// System.out.println(sign);
|
||||||
}
|
}
|
||||||
} catch (Exception e){
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
} finally {
|
||||||
finally {
|
|
||||||
if (null != api) {
|
if (null != api) {
|
||||||
api.disconnect();
|
api.disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, i + "t").start();
|
}
|
||||||
}
|
}, i + "t").start();
|
||||||
System.out.println(1);
|
|
||||||
}
|
}
|
||||||
|
System.out.println(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,9 @@
|
|||||||
package sample;
|
package sample;
|
||||||
|
|
||||||
import com.sunyard.SydApi;
|
import com.sunyard.SydApi;
|
||||||
import com.sunyard.cert.X509;
|
|
||||||
import com.sunyard.proto.Util;
|
import com.sunyard.proto.Util;
|
||||||
import racal.sunyard.main.SydApiClusterBuilder;
|
import racal.sunyard.main.SydApiClusterBuilder;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 集群模式 - 短连接
|
* 集群模式 - 短连接
|
||||||
@ -19,34 +16,37 @@ public class HSMPoolShortLink {
|
|||||||
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
System.setProperty("com.sunyard.sydapi4j.debug", "true");
|
||||||
|
|
||||||
// 建立链接
|
// 建立链接
|
||||||
String[] pcipList = {"172.16.18.59", "192.168.0.200"};
|
final String[] pcipList = {"172.16.18.59", "192.168.0.200"};
|
||||||
int[] iportList = {8889, 8889};
|
final int[] iportList = {8889, 8889};
|
||||||
|
|
||||||
|
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
int tmp = i;
|
int tmp = i;
|
||||||
new Thread(() -> {
|
new Thread(new Runnable() {
|
||||||
SydApi api = SydApiClusterBuilder.SYD_Short_Connect_Ex(
|
@Override
|
||||||
pcipList,
|
public void run() {
|
||||||
iportList,
|
SydApi api = SydApiClusterBuilder.SYD_Short_Connect_Ex(
|
||||||
10,
|
pcipList,
|
||||||
1000);
|
iportList,
|
||||||
try {
|
10,
|
||||||
for (int j = 0; j < 20; j++) {
|
1000);
|
||||||
byte[] data = api.SM3Hash("test".getBytes(StandardCharsets.UTF_8));
|
try {
|
||||||
System.out.println("hex=" + Util.bytes2HexString(data));
|
for (int j = 0; j < 20; j++) {
|
||||||
|
byte[] data = api.SM3Hash("test".getBytes());
|
||||||
|
System.out.println("hex=" + Util.bytes2HexString(data));
|
||||||
// boolean sign = api.deleteCert("C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1");
|
// boolean sign = api.deleteCert("C=CN,O=CFCA OCA1,OU=YCCA,OU=Individual-2,CN=YCCA@黄金交易所@Zhuangj@1");
|
||||||
// System.out.println(sign);
|
// System.out.println(sign);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
if (null != api) {
|
if (null != api) {
|
||||||
api.disconnect();
|
api.disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, i + "t").start();
|
}).start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user