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.SydApiException;
|
||||||
import com.sunyard.proto.Util;
|
import com.sunyard.proto.Util;
|
||||||
|
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||||
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;
|
||||||
@ -19,7 +19,9 @@ 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 HsmLinkInfo[] linkInfos = null;
|
||||||
|
|
||||||
private boolean isShortLinkMode = false;
|
private boolean isShortLinkMode = false;
|
||||||
|
|
||||||
@ -27,16 +29,19 @@ public class SydApi4Database implements SydApi {
|
|||||||
isShortLinkMode = true;
|
isShortLinkMode = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SydApi4Database(SydApi4j[] hsms) {
|
public SydApi4Database(SydApi4j[] hsms, HsmLinkInfo[] linkInfos) {
|
||||||
synchronized (this){
|
|
||||||
this.hsms = hsms;
|
this.hsms = hsms;
|
||||||
}
|
this.linkInfos = linkInfos;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SydApi4j[] getHsms() {
|
public SydApi4j[] getHsms() {
|
||||||
return hsms;
|
return hsms;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HsmLinkInfo[] getLinkInfos() {
|
||||||
|
return linkInfos;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SydApi SYD_Short_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int allDealTimeOut) {
|
public SydApi SYD_Short_Connect_Ex(String[] pcIpList, int[] iPortList, int iConnectTimeOut, int allDealTimeOut) {
|
||||||
Logger log = LogFactory.getLogger();
|
Logger log = LogFactory.getLogger();
|
||||||
@ -73,37 +78,34 @@ public class SydApi4Database implements SydApi {
|
|||||||
return this;
|
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
|
@Override
|
||||||
public void SYD_Disconnect_Ex() {
|
public void SYD_Disconnect_Ex() {
|
||||||
|
|
||||||
if (isShortLinkMode) {
|
if (isShortLinkMode) {
|
||||||
if (null == hsms) {
|
SYD_Disconnect();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// 配合链接池,不再需要释放。
|
// 配合链接池,不再需要释放。
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -117,16 +119,15 @@ public class SydApi4Database implements SydApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SydApiException le = null;
|
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 {
|
try {
|
||||||
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
|
byte[] hash = api.SYD_SM3_LongData(pcData, null, 0);
|
||||||
log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash));
|
log.debug("api.SYD_SM3_LongData方法返回值:{}", Arrays.toString(hash));
|
||||||
@ -139,15 +140,23 @@ public class SydApi4Database implements SydApi {
|
|||||||
}
|
}
|
||||||
throw e;
|
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;
|
throw le;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -8,9 +8,6 @@ import com.sunyard.sge.pool.ThreadBasedConnectPool;
|
|||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import racal.sunyard.main.SydApi4j;
|
import racal.sunyard.main.SydApi4j;
|
||||||
|
|
||||||
import java.util.concurrent.locks.Lock;
|
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
|
||||||
|
|
||||||
public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
||||||
static {
|
static {
|
||||||
SydApi.SYD_SetLogConfig("./logs/", 0);
|
SydApi.SYD_SetLogConfig("./logs/", 0);
|
||||||
@ -27,70 +24,85 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
|||||||
protected SydApi initialValue() {
|
protected SydApi initialValue() {
|
||||||
|
|
||||||
// 所有加密机一次性链接
|
// 所有加密机一次性链接
|
||||||
SydApi4j[] apis = new SydApi4j[ linkInfos.length ];
|
SydApi4j[] apis = new SydApi4j[linkInfos.length];
|
||||||
|
|
||||||
// 只有所有都不好,才报错。
|
// 只有所有都不好,才报错。
|
||||||
boolean connected = false;
|
boolean connected = false;
|
||||||
|
|
||||||
for ( int i = 0; i < apis.length; i ++ ) {
|
for (int i = 0; i < apis.length; i++) {
|
||||||
HsmLinkInfo info = linkInfos[i];
|
HsmLinkInfo info = linkInfos[i];
|
||||||
try {
|
try {
|
||||||
apis[ i ] = (SydApi4j) new SydApi4j().connect(info.getPcIp(), info.getiPort(),
|
apis[i] = (SydApi4j) new SydApi4j().connect(info.getPcIp(), info.getiPort(),
|
||||||
null,
|
null,
|
||||||
info.getiConnectTimeOut(),
|
info.getiConnectTimeOut(),
|
||||||
info.getiDealTimeOut()
|
info.getiDealTimeOut()
|
||||||
);
|
);
|
||||||
connected = true;
|
connected = true;
|
||||||
} catch ( Exception e ) {
|
} catch (Exception e) {
|
||||||
Logger log = LogFactory.getLogger();
|
Logger log = LogFactory.getLogger();
|
||||||
// 忽略单个错误
|
// 忽略单个错误
|
||||||
log.error("网络连接错误 {} {} - 忽略",info.getPcIp(),info.getiPort());
|
log.error("网络连接错误 {} {} - 忽略", info.getPcIp(), info.getiPort());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if( ! connected ) {
|
if (!connected) {
|
||||||
// TODO
|
// TODO
|
||||||
throw new SydApiException("连接错误",-1);
|
throw new SydApiException("连接错误", -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return new SydApi4Database( apis );
|
return new SydApi4Database(apis, linkInfos);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保活接口
|
// 保活接口
|
||||||
public boolean check(SydApi s) {
|
public boolean check(SydApi s) {
|
||||||
SydApi4Database o=(SydApi4Database)s;
|
if( !(s instanceof SydApi4Database) ) {
|
||||||
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){
|
|
||||||
return false;
|
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) {
|
protected void free(SydApi o) {
|
||||||
if ( null != o ) {
|
if (null != o) {
|
||||||
o.SYD_Disconnect_Ex();
|
if (o instanceof SydApi4Database) {
|
||||||
|
((SydApi4Database) o).SYD_Disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user