check修改

This commit is contained in:
junj2.liang 2022-08-16 09:36:25 +08:00
parent d6003c4d76
commit 1806fa8153
2 changed files with 66 additions and 32 deletions

View File

@ -5,6 +5,7 @@ import com.sunyard.proto.Util;
import com.sunyard.sge.log.LogFactory; import com.sunyard.sge.log.LogFactory;
import com.sunyard.util.SYMUtil; import com.sunyard.util.SYMUtil;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.codehaus.groovy.util.LockableObject;
import racal.sunyard.main.SydApi4j; import racal.sunyard.main.SydApi4j;
import java.util.Arrays; import java.util.Arrays;
@ -18,8 +19,8 @@ public class SydApi4Database implements SydApi {
static { static {
SydApi.SYD_SetLogConfig("./logs/", 0); SydApi.SYD_SetLogConfig("./logs/", 0);
} }
private SydApi4j[] hsms = null; private SydApi4j[] hsms = null;
private boolean isShortLinkMode = false; private boolean isShortLinkMode = false;
public SydApi4Database() { public SydApi4Database() {
@ -27,8 +28,10 @@ public class SydApi4Database implements SydApi {
} }
public SydApi4Database(SydApi4j[] hsms) { public SydApi4Database(SydApi4j[] hsms) {
synchronized (this){
this.hsms = hsms; this.hsms = hsms;
} }
}
public SydApi4j[] getHsms() { public SydApi4j[] getHsms() {
return hsms; return hsms;
@ -44,7 +47,6 @@ public class SydApi4Database implements SydApi {
if (pcIpList.length != iPortList.length) { if (pcIpList.length != iPortList.length) {
throw new IllegalArgumentException("ip与端口数量不一致"); throw new IllegalArgumentException("ip与端口数量不一致");
} }
SydApi4j[] apis = new SydApi4j[pcIpList.length]; SydApi4j[] apis = new SydApi4j[pcIpList.length];
int iDealTimeOut = allDealTimeOut / pcIpList.length; int iDealTimeOut = allDealTimeOut / pcIpList.length;
boolean connected = false; boolean connected = false;
@ -73,11 +75,14 @@ public class SydApi4Database implements SydApi {
@Override @Override
public void SYD_Disconnect_Ex() { public void SYD_Disconnect_Ex() {
if (isShortLinkMode) { if (isShortLinkMode) {
if (null == hsms) { if (null == hsms) {
return; return;
} }
LockableObject lockableObject = new LockableObject();
try {
lockableObject.lock();
for (int i = 0; i < hsms.length; i++) { for (int i = 0; i < hsms.length; i++) {
SydApi4j api = hsms[i]; SydApi4j api = hsms[i];
if (null != api) { if (null != api) {
@ -89,6 +94,13 @@ public class SydApi4Database implements SydApi {
} }
} }
} }
catch (Exception e){
}
finally {
lockableObject.unlock();
}
}
// 配合链接池不再需要释放 // 配合链接池不再需要释放
@ -105,6 +117,10 @@ public class SydApi4Database implements SydApi {
} }
SydApiException le = null; SydApiException le = null;
//加锁
LockableObject lockableObject = new LockableObject();
lockableObject.lock();
try {
for (SydApi4j api : this.hsms) { for (SydApi4j api : this.hsms) {
if (null == api) { if (null == api) {
@ -124,6 +140,14 @@ public class SydApi4Database implements SydApi {
throw e; throw e;
} }
} }
}
catch (Exception e){
}
finally {
lockableObject.unlock();
}
throw le; throw le;
} }

View File

@ -58,28 +58,38 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
} }
// 保活接口 // 保活接口
protected boolean check(SydApi4Database o) { public boolean check(SydApi s) {
// if (null == o) { SydApi4Database o=(SydApi4Database)s;
// return false;
// }
Lock l = new ReentrantLock(); Lock l = new ReentrantLock();
try {
l.lock(); l.lock();
SydApi4j[] hsms = o.getHsms(); SydApi4j[] hsms = o.getHsms();
l.unlock();
boolean ifBoolean = false; boolean ifBoolean = false;
int length = hsms.length; int length = hsms.length;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
if (null != hsms[i]) { if (null != hsms[i]) {
try { try {
l.lock();
o.SYD_SM3_Hash_ShortData(new byte[8]); o.SYD_SM3_Hash_ShortData(new byte[8]);
ifBoolean = true; ifBoolean = true;
} catch (Exception e) { } catch (Exception e) {
// 忽略 // 忽略
hsms[i] = null; hsms[i] = null;
} }
finally {
l.unlock();
}
} }
} }
return ifBoolean; return ifBoolean;
}
catch (Exception e){
return false;
}
finally {
l.unlock();
}
} }
// 释放 // 释放
protected void free(SydApi o) { protected void free(SydApi o) {