绿通图片接口,多线程取图片逻辑修改,修改图片存放顺序

master
gsy 2 years ago
parent ad26dd678c
commit d9e46c5e57

@ -272,6 +272,7 @@ public class GreenTrafficServiceImpl implements IGreenTrafficService {
//从配置文件中读取数据库连接类型
params.put("DBTYPE", PropertiesUtil.getValue("DBType"));
JSONArray jsonArray = new JSONArray();
List<String> picList = new ArrayList<>();
//如果流水号没有的话,直接返回失败
@ -293,6 +294,7 @@ public class GreenTrafficServiceImpl implements IGreenTrafficService {
logger.info("取图片轮次=======round======="+round);
//定义同步工具,每轮五个线程
CountDownLatch countDownLatch = new CountDownLatch(5); // 同步工具
@ -302,6 +304,15 @@ public class GreenTrafficServiceImpl implements IGreenTrafficService {
//定义每一轮 取的时候从第几个下标开始取 1 6 11
int position = ((round-1)*taskCount)+1;
//给jsonArray增加5张图片位置让图片按照顺序放置
for (int j = position-1 ; j < getOver ; j++){
JSONArray pic = new JSONArray();
pic.add("");
pic.add("");
jsonArray.add(pic);
}
//循环开启5个线程取图片
for (int i = position ; i <= getOver;i++) {
@ -309,10 +320,18 @@ public class GreenTrafficServiceImpl implements IGreenTrafficService {
Map<String, Object> paramsWebService = new HashMap<>();
paramsWebService.put("listNO", params.get("listNo")+"_"+i);
paramsWebService.put("imgType", "1");
//执行线程
//定义每张图片要放置的位置,避免顺序错乱
int finalI = i-1;
greenTrafficThreadPool.execute( () -> {
try {
picList.add(getPic(paramsWebService));
//取出该位置的JSONArray放置图片
JSONArray pic = (JSONArray) jsonArray.get(finalI);
pic.set(1,getPic(paramsWebService));
jsonArray.set(finalI,pic);
} catch (Exception e) {
logger.info("获取图片失败========"+e);
picList.add("");
@ -327,8 +346,9 @@ public class GreenTrafficServiceImpl implements IGreenTrafficService {
logger.info("主线程工作结束==============");
//遍历数据,是否有空,有空就停止执行
for (String pic : picList) {
if("".equals(pic)){
for (Object pic : jsonArray) {
JSONArray obj = (JSONArray) pic;
if("".equals(obj.get(1))){
isStop = false;
break;
}
@ -338,14 +358,12 @@ public class GreenTrafficServiceImpl implements IGreenTrafficService {
logger.info("是否停止取图片=======isStop======="+isStop);
}
//封装数据,为空的跳过
JSONArray jsonArray = new JSONArray();
for (String picUrl : picList) {
if(!"".equals(picUrl)){
JSONArray jsonArrayPic = new JSONArray();
jsonArrayPic.add("");
jsonArrayPic.add(picUrl);
jsonArray.add(jsonArrayPic);
//封装数据,为空的删除
for (int i = 0 ; i < jsonArray.size() ; i++) {
JSONArray obj = (JSONArray) jsonArray.get(i);
if("".equals(obj.get(1))){
jsonArray.remove(i);
i--;
}
}
//设置数据

Loading…
Cancel
Save