修改连接逻辑

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

View File

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

View File

@ -4,9 +4,14 @@ public class ObjectInPool<T> {
private T object;
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.pool = pool;
this.key = key;
}
public T getObjectForCheck() {
@ -25,4 +30,9 @@ public class ObjectInPool<T> {
public long getLasUseAt() {
return lasUseAt;
}
public void setError(){
this.pool.remove( this.key );
}
}

View File

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