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,16 +19,18 @@ 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() {
isShortLinkMode = true; isShortLinkMode = true;
} }
public SydApi4Database(SydApi4j[] hsms) { public SydApi4Database(SydApi4j[] hsms) {
synchronized (this){
this.hsms = hsms; this.hsms = hsms;
}
} }
public SydApi4j[] getHsms() { public SydApi4j[] getHsms() {
@ -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,21 +75,31 @@ 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++) {
SydApi4j api = hsms[i];
if (null != api) {
try {
api.disconnect();
} catch (Exception e) {
for (int i = 0; i < hsms.length; i++) { }
SydApi4j api = hsms[i];
if (null != api) {
try {
api.disconnect();
} catch (Exception e) {
} }
} }
} }
catch (Exception e){
}
finally {
lockableObject.unlock();
}
} }
// 配合链接池不再需要释放 // 配合链接池不再需要释放
@ -105,25 +117,37 @@ public class SydApi4Database implements SydApi {
} }
SydApiException le = null; SydApiException le = null;
for (SydApi4j api : this.hsms) { //加锁
LockableObject lockableObject = new LockableObject();
lockableObject.lock();
try {
for (SydApi4j api : this.hsms) {
if (null == api) { if (null == api) {
continue;
}
try {
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
log.debug("api.SYD_SM3_LongData方法返回值{}", Arrays.toString(hash));
return Util.bytes2HexString(hash);
} catch (SydApiException e) {
le = e;
if (e.getRetCode() < 0) {
log.error("HA 重试");
continue; continue;
} }
throw e;
try {
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
log.debug("api.SYD_SM3_LongData方法返回值{}", Arrays.toString(hash));
return Util.bytes2HexString(hash);
} catch (SydApiException e) {
le = e;
if (e.getRetCode() < 0) {
log.error("HA 重试");
continue;
}
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();
l.lock(); try {
SydApi4j[] hsms = o.getHsms(); l.lock();
l.unlock(); SydApi4j[] hsms = o.getHsms();
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) {