# Conflicts:
#	pom.xml
#	src/main/java/com/sunyard/SydApiException.java
#	src/main/java/racal/sunyard/main/SydApi4j.java
#	src/test/java/com/sunyard/sydapi/test/TestAllMethods.java
This commit is contained in:
cheney
2022-10-26 17:24:39 +08:00
parent d0ea067870
commit 24161e2ad5
29 changed files with 1806 additions and 3 deletions
@@ -0,0 +1,63 @@
package com.sunyard.pool;
/**
* 到加密机的链接信息
*/
public class HsmLinkInfo {
private String pcIp;
private int iPort;
private int iConnectTimeOut;
private int iDealTimeOut;
public HsmLinkInfo(String pcIp, int iPort, int iConnectTimeOut, int iDealTimeOut) {
this.pcIp = pcIp;
this.iPort = iPort;
this.iConnectTimeOut = iConnectTimeOut;
this.iDealTimeOut = iDealTimeOut;
}
public static HsmLinkInfo[] mutiHsmLinkInfo(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int iAllDealTimeOut) {
if (null == pcIpList &&
null == iPortList &&
pcIpList.length != iPortList.length) {
throw new IllegalArgumentException("入参错误");
}
if ( iAllDealTimeOut < pcIpList.length) {
throw new IllegalArgumentException("入参错误");
}
// 交易时间进行处理
int iDealTimeOut = iAllDealTimeOut / pcIpList.length;
HsmLinkInfo[]hsms = new HsmLinkInfo[ pcIpList.length ];
for ( int i = 0 ; i < pcIpList.length ; i++ ) {
hsms[ i ] = new HsmLinkInfo( pcIpList[i], iPortList[i], iConnectTimeOut, iDealTimeOut );
}
return hsms;
}
public String getPcIp() {
return pcIp;
}
public int getiPort() {
return iPort;
}
public int getiConnectTimeOut() {
return iConnectTimeOut;
}
public int getiDealTimeOut() {
return iDealTimeOut;
}
}
@@ -0,0 +1,28 @@
package com.sunyard.pool;
public class ObjectInPool<T> {
private T object;
private long lasUseAt = System.currentTimeMillis();
public ObjectInPool(T object) {
this.object = object;
}
public T getObjectForCheck() {
return this.object;
}
public void updateLastUseTime(){
this.lasUseAt = System.currentTimeMillis();
}
public T getObject() {
this.lasUseAt = System.currentTimeMillis();
return this.object;
}
public long getLasUseAt() {
return this.lasUseAt;
}
}
@@ -0,0 +1,26 @@
package com.sunyard.pool;
public class PoolConfig {
// 最大空闲时间
private long maxIdle = 5 * 60 *1000; // 5 分钟
// 最大无用时间
private long maxUseless = 30 * 60 * 1000; // 30 分钟
public long getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(long maxIdle) {
this.maxIdle = maxIdle;
}
public long getMaxUseless() {
return maxUseless;
}
public void setMaxUseless(long maxUseless) {
this.maxUseless = maxUseless;
}
}
@@ -0,0 +1,84 @@
package com.sunyard.pool;
import com.sunyard.SydApi;
import com.sunyard.SydApiException;
import com.sunyard.proxy.ProxyInvocationHandler;
import racal.sunyard.main.SydApi4j;
public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
private HsmLinkInfo[] linkInfos;
public SydApiPool(HsmLinkInfo[] linkInfos) {
this.linkInfos = linkInfos;
}
protected SydApi initialValue() {
SydApi4j[] apis = new SydApi4j[this.linkInfos.length];
boolean connected = false;
for(int i = 0; i < apis.length; ++i) {
HsmLinkInfo info = this.linkInfos[i];
try {
apis[i] = (SydApi4j)(new SydApi4j()).connect(info.getPcIp(), info.getiPort(), (String)null, info.getiConnectTimeOut(), info.getiDealTimeOut());
connected = true;
} catch (Exception var6) {
}
}
if (!connected) {
throw new SydApiException("连接错误", -1);
} else {
ProxyInvocationHandler proxyInvocationHandler = new ProxyInvocationHandler(apis, this.linkInfos, false);
return proxyInvocationHandler.getProxy();
}
}
public boolean check(SydApi s) {
boolean ret = super.check(s);
if (!ret) {
return false;
} else {
ProxyInvocationHandler o = (ProxyInvocationHandler)s.getBindObject();
try {
SydApi4j[] hsms = o.getApis();
boolean canUse = false;
int length = hsms.length;
for(int i = 0; i < length; ++i) {
if (null != hsms[i]) {
try {
synchronized(hsms[i]) {
hsms[i].GenerateRandom(1);
canUse = true;
}
} catch (Exception var12) {
hsms[i] = null;
}
} else {
HsmLinkInfo[] infos = o.getApisInfos();
HsmLinkInfo info = infos[i];
try {
hsms[i] = (SydApi4j)(new SydApi4j()).connect(info.getPcIp(), info.getiPort(), (String)null, info.getiConnectTimeOut(), info.getiDealTimeOut());
canUse = true;
} catch (Exception e) {
}
}
}
return canUse;
} catch (Exception e) {
return false;
}
}
}
protected void free(SydApi o) {
if (null != o) {
o.disconnect();
}
}
}
@@ -0,0 +1,125 @@
package com.sunyard.pool;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ThreadBasedConnectPool<T> {
// ThreadLocal
private final Map<Thread, ObjectInPool<T>> map = new HashMap<>();
// 守护
private final ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(1);
// 配置
private PoolConfig config;
public ThreadBasedConnectPool(PoolConfig config) {
this.config = config;
threadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// 检测和回收逻辑
Iterator<Map.Entry<Thread, ObjectInPool<T>>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Thread, ObjectInPool<T>> entry = it.next();
ObjectInPool<T> oip = entry.getValue();
if (System.currentTimeMillis() - oip.getLasUseAt() < config.getMaxIdle()) {
// 空闲时间不足,免检。
continue;
}
if (System.currentTimeMillis() - oip.getLasUseAt() > config.getMaxUseless()) {
// 长时间未使用,回收链接
it.remove();
try {
free(oip.getObject());
} catch (Exception e) {
}
continue;
}
boolean checkPass = false;
try {
checkPass = check(entry.getValue().getObjectForCheck());
} catch (Exception e) {
checkPass = false;
}
finally {
}
if ( ! checkPass) {
// 检查未通过
it.remove();
try {
free(oip.getObject());
} catch (Exception e) {
}
continue;
}
}
}
}, config.getMaxIdle(), config.getMaxIdle(), TimeUnit.MILLISECONDS);
}
public ThreadBasedConnectPool() {
this(new PoolConfig());
}
// 继承 初始化
protected T initialValue() {
return null;
}
// 保活接口
protected boolean check(T o) {
return null != o;
}
// 释放
protected void free(T o) {
}
// 获取
public T get() {
ObjectInPool<T> o = map.get(Thread.currentThread());
if (null != o) {
return o.getObject();
}
return setInitialValue();
}
// 设置
public void set(T o) {
map.put(Thread.currentThread(), new ObjectInPool<>(o));
}
// 清除
public void remove() {
map.remove(Thread.currentThread());
}
protected void afterInit(ObjectInPool oip, T value ){
}
private T setInitialValue() {
T value = initialValue();
ObjectInPool opi = new ObjectInPool<>(value);
map.put(Thread.currentThread(), opi);
afterInit( opi , value);
return value;
}
}