并行处理
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
public interface ISM3 {
|
||||
public void update(byte[] pcData);
|
||||
public String digest();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
public interface ISM4 {
|
||||
public byte[] update(byte[] pcData);
|
||||
public byte[] finish();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
public interface ISM4Mac {
|
||||
public void update(byte[] pcData);
|
||||
public String disget();
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -28,7 +29,7 @@ public interface SydApi {
|
||||
throw new IllegalArgumentException("地址不能为空且等级范围为0到3");
|
||||
}
|
||||
Logger logger = LogFactory.createLogger(pcLogPath, iLogLevel);
|
||||
// SydApi4j.setLogger(logger);
|
||||
SydApi4j.setLogger(logger);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -271,9 +272,9 @@ public interface SydApi {
|
||||
);
|
||||
|
||||
|
||||
public SydSM4 initSM4(int iKeyIndex, int ende);
|
||||
public ISM4 initSM4(int iKeyIndex, int ende);
|
||||
|
||||
public SydSM3 initSM3();
|
||||
public ISM3 initSM3();
|
||||
|
||||
public SydSM4Mac initSM4Mac(int iKeyIndex);
|
||||
public ISM4Mac initSM4Mac(int iKeyIndex);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||
import com.sunyard.sge.pool.CommWorker;
|
||||
import racal.sunyard.main.SydApi4j;
|
||||
@@ -8,17 +7,14 @@ import racal.sunyard.main.SydApi4j;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
import static com.sunyard.sge.database.SydApiConfig.BATCH_SIZE;
|
||||
|
||||
|
||||
/**
|
||||
* 主要用于短连接
|
||||
*/
|
||||
public class SydApi4Database extends SydApi4Function {
|
||||
private static final int BATCH_SIZE = 7680;
|
||||
// 长连接的所有处理
|
||||
private static final CommWorker longLinkWorks = new CommWorker();
|
||||
public class SydApi4Database extends SydApiBaseFunction {
|
||||
|
||||
public SydApi4Database() {
|
||||
super();
|
||||
@@ -66,30 +62,13 @@ public class SydApi4Database extends SydApi4Function {
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
// 并发处理
|
||||
List<FutureTask<List<byte[]>>> tasks = new ArrayList<>();
|
||||
// 排队处理
|
||||
List<byte[]> ret = new ArrayList<>();
|
||||
for (int i = 0; i < dataGroups.size(); i++) {
|
||||
|
||||
int finalI = i;
|
||||
FutureTask<List<byte[]>> task = longLinkWorks.fTask(new Callable<List<byte[]>>() {
|
||||
@Override
|
||||
public List<byte[]> call() throws Exception {
|
||||
return SydApi4Database.super.SYD_SM4_BatchData(toIntArray(keyGroups.get(finalI)), dataGroups.get(finalI), iFlag);
|
||||
}
|
||||
});
|
||||
tasks.add(task);
|
||||
ret.addAll(super.SYD_SM4_BatchData(toIntArray(keyGroups.get(i)), dataGroups.get(i), iFlag));
|
||||
}
|
||||
|
||||
try {
|
||||
List<byte[]> ret = new ArrayList<>();
|
||||
for (FutureTask<List<byte[]>> t : tasks) {
|
||||
ret.addAll(t.get());
|
||||
}
|
||||
return ret;
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -127,31 +106,13 @@ public class SydApi4Database extends SydApi4Function {
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
// 并发处理
|
||||
List<FutureTask<String[]>> tasks = new ArrayList<>();
|
||||
// 排队处理
|
||||
List<String> ret = new ArrayList<>();
|
||||
for (int i = 0; i < dataGroups.size(); i++) {
|
||||
|
||||
int finalI = i;
|
||||
FutureTask<String[]> task = longLinkWorks.fTask(new Callable<String[]>() {
|
||||
@Override
|
||||
public String[] call() throws Exception {
|
||||
return SydApi4Database.super.SYD_SM4Mac_BatchData(toIntArray(keyGroups.get(finalI)), dataGroups.get(finalI));
|
||||
}
|
||||
});
|
||||
tasks.add(task);
|
||||
}
|
||||
|
||||
try {
|
||||
List<String> ret = new ArrayList<>();
|
||||
for (FutureTask<String[]> t : tasks) {
|
||||
String[] macs = t.get();
|
||||
ret.addAll(Arrays.asList(macs));
|
||||
}
|
||||
return ret.toArray(new String[0]);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
String[] macs = super.SYD_SM4Mac_BatchData(toIntArray(keyGroups.get(i)), dataGroups.get(i));
|
||||
ret.addAll(Arrays.asList(macs));
|
||||
}
|
||||
return ret.toArray(new String[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -196,37 +157,20 @@ public class SydApi4Database extends SydApi4Function {
|
||||
keyGroups.add(curKeyGroup);
|
||||
macGroups.add(curMacGroup);
|
||||
|
||||
// 并发处理
|
||||
List<FutureTask<boolean[]>> tasks = new ArrayList<>();
|
||||
// 排队处理
|
||||
List<Boolean> ret = new ArrayList<>();
|
||||
for (int i = 0; i < dataGroups.size(); i++) {
|
||||
|
||||
int finalI = i;
|
||||
FutureTask<boolean[]> task = longLinkWorks.fTask(new Callable<boolean[]>() {
|
||||
@Override
|
||||
public boolean[] call() throws Exception {
|
||||
return SydApi4Database.super.SYD_SM4Mac_BatchData(
|
||||
toIntArray(keyGroups.get(finalI)),
|
||||
dataGroups.get(finalI),
|
||||
macGroups.get(finalI).toArray(new String[0])
|
||||
);
|
||||
}
|
||||
});
|
||||
tasks.add(task);
|
||||
}
|
||||
|
||||
try {
|
||||
List<Boolean> ret = new ArrayList<>();
|
||||
for (FutureTask<boolean[]> t : tasks) {
|
||||
boolean[] rets = t.get();
|
||||
for (boolean r : rets) {
|
||||
ret.add(r);
|
||||
}
|
||||
boolean[] rets = super.SYD_SM4Mac_BatchData(
|
||||
toIntArray(keyGroups.get(i)),
|
||||
dataGroups.get(i),
|
||||
macGroups.get(i).toArray(new String[0])
|
||||
);
|
||||
for (boolean r : rets) {
|
||||
ret.add(r);
|
||||
}
|
||||
return toBoolArray(ret);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
return toBoolArray(ret);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+4
-5
@@ -15,7 +15,7 @@ import java.util.List;
|
||||
/**
|
||||
* 上海黄金交易所 - 数据库加密方案
|
||||
*/
|
||||
public class SydApi4Function implements SydApi {
|
||||
public class SydApiBaseFunction implements SydApi {
|
||||
|
||||
private SydApi4j[] hsms = null;
|
||||
private HsmLinkInfo[] linkInfos = null;
|
||||
@@ -23,11 +23,11 @@ public class SydApi4Function implements SydApi {
|
||||
|
||||
private boolean isShortLinkMode = false;
|
||||
|
||||
public SydApi4Function() {
|
||||
public SydApiBaseFunction() {
|
||||
isShortLinkMode = true;
|
||||
}
|
||||
|
||||
public SydApi4Function(SydApi4j[] hsms, HsmLinkInfo[] linkInfos) {
|
||||
public SydApiBaseFunction(SydApi4j[] hsms, HsmLinkInfo[] linkInfos) {
|
||||
this.hsms = hsms;
|
||||
this.linkInfos = linkInfos;
|
||||
}
|
||||
@@ -76,7 +76,6 @@ public class SydApi4Function implements SydApi {
|
||||
|
||||
}
|
||||
if (!connected) {
|
||||
// TODO
|
||||
throw new SydApiException("连接错误", -1);
|
||||
}
|
||||
|
||||
@@ -769,7 +768,7 @@ public class SydApi4Function implements SydApi {
|
||||
|
||||
|
||||
@Override
|
||||
public SydSM4 initSM4(int iKeyIndex, int ende) {
|
||||
public ISM4 initSM4(int iKeyIndex, int ende) {
|
||||
|
||||
return new SydSM4(this,iKeyIndex,ende);
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package com.sunyard.sge.database;
|
||||
import com.sunyard.sge.database.pool.HsmLinkInfo;
|
||||
import com.sunyard.sge.database.pool.SydApiPool;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import com.sunyard.sge.pool.CommWorker;
|
||||
import com.sunyard.sge.pool.PoolConfig;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -12,24 +14,59 @@ public class SydApiBuilder {
|
||||
SydApi.SYD_SetLogConfig("./logs/", 0);
|
||||
}
|
||||
private SydApiPool pool;
|
||||
private CommWorker longLinkWorks;
|
||||
|
||||
|
||||
public SydApiBuilder(
|
||||
String[] pcIpList,
|
||||
int[] iPortList,
|
||||
int iConnectTimeOut,
|
||||
int iDealTimeOut
|
||||
) {
|
||||
this( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut, new PoolConfig() );
|
||||
}
|
||||
public SydApiBuilder(
|
||||
String[] pcIpList,
|
||||
int[] iPortList,
|
||||
int iConnectTimeOut,
|
||||
int iDealTimeOut,
|
||||
PoolConfig config
|
||||
) {
|
||||
Logger log = LogFactory.getLogger();
|
||||
log.debug("创建链接");
|
||||
log.debug("SydApiBuilder构造方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList),Arrays.toString(iPortList),iConnectTimeOut,iDealTimeOut);
|
||||
|
||||
if ( log.isDebugEnabled() ) {
|
||||
log.debug("创建链接");
|
||||
log.debug("SydApiBuilder构造方法接收参数:pcIpList:{},iPortList:{},iConnectTimeOut:{},iDealTimeOut:{}", Arrays.toString(pcIpList),Arrays.toString(iPortList),iConnectTimeOut,iDealTimeOut);
|
||||
log.debug("pool config {}", config);
|
||||
}
|
||||
|
||||
HsmLinkInfo[] linkInfo = HsmLinkInfo.mutiHsmLinkInfo(pcIpList, iPortList, iConnectTimeOut, iDealTimeOut);
|
||||
this.pool = new SydApiPool(linkInfo);
|
||||
longLinkWorks = new CommWorker();
|
||||
warmUp( pcIpList, iPortList, iConnectTimeOut, iDealTimeOut );
|
||||
}
|
||||
|
||||
public SydApi build() {
|
||||
public CommWorker getLongLinkWorks() {
|
||||
return longLinkWorks;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取池内对象
|
||||
* @return 池内对象
|
||||
*/
|
||||
public SydApi get(){
|
||||
return pool.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 升级
|
||||
* 修改了原实现
|
||||
* @return 包装对象
|
||||
*/
|
||||
public SydApi build() {
|
||||
return new SydApiLongLinkWrap( this );
|
||||
}
|
||||
|
||||
|
||||
private void warmUp(String[] pcIpList,
|
||||
int[] iPortList,
|
||||
|
||||
@@ -4,6 +4,9 @@ package com.sunyard.sge.database;
|
||||
* */
|
||||
public class SydApiConfig {
|
||||
|
||||
// 批量数据,每组最大长度
|
||||
public static final int BATCH_SIZE = 7680;
|
||||
|
||||
// 数据包长度
|
||||
private final int MaxDataLenEachPack = 4064;
|
||||
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.sge.database.pool.Workers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
import static com.sunyard.sge.database.SydApiBaseFunction.toBoolArray;
|
||||
import static com.sunyard.sge.database.SydApiBaseFunction.toIntArray;
|
||||
import static com.sunyard.sge.database.SydApiConfig.BATCH_SIZE;
|
||||
|
||||
/**
|
||||
* 长连接的真正执行过程转发到长连接线程池中执行。
|
||||
*/
|
||||
@@ -30,10 +35,10 @@ public class SydApiLongLinkWrap implements SydApi {
|
||||
|
||||
@Override
|
||||
public String SYD_SM3_Hash_ShortData(byte[] pcData) {
|
||||
FutureTask<String> task = Workers.getLongLinkWorks().fTask(new Callable<String>() {
|
||||
FutureTask<String> task = builder.getLongLinkWorks().fTask(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return builder.build().SYD_SM3_Hash_ShortData(pcData);
|
||||
return builder.get().SYD_SM3_Hash_ShortData(pcData);
|
||||
}
|
||||
});
|
||||
try {
|
||||
@@ -45,10 +50,10 @@ public class SydApiLongLinkWrap implements SydApi {
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, String> SYD_SM3_Hash_LongData(byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
FutureTask<LongDataReturn<String, String>> task = Workers.getLongLinkWorks().fTask(new Callable<LongDataReturn<String, String>>() {
|
||||
FutureTask<LongDataReturn<String, String>> task = builder.getLongLinkWorks().fTask(new Callable<LongDataReturn<String, String>>() {
|
||||
@Override
|
||||
public LongDataReturn<String, String> call() throws Exception {
|
||||
return builder.build().SYD_SM3_Hash_LongData(pcData,iPkgNum, pcProcData);
|
||||
return builder.get().SYD_SM3_Hash_LongData(pcData, iPkgNum, pcProcData);
|
||||
}
|
||||
});
|
||||
try {
|
||||
@@ -60,12 +65,17 @@ public class SydApiLongLinkWrap implements SydApi {
|
||||
|
||||
@Override
|
||||
public byte[] SYD_SM4_ShortData(int iKeyIndex, byte[] pcData, int iFlag) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
|
||||
return null;
|
||||
FutureTask<byte[]> task = builder.getLongLinkWorks().fTask(new Callable<byte[]>() {
|
||||
@Override
|
||||
public byte[] call() throws Exception {
|
||||
return builder.get().SYD_SM4_ShortData(iKeyIndex, pcData, iFlag);
|
||||
}
|
||||
});
|
||||
try {
|
||||
return task.get();
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,15 +83,6 @@ public class SydApiLongLinkWrap implements SydApi {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, String[] pcMac) {
|
||||
return new boolean[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public LongDataReturn<String, String> SYD_SM4Mac_LongData(int iKeyIndex, byte[] pcData, int iPkgNum, String pcProcData) {
|
||||
@@ -104,17 +105,221 @@ public class SydApiLongLinkWrap implements SydApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SydSM4 initSM4(int iKeyIndex, int ende) {
|
||||
return null;
|
||||
public ISM4 initSM4(int iKeyIndex, int ende) {
|
||||
return new SydSM4(this, iKeyIndex, ende);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SydSM3 initSM3() {
|
||||
return null;
|
||||
public ISM3 initSM3() {
|
||||
return new SydSM3(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SydSM4Mac initSM4Mac(int iKeyIndex) {
|
||||
return null;
|
||||
public ISM4Mac initSM4Mac(int iKeyIndex) {
|
||||
return new SydSM4Mac(this, iKeyIndex);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<byte[]> SYD_SM4_BatchData(int[] iKeyIndex, List<byte[]> pcData, int iFlag) {
|
||||
// 将数据进行分组,防止超长。
|
||||
List<List<byte[]>> dataGroups = new ArrayList<>();
|
||||
List<List<Integer>> keyGroups = new ArrayList<>();
|
||||
int totalLen = 0;
|
||||
|
||||
List<byte[]> curDataGroup = new ArrayList<>();
|
||||
List<Integer> curKeyGroup = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < pcData.size(); i++) {
|
||||
byte[] data = pcData.get(i);
|
||||
if (null == data) {
|
||||
data = new byte[0];
|
||||
}
|
||||
if (
|
||||
curDataGroup.size() > 9 // 分组小于等于 9 个
|
||||
|| (totalLen + data.length > BATCH_SIZE) // 总大小小于 BATCH_SIZE
|
||||
) { // 扩展包
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
curDataGroup = new ArrayList<>();
|
||||
curKeyGroup = new ArrayList<>();
|
||||
}
|
||||
|
||||
|
||||
curDataGroup.add(data);
|
||||
curKeyGroup.add(iKeyIndex[i]);
|
||||
|
||||
totalLen = data.length;
|
||||
|
||||
}
|
||||
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
// 并发处理
|
||||
List<FutureTask<List<byte[]>>> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < dataGroups.size(); i++) {
|
||||
|
||||
int finalI = i;
|
||||
FutureTask<List<byte[]>> task = builder.getLongLinkWorks().fTask(new Callable<List<byte[]>>() {
|
||||
@Override
|
||||
public List<byte[]> call() throws Exception {
|
||||
SydApi api = builder.get();
|
||||
return api.SYD_SM4_BatchData(toIntArray(keyGroups.get(finalI)), dataGroups.get(finalI), iFlag);
|
||||
}
|
||||
});
|
||||
tasks.add(task);
|
||||
}
|
||||
|
||||
try {
|
||||
List<byte[]> ret = new ArrayList<>();
|
||||
for (FutureTask<List<byte[]>> t : tasks) {
|
||||
ret.addAll(t.get());
|
||||
}
|
||||
return ret;
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData) {
|
||||
// 将数据进行分组,防止超长。
|
||||
List<List<byte[]>> dataGroups = new ArrayList<>();
|
||||
List<List<Integer>> keyGroups = new ArrayList<>();
|
||||
int totalLen = 0;
|
||||
|
||||
List<byte[]> curDataGroup = new ArrayList<>();
|
||||
List<Integer> curKeyGroup = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < pcData.size(); i++) {
|
||||
byte[] data = pcData.get(i);
|
||||
if (null == data) {
|
||||
data = new byte[0];
|
||||
}
|
||||
if (
|
||||
curDataGroup.size() > 9 // 分组小于等于 9 个
|
||||
|| (totalLen + data.length > BATCH_SIZE) // 总大小小于 BATCH_SIZE
|
||||
) {// 扩展包
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
curDataGroup = new ArrayList<>();
|
||||
curKeyGroup = new ArrayList<>();
|
||||
}
|
||||
|
||||
curDataGroup.add(data);
|
||||
curKeyGroup.add(iKeyIndex[i]);
|
||||
|
||||
totalLen = data.length;
|
||||
}
|
||||
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
|
||||
// 并发处理
|
||||
List<FutureTask<String[]>> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < dataGroups.size(); i++) {
|
||||
|
||||
int finalI = i;
|
||||
FutureTask<String[]> task = builder.getLongLinkWorks().fTask(new Callable<String[]>() {
|
||||
@Override
|
||||
public String[] call() throws Exception {
|
||||
SydApi api = builder.get();
|
||||
return api.SYD_SM4Mac_BatchData(toIntArray(keyGroups.get(finalI)), dataGroups.get(finalI));
|
||||
}
|
||||
});
|
||||
tasks.add(task);
|
||||
}
|
||||
|
||||
try {
|
||||
List<String> ret = new ArrayList<>();
|
||||
for (FutureTask<String[]> t : tasks) {
|
||||
String[] macs = t.get();
|
||||
ret.addAll(Arrays.asList(macs));
|
||||
}
|
||||
return ret.toArray(new String[0]);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] SYD_SM4Mac_BatchData(int[] iKeyIndex, List<byte[]> pcData, String[] pcMac) {
|
||||
// 将数据进行分组,防止超长。
|
||||
List<List<byte[]>> dataGroups = new ArrayList<>();
|
||||
List<List<Integer>> keyGroups = new ArrayList<>();
|
||||
List<List<String>> macGroups = new ArrayList<>();
|
||||
int totalLen = 0;
|
||||
|
||||
List<byte[]> curDataGroup = new ArrayList<>();
|
||||
List<Integer> curKeyGroup = new ArrayList<>();
|
||||
List<String> curMacGroup = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < pcData.size(); i++) {
|
||||
byte[] data = pcData.get(i);
|
||||
if (null == data) {
|
||||
data = new byte[0];
|
||||
}
|
||||
if (
|
||||
curDataGroup.size() > 9 // 分组小于等于 9 个
|
||||
|| (totalLen + data.length > BATCH_SIZE) // 总大小小于 BATCH_SIZE
|
||||
) {
|
||||
// 扩展包
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
macGroups.add(curMacGroup);
|
||||
|
||||
curDataGroup = new ArrayList<>();
|
||||
curKeyGroup = new ArrayList<>();
|
||||
curMacGroup = new ArrayList<>();
|
||||
}
|
||||
|
||||
|
||||
curDataGroup.add(data);
|
||||
curKeyGroup.add(iKeyIndex[i]);
|
||||
curMacGroup.add(pcMac[i]);
|
||||
totalLen = data.length;
|
||||
}
|
||||
|
||||
dataGroups.add(curDataGroup);
|
||||
keyGroups.add(curKeyGroup);
|
||||
macGroups.add(curMacGroup);
|
||||
|
||||
// 并发处理
|
||||
List<FutureTask<boolean[]>> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < dataGroups.size(); i++) {
|
||||
|
||||
int finalI = i;
|
||||
FutureTask<boolean[]> task = builder.getLongLinkWorks().fTask(new Callable<boolean[]>() {
|
||||
@Override
|
||||
public boolean[] call() throws Exception {
|
||||
SydApi api = builder.get();
|
||||
return api.SYD_SM4Mac_BatchData(
|
||||
toIntArray(keyGroups.get(finalI)),
|
||||
dataGroups.get(finalI),
|
||||
macGroups.get(finalI).toArray(new String[0])
|
||||
);
|
||||
}
|
||||
});
|
||||
tasks.add(task);
|
||||
}
|
||||
|
||||
try {
|
||||
List<Boolean> ret = new ArrayList<>();
|
||||
for (FutureTask<boolean[]> t : tasks) {
|
||||
boolean[] rets = t.get();
|
||||
for (boolean r : rets) {
|
||||
ret.add(r);
|
||||
}
|
||||
}
|
||||
return toBoolArray(ret);
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new SydApiException("并行任务执行失败", 0xC101, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.sunyard.sge.database;
|
||||
|
||||
import com.sunyard.SydApiException;
|
||||
|
||||
public class SydSM3 {
|
||||
public class SydSM3 implements ISM3 {
|
||||
private boolean is_finished = false;
|
||||
private SydApi api;
|
||||
private String pcProcData;
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.sunyard.SydApiException;
|
||||
import com.sunyard.sge.bytes.BytesQueue;
|
||||
|
||||
|
||||
public class SydSM4 {
|
||||
public class SydSM4 implements ISM4 {
|
||||
|
||||
private SydApi api;
|
||||
private int iKeyIndex;
|
||||
@@ -66,9 +66,6 @@ public class SydSM4 {
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (null == queue || null == pcData) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
queue.push(pcData);
|
||||
if ((queue.getLength()) / 16 < 2) {
|
||||
return new byte[0];
|
||||
@@ -78,9 +75,7 @@ public class SydSM4 {
|
||||
|
||||
iPkgNum = 2;
|
||||
return bytes2;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public byte[] finish() {
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.sunyard.proto.Util;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class SydSM4Backup {
|
||||
public class SydSM4Backup implements ISM4 {
|
||||
|
||||
private SydApi api;
|
||||
private int iKeyIndex;
|
||||
|
||||
@@ -5,31 +5,19 @@ import com.sunyard.sge.bytes.BytesQueue;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class SydSM4Mac {
|
||||
private SydApi api;
|
||||
private byte[] last_in_data;
|
||||
private int iKeyIndex;
|
||||
public class SydSM4Mac implements ISM4Mac {
|
||||
private final SydApi api;
|
||||
private final int iKeyIndex;
|
||||
private int iPkgNum = 1;
|
||||
private String pcProcData;
|
||||
private boolean is_finished;
|
||||
private String pcMac;
|
||||
private byte[] pcdataResult = new byte[0];
|
||||
private String pcdataStr;
|
||||
BytesQueue queue = new BytesQueue();
|
||||
private BytesQueue queue = new BytesQueue();
|
||||
|
||||
public SydSM4Mac(SydApi api, int iKeyIndex) {
|
||||
this.api = api;
|
||||
this.iKeyIndex = iKeyIndex;
|
||||
this.pcProcData = pcProcData;
|
||||
// this.pcMac=pcMac;
|
||||
}
|
||||
|
||||
public SydSM4Mac(SydApi api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
public void setPcMac(String pcMac) {
|
||||
this.pcMac = pcMac;
|
||||
this.pcProcData = null;
|
||||
}
|
||||
|
||||
public void update(byte[] pcData) {
|
||||
@@ -37,7 +25,7 @@ public class SydSM4Mac {
|
||||
return;
|
||||
}
|
||||
int length = pcData.length;
|
||||
this.pcdataResult = pcData;
|
||||
byte[] pcdataResult = pcData;
|
||||
if (is_finished) {
|
||||
throw new SydApiException(-1);
|
||||
}
|
||||
@@ -51,7 +39,7 @@ public class SydSM4Mac {
|
||||
if (length % 16 != 0) {
|
||||
queue.push(pcData);
|
||||
byte[] pop = queue.pop(16, 0);
|
||||
pcdataResult=pop;
|
||||
pcdataResult =pop;
|
||||
}
|
||||
if (null == this.pcMac || "".equals(this.pcMac)) {
|
||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pcdataResult, iPkgNum, pcProcData);
|
||||
@@ -82,7 +70,7 @@ public class SydSM4Mac {
|
||||
}
|
||||
|
||||
}
|
||||
public LongDataReturn disget() {
|
||||
public String disget() {
|
||||
|
||||
if (is_finished) {
|
||||
throw new SydApiException(0);
|
||||
@@ -95,13 +83,8 @@ public class SydSM4Mac {
|
||||
}
|
||||
byte[] pop = queue.pop();
|
||||
|
||||
if (null == this.pcMac || "".equals(this.pcMac)) {
|
||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pop, iPkgNum, pcProcData);
|
||||
return stringStringLongDataReturn;
|
||||
} else {
|
||||
LongDataReturn<String, Boolean> stringBooleanLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pop, iPkgNum, pcProcData, pcMac);
|
||||
return stringBooleanLongDataReturn;
|
||||
}
|
||||
LongDataReturn<String, String> stringStringLongDataReturn = api.SYD_SM4Mac_LongData(iKeyIndex, pop, iPkgNum, pcProcData);
|
||||
return stringStringLongDataReturn.getData();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.sunyard.sge.database.pool;
|
||||
import com.sunyard.SydApiException;
|
||||
import com.sunyard.sge.database.SydApi;
|
||||
import com.sunyard.sge.database.SydApi4Database;
|
||||
import com.sunyard.sge.database.SydApiBaseFunction;
|
||||
import com.sunyard.sge.log.LogFactory;
|
||||
import com.sunyard.sge.pool.ObjectInPool;
|
||||
import com.sunyard.sge.pool.ThreadBasedConnectPool;
|
||||
@@ -51,7 +52,7 @@ public class SydApiPool extends ThreadBasedConnectPool<SydApi> {
|
||||
}
|
||||
|
||||
|
||||
return new SydApi4Database(apis, linkInfos);
|
||||
return new SydApiBaseFunction(apis, linkInfos);
|
||||
}
|
||||
|
||||
// 保活接口
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.sunyard.sge.database.pool;
|
||||
|
||||
import com.sunyard.sge.pool.CommWorker;
|
||||
|
||||
public class Workers {
|
||||
// 长连接的所有处理
|
||||
private static final CommWorker longLinkWorks = new CommWorker();
|
||||
|
||||
// 短链接 批量处理
|
||||
private static final CommWorker shortLinkWorks = new CommWorker();
|
||||
|
||||
public static CommWorker getLongLinkWorks() {
|
||||
return longLinkWorks;
|
||||
}
|
||||
|
||||
public static CommWorker getShortLinkWorks() {
|
||||
return shortLinkWorks;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user