修改连接逻辑

This commit is contained in:
junj2.liang 2022-09-09 17:16:22 +08:00
parent bc886998c7
commit 3ed721b788
4 changed files with 64 additions and 24 deletions

View File

@ -166,6 +166,9 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) {
this.oip.setError();
}
throw le; throw le;
} }
@ -230,7 +233,9 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) {
this.oip.setError();
}
throw le; throw le;
} }
@ -304,6 +309,9 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) {
this.oip.setError();
}
throw le; throw le;
} }
@ -371,6 +379,9 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) {
this.oip.setError();
}
throw le; throw le;
} }
@ -453,7 +464,9 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) {
this.oip.setError();
}
throw le; throw le;
} }
@ -521,6 +534,9 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) {
this.oip.setError();
}
throw le; throw le;
} }
@ -589,6 +605,9 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) {
this.oip.setError();
}
throw le; throw le;
} }
@ -676,6 +695,9 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) {
this.oip.setError();
}
throw le; throw le;
} }
@ -746,6 +768,9 @@ public class SydApiBaseFunction implements SydApi {
} }
} }
} }
if ( ! this.isShortLinkMode ) {
this.oip.setError();
}
throw le; throw le;
} }

View File

@ -11,11 +11,12 @@ import org.apache.logging.log4j.Logger;
import racal.sunyard.main.SydApi4j; import racal.sunyard.main.SydApi4j;
public class SydApiPool extends ThreadBasedConnectPool<SydApi> { public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
private HsmLinkInfo[] linkInfos; private HsmLinkInfo[] linkInfos;
static { static {
LogFactory.createLogger("./logs", 0); LogFactory.createLogger("./logs", 0);
} }
public SydApiPool(HsmLinkInfo[] linkInfos) { public SydApiPool(HsmLinkInfo[] linkInfos) {
super(); super();
this.linkInfos = linkInfos; this.linkInfos = linkInfos;
@ -23,7 +24,7 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
// 继承 初始化 // 继承 初始化
protected SydApi initialValue() { protected SydApi initialValue() {
Logger log = LogFactory.getLogger();
// 所有加密机一次性链接 // 所有加密机一次性链接
SydApi4j[] apis = new SydApi4j[linkInfos.length]; SydApi4j[] apis = new SydApi4j[linkInfos.length];
@ -40,15 +41,12 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
); );
connected = true; connected = true;
} catch (Exception e) { } catch (Exception e) {
Logger log = LogFactory.getLogger();
// 忽略单个错误 // 忽略单个错误
log.error("网络连接错误 {} {} - 忽略", info.getPcIp(), info.getiPort()); log.error("网络连接错误 {} {} - 忽略", info.getPcIp(), info.getiPort());
} }
} }
if (!connected) { if (!connected) { throw new SydApiException("连接错误", -1); }
throw new SydApiException("连接错误", -1);
}
return new SydApiBaseFunction(apis, linkInfos); return new SydApiBaseFunction(apis, linkInfos);
@ -56,7 +54,7 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
// 保活接口 // 保活接口
public boolean check(SydApi s) { public boolean check(SydApi s) {
if( !(s instanceof SydApi4Database) ) { if (!(s instanceof SydApi4Database)) {
return false; return false;
} }
@ -84,7 +82,7 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
try { try {
hsms[i] = (SydApi4j) new SydApi4j().connect(info.getPcIp(), info.getiPort(), null, info.getiConnectTimeOut(), info.getiDealTimeOut()); hsms[i] = (SydApi4j) new SydApi4j().connect(info.getPcIp(), info.getiPort(), null, info.getiConnectTimeOut(), info.getiDealTimeOut());
canUse = true; canUse = true;
} catch ( Exception e ) { } catch (Exception e) {
} }
} }
@ -99,19 +97,19 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
// 释放 // 释放
protected void free(SydApi o) { protected void free(SydApi o) {
if (null != o) { if (null != o) {
if (o instanceof SydApi4Database) { if (o instanceof SydApiBaseFunction) {
((SydApi4Database) o).SYD_Disconnect(); ((SydApiBaseFunction) o).SYD_Disconnect();
} }
} }
} }
protected void afterInit(ObjectInPool oip, SydApi value ){ protected void afterInit(ObjectInPool oip, SydApi value) {
if ( !( value instanceof SydApi4Database ) ) { if (!(value instanceof SydApiBaseFunction)) {
return; return;
} }
SydApi4Database api = (SydApi4Database) value; SydApiBaseFunction api = (SydApiBaseFunction) value;
api.setOip( oip ); api.setOip(oip);
} }
} }

View File

@ -4,9 +4,14 @@ public class ObjectInPool<T> {
private T object; private T object;
private long lasUseAt = System.currentTimeMillis(); private long lasUseAt = System.currentTimeMillis();
private ThreadBasedConnectPool pool;
private Object key;
public ObjectInPool(T object) {
public ObjectInPool(ThreadBasedConnectPool pool, Object key, T object) {
this.object = object; this.object = object;
this.pool = pool;
this.key = key;
} }
public T getObjectForCheck() { public T getObjectForCheck() {
@ -25,4 +30,9 @@ public class ObjectInPool<T> {
public long getLasUseAt() { public long getLasUseAt() {
return lasUseAt; return lasUseAt;
} }
public void setError(){
this.pool.remove( this.key );
}
} }

View File

@ -1,6 +1,8 @@
package com.sunyard.sge.pool; package com.sunyard.sge.pool;
import com.sunyard.sge.database.SydApi;
import com.sunyard.sge.log.LogFactory; import com.sunyard.sge.log.LogFactory;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -12,9 +14,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class ThreadBasedConnectPool<T> { public class ThreadBasedConnectPool<T> {
static {
LogFactory.createLogger("./logs", 0);
}
// ThreadLocal // ThreadLocal
private final Map<Thread, ObjectInPool<T>> map = new HashMap<>(); private final Map<Thread, ObjectInPool<T>> map = new HashMap<>();
@ -23,13 +23,16 @@ public class ThreadBasedConnectPool<T> {
// 配置 // 配置
private PoolConfig config; private PoolConfig config;
static {
LogFactory.createLogger("./logs", 0);
}
public ThreadBasedConnectPool(PoolConfig config) { public ThreadBasedConnectPool(PoolConfig config) {
this.config = config; this.config = config;
Logger log = LogFactory.getLogger();
threadPool.scheduleAtFixedRate(new Runnable() { threadPool.scheduleAtFixedRate(new Runnable() {
@Override @Override
public void run() { public void run() {
Logger log = LogFactory.getLogger();
log.debug("连接池检测进程开始执行"); log.debug("连接池检测进程开始执行");
log.debug("共有链接 {} 个", map.size()); log.debug("共有链接 {} 个", map.size());
@ -129,7 +132,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<>(this, Thread.currentThread(), o));
} }
// 清除 // 清除
@ -137,13 +140,17 @@ public class ThreadBasedConnectPool<T> {
map.remove(Thread.currentThread()); map.remove(Thread.currentThread());
} }
public void remove(Object key) {
map.remove(key);
}
protected void afterInit(ObjectInPool oip, T value) { protected void afterInit(ObjectInPool oip, T value) {
} }
private T setInitialValue() { private T setInitialValue() {
T value = initialValue(); T value = initialValue();
ObjectInPool opi = new ObjectInPool<>(value); ObjectInPool opi = new ObjectInPool<>(this, Thread.currentThread(), value);
map.put(Thread.currentThread(), opi); map.put(Thread.currentThread(), opi);
afterInit(opi, value); afterInit(opi, value);
return value; return value;