Merge remote-tracking branch 'origin/dev-1.1' into dev-1.0
# Conflicts: # src/main/java/com/sunyard/sge/database/pool/SydApiPool.java
This commit is contained in:
commit
f9ab152cf1
@ -2,10 +2,10 @@ package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.proto.Util;
|
||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import com.sunyard.util.SYMUtil;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.codehaus.groovy.util.LockableObject;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -19,7 +19,9 @@ public class SydApi4Database implements SydApi {
|
||||
static {
|
||||
SydApi.SYD_SetLogConfig("./logs/", 0);
|
||||
}
|
||||
private SydApi4j[] hsms = null;
|
||||
|
||||
private SydApi4j[] hsms = null;
|
||||
private HsmLinkInfo[] linkInfos = null;
|
||||
|
||||
private boolean isShortLinkMode = false;
|
||||
|
||||
@ -27,16 +29,19 @@ public class SydApi4Database implements SydApi {
|
||||
isShortLinkMode = true;
|
||||
}
|
||||
|
||||
public SydApi4Database(SydApi4j[] hsms) {
|
||||
synchronized (this){
|
||||
public SydApi4Database(SydApi4j[] hsms, HsmLinkInfo[] linkInfos) {
|
||||
this.hsms = hsms;
|
||||
}
|
||||
this.linkInfos = linkInfos;
|
||||
}
|
||||
|
||||
public SydApi4j[] getHsms() {
|
||||
return hsms;
|
||||
}
|
||||
|
||||
public HsmLinkInfo[] getLinkInfos() {
|
||||
return linkInfos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SydApi SYD_Short_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int allDealTimeOut) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
@ -73,37 +78,34 @@ public class SydApi4Database implements SydApi {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void SYD_Disconnect() {
|
||||
if (null == hsms) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
for (int i = 0; i < hsms.length; i++) {
|
||||
SydApi4j api = hsms[i];
|
||||
if (null != api) {
|
||||
try {
|
||||
api.disconnect();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
hsms = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void SYD_Disconnect_Ex() {
|
||||
|
||||
if (isShortLinkMode) {
|
||||
if (null == hsms) {
|
||||
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) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e){
|
||||
|
||||
}
|
||||
finally {
|
||||
lockableObject.unlock();
|
||||
}
|
||||
SYD_Disconnect();
|
||||
}
|
||||
// 配合链接池,不再需要释放。
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -117,16 +119,15 @@ public class SydApi4Database implements SydApi {
|
||||
}
|
||||
|
||||
SydApiException le = null;
|
||||
//加锁
|
||||
LockableObject lockableObject = new LockableObject();
|
||||
lockableObject.lock();
|
||||
try {
|
||||
for (SydApi4j api : this.hsms) {
|
||||
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (SydApi4j api : this.hsms) {
|
||||
|
||||
if (null == api) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isShortLinkMode) {
|
||||
try {
|
||||
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
|
||||
log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash));
|
||||
@ -139,15 +140,23 @@ public class SydApi4Database implements SydApi {
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
} else {
|
||||
synchronized (api) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -8,9 +8,6 @@ import com.sunyard.sge.pool.ThreadBasedConnectPool;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
||||
static {
|
||||
SydApi.SYD_SetLogConfig("./logs/", 0);
|
||||
@ -27,70 +24,85 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
||||
protected SydApi initialValue() {
|
||||
|
||||
// 所有加密机一次性链接
|
||||
SydApi4j[] apis = new SydApi4j[ linkInfos.length ];
|
||||
SydApi4j[] apis = new SydApi4j[linkInfos.length];
|
||||
|
||||
// 只有所有都不好,才报错。
|
||||
boolean connected = false;
|
||||
|
||||
for ( int i = 0; i < apis.length; i ++ ) {
|
||||
for (int i = 0; i < apis.length; i++) {
|
||||
HsmLinkInfo info = linkInfos[i];
|
||||
try {
|
||||
apis[ i ] = (SydApi4j) new SydApi4j().connect(info.getPcIp(), info.getiPort(),
|
||||
apis[i] = (SydApi4j) new SydApi4j().connect(info.getPcIp(), info.getiPort(),
|
||||
null,
|
||||
info.getiConnectTimeOut(),
|
||||
info.getiDealTimeOut()
|
||||
);
|
||||
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) {
|
||||
// TODO
|
||||
throw new SydApiException("连接错误",-1);
|
||||
throw new SydApiException("连接错误", -1);
|
||||
}
|
||||
|
||||
|
||||
return new SydApi4Database( apis );
|
||||
return new SydApi4Database(apis, linkInfos);
|
||||
}
|
||||
|
||||
// 保活接口
|
||||
public boolean check(SydApi s) {
|
||||
SydApi4Database o=(SydApi4Database)s;
|
||||
Lock l = new ReentrantLock();
|
||||
try {
|
||||
l.lock();
|
||||
SydApi4j[] hsms = o.getHsms();
|
||||
boolean ifBoolean = false;
|
||||
int length = hsms.length;
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (null != hsms[i]) {
|
||||
try {
|
||||
o.SYD_SM3_Hash_ShortData(new byte[8]);
|
||||
ifBoolean = true;
|
||||
} catch (Exception e) {
|
||||
// 忽略
|
||||
hsms[i] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ifBoolean;
|
||||
|
||||
}
|
||||
catch (Exception e){
|
||||
if( !(s instanceof SydApi4Database) ) {
|
||||
return false;
|
||||
}
|
||||
finally {
|
||||
l.unlock();
|
||||
|
||||
SydApi4Database o = (SydApi4Database) s;
|
||||
|
||||
try {
|
||||
SydApi4j[] hsms = o.getHsms();
|
||||
boolean canUse = false;
|
||||
int length = hsms.length;
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (null != hsms[i]) {
|
||||
try {
|
||||
synchronized (hsms[i]) {
|
||||
hsms[i].SYD_SM3_LongData(new byte[8], null, 0);
|
||||
canUse = true;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 忽略
|
||||
hsms[i] = null;
|
||||
}
|
||||
} else {
|
||||
// 尝试恢复连接
|
||||
HsmLinkInfo[] infos = o.getLinkInfos();
|
||||
HsmLinkInfo info = infos[i];
|
||||
try {
|
||||
hsms[i] = (SydApi4j) new SydApi4j().connect(info.getPcIp(), info.getiPort(), 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.SYD_Disconnect_Ex();
|
||||
if (null != o) {
|
||||
if (o instanceof SydApi4Database) {
|
||||
((SydApi4Database) o).SYD_Disconnect();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user