perf: 改用异步线程队列保存上传的图片

dev
huangfeng 7 days ago
parent 7986230a59
commit 1084a6c3ed

@ -108,8 +108,8 @@ public class TerminalPhotoController extends BaseController {
@RequestMapping("/getTakePicStatus") @RequestMapping("/getTakePicStatus")
@Log(title = "主动拍照下发状态查询", type = "查询") @Log(title = "主动拍照下发状态查询", type = "查询")
public ResponseReult<TakePIcStatusModel> getTakePicStatus(@Validated ReturnedPhotoVo vo) { public ResponseReult<TakePIcStatusModel> getTakePicStatus(@Validated ReturnedPhotoVo vo) {
try{ try {
if (workingMap.containsKey(vo.getRequestid())){ if (workingMap.containsKey(vo.getRequestid())) {
throw new ApiException("相同请求正在处理中, 请刷新页面更新"); throw new ApiException("相同请求正在处理中, 请刷新页面更新");
} }
workingMap.put(vo.getRequestid(), "1"); workingMap.put(vo.getRequestid(), "1");
@ -119,8 +119,7 @@ public class TerminalPhotoController extends BaseController {
} else { } else {
return ResponseReult.error(serviceBody.getCode(), serviceBody.getMsg()); return ResponseReult.error(serviceBody.getCode(), serviceBody.getMsg());
} }
} } finally {
finally {
workingMap.remove(vo.getRequestid()); workingMap.remove(vo.getRequestid());
} }
} }
@ -129,7 +128,7 @@ public class TerminalPhotoController extends BaseController {
@ApiResponses({@ApiResponse(code = 200, message = "请求成功"), @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")}) @ApiResponses({@ApiResponse(code = 200, message = "请求成功"), @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")})
@RequestMapping("/getTakePicPhotoStatus") @RequestMapping("/getTakePicPhotoStatus")
@Log(title = "主动拍照下照片状态查询", type = "查询") @Log(title = "主动拍照下照片状态查询", type = "查询")
public ResponseReult<Boolean> getTakePicPhotoStatus( @Validated ReturnedPhotoVo vo) { public ResponseReult<Boolean> getTakePicPhotoStatus(@Validated ReturnedPhotoVo vo) {
try { try {
if (workingMap.containsKey(vo.getRequestid())) { if (workingMap.containsKey(vo.getRequestid())) {
throw new ApiException("相同请求正在处理中, 请刷新页面更新"); throw new ApiException("相同请求正在处理中, 请刷新页面更新");
@ -198,7 +197,7 @@ public class TerminalPhotoController extends BaseController {
private void buildPhotoStat(List<StatTerm> termList, List<TerminalPhotoSelectListModel.PhotoBean> list, TerminalPhotoSelectVo vo) throws Exception { private void buildPhotoStat(List<StatTerm> termList, List<TerminalPhotoSelectListModel.PhotoBean> list, TerminalPhotoSelectVo vo) throws Exception {
for (StatTerm statTerm : termList) { for (StatTerm statTerm : termList) {
List<CameraSchedule> scheduleList = cameraScheduleService.list(statTerm.getTermId()); List<CameraSchedule> scheduleList = cameraScheduleService.list(statTerm.getTermId());
statTerm.initScheduleList(scheduleList, vo.getStarttime().getTime() , vo.getEndtime().getTime() , vo.getInterval()); statTerm.initScheduleList(scheduleList, vo.getStarttime().getTime(), vo.getEndtime().getTime(), vo.getInterval());
} }
if (list != null) { if (list != null) {
for (TerminalPhotoSelectListModel.PhotoBean photo : list) { for (TerminalPhotoSelectListModel.PhotoBean photo : list) {

@ -0,0 +1,18 @@
package com.shxy.xymanager_common.model;
import com.shxy.xymanager_common.entity.TerminalPhoto;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;
@Data
public class PhotoPrepareModel {
MultipartFile file;
String cmdid;
Integer termId;
String hexC;
TerminalPhoto record;
Long photoId;
Long time;
String suffix;
String fileName;
}

@ -65,8 +65,8 @@
<result column="path" jdbcType="VARCHAR" property="path" /> <result column="path" jdbcType="VARCHAR" property="path" />
<result column="thumb" jdbcType="VARCHAR" property="thumb" /> <result column="thumb" jdbcType="VARCHAR" property="thumb" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" /> <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="width" jdbcType="INTEGER" property="markWidth" /> <result column="width" jdbcType="INTEGER" property="width" />
<result column="height" jdbcType="INTEGER" property="markHeight" /> <result column="height" jdbcType="INTEGER" property="height" />
<result column="color" jdbcType="VARCHAR" property="color" /> <result column="color" jdbcType="VARCHAR" property="color" />
<result column="boder_width" jdbcType="SMALLINT" property="boderWidth" /> <result column="boder_width" jdbcType="SMALLINT" property="boderWidth" />
<collection javaType="list" ofType="com.shxy.xymanager_common.dto.TermChannelCoordinateDto" property="lineList"> <collection javaType="list" ofType="com.shxy.xymanager_common.dto.TermChannelCoordinateDto" property="lineList">

@ -0,0 +1,106 @@
package com.shxy.xymanager_service.impl;
import com.shxy.xymanager_common.entity.TerminalPhoto;
import com.shxy.xymanager_common.entity.TerminalPhotoExample;
import com.shxy.xymanager_common.model.PhotoPrepareModel;
import com.shxy.xymanager_common.util.DateUtil;
import com.shxy.xymanager_dao.dao.TerminalPhotoDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;
import static com.shxy.xymanager_service.impl.TerminalPhotoServiceImpl.C_PATH;
@Service
@Slf4j
public class AsyncService {
@Resource
TerminalPhotoDao terminalPhotoDao;
@Async
public void savePreparePhotos(List<PhotoPrepareModel> list) {
for (PhotoPrepareModel item : list) {
this.saveOnePreparePhoto(item);
}
}
private void saveOnePreparePhoto(PhotoPrepareModel item) {
TerminalPhoto record = item.getRecord();
String cmdid = item.getCmdid();
MultipartFile file = item.getFile();
String hexC = item.getHexC();
Integer termId = item.getTermId();
Long photoId = item.getPhotoId();
Long time = item.getTime();
String suffix = item.getSuffix();
String fileName = item.getFileName();
int length = cmdid.length();
Date now = new Date();
String folder = DateUtil.format(now, "yyyy/MM/dd") + "/" + cmdid.substring(length - 2);
File dir = new File(C_PATH + folder);
dir.mkdirs();
Date photoTime = new Date(time * 1000);
String strTime = DateUtil.format(photoTime, "yyyyMMddHHmmss");
String filename = cmdid + "_" + record.getChannelId() + "_" + hexC + "_" + strTime + "." + suffix;
String filePath = folder + "/" + filename;
File dest = new File(C_PATH + filePath);
try {
file.transferTo(dest);
log.info("成功上传一张图片" + filePath);
} catch (IOException e) {
log.error("保存上传的图片失败, cmdid=" + cmdid + ", fileName=" + fileName);
return;
}
record.setTermId(termId);
record.setMediaType(0);
record.setPath(filePath);
record.setRecvTime(BigInteger.valueOf(now.getTime() / 1000));
record.setRecvEndTime(BigInteger.valueOf(now.getTime() / 1000));
record.setCreateTime(now);
record.setFlags(0);
record.setIsMark(0);
try {
BufferedImage image = ImageIO.read(dest);
if (image != null) {
int width = image.getWidth();
int height = image.getHeight();
record.setWidth(width);
record.setHeight(height);
}
} catch (IOException ignore) {
}
try {
long fileSize = Files.size(Paths.get(C_PATH + filePath));
record.setFileSize((int) fileSize);
} catch (IOException ignore) {
}
TerminalPhotoExample example = new TerminalPhotoExample();
TerminalPhotoExample.Criteria criteria = example.createCriteria();
criteria.andTermIdEqualTo(termId);
criteria.andOrginalIdEqualTo(photoId.intValue());
List<TerminalPhoto> list = terminalPhotoDao.selectByExample(example);
if (CollectionUtils.isEmpty(list)) {
terminalPhotoDao.insert(record);
} else {
record.setId(list.get(0).getId());
terminalPhotoDao.updateByPrimaryKey(record);
}
}
}

@ -89,6 +89,8 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
TerminalExtService terminalExtService; TerminalExtService terminalExtService;
@Resource @Resource
MqttService mqttService; MqttService mqttService;
@Resource
AsyncService asyncService;
@Autowired @Autowired
RequestResultsDao requestResultsDao; RequestResultsDao requestResultsDao;
@ -105,7 +107,7 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
private String photoAddress; private String photoAddress;
//服务器中的位置 //服务器中的位置
private static final String C_PATH = "/home/xymp/photos/"; public static final String C_PATH = "/home/xymp/photos/";
/** /**
* idid * idid
@ -432,13 +434,16 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
if (term == null) { if (term == null) {
throw new ApiException(cmdid + "该装置不存在"); throw new ApiException(cmdid + "该装置不存在");
} }
List<PhotoPrepareModel> list = new ArrayList<>();
for (MultipartFile file : files) { for (MultipartFile file : files) {
this.uploadOnePhoto(file, cmdid, term.getId()); PhotoPrepareModel item = this.preparePhoto(file, cmdid, term.getId());
list.add(item);
} }
asyncService.savePreparePhotos(list);
} }
private void uploadOnePhoto(MultipartFile file, String cmdid, Integer termId) throws Exception { private PhotoPrepareModel preparePhoto(MultipartFile file, String cmdid, Integer termId) throws Exception {
PhotoPrepareModel item = new PhotoPrepareModel();
String suffix; String suffix;
String hexC = ""; String hexC = "";
Long photoId; Long photoId;
@ -458,55 +463,19 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
record.setPresetId(preset); record.setPresetId(preset);
record.setPhotoTime(BigInteger.valueOf(time)); record.setPhotoTime(BigInteger.valueOf(time));
record.setOrginalId(BigInteger.valueOf(photoId)); record.setOrginalId(BigInteger.valueOf(photoId));
item.setRecord(record);
item.setHexC(hexC);
item.setPhotoId(photoId);
item.setTime(time);
item.setSuffix(suffix);
item.setFileName(fileName);
item.setFile(file);
item.setCmdid(cmdid);
item.setTermId(termId);
} catch (Exception ex) { } catch (Exception ex) {
throw new ApiException("文件名不符合规范,解析失败"); throw new ApiException("文件名不符合规范,解析失败");
} }
int length = cmdid.length(); return item;
Date now = new Date();
String folder = DateUtil.format(now, "yyyy/MM/dd") + "/" + cmdid.substring(length - 2);
File dir = new File(C_PATH + folder);
dir.mkdirs();
Date photoTime = new Date(time * 1000);
String strTime = DateUtil.format(photoTime, "yyyyMMddHHmmss");
String filename = cmdid + "_" + record.getChannelId() + "_" + hexC + "_" + strTime + "." + suffix;
String filePath = folder + "/" + filename;
File dest = new File(C_PATH + filePath);
file.transferTo(dest);
log.info("成功上传一张图片" + filePath);
record.setTermId(termId);
record.setMediaType(0);
record.setPath(filePath);
record.setRecvTime(BigInteger.valueOf(now.getTime() / 1000));
record.setRecvEndTime(BigInteger.valueOf(now.getTime() / 1000));
record.setCreateTime(now);
record.setFlags(0);
record.setIsMark(0);
try {
BufferedImage image = ImageIO.read(dest);
if (image != null) {
int width = image.getWidth();
int height = image.getHeight();
record.setWidth(width);
record.setHeight(height);
}
} catch (IOException ignore) {
}
long fileSize = Files.size(Paths.get(C_PATH + filePath));
record.setFileSize((int) fileSize);
TerminalPhotoExample example = new TerminalPhotoExample();
TerminalPhotoExample.Criteria criteria = example.createCriteria();
criteria.andTermIdEqualTo(termId);
criteria.andOrginalIdEqualTo(photoId.intValue());
List<TerminalPhoto> list = terminalPhotoDao.selectByExample(example);
if (CollectionUtils.isEmpty(list)) {
terminalPhotoDao.insert(record);
} else {
record.setId(list.get(0).getId());
terminalPhotoDao.updateByPrimaryKey(record);
}
} }
/** /**

@ -24,7 +24,7 @@ public interface TerminalPhotoService {
* @return * @return
*/ */
ServiceBody<TerminalPhotoListModel> getTerminalPhotoList(String requestIp,TerminalAndChannelIdAndTimeVo vo); ServiceBody<TerminalPhotoListModel> getTerminalPhotoList(String requestIp, TerminalAndChannelIdAndTimeVo vo);
/** /**
* *
@ -35,7 +35,7 @@ public interface TerminalPhotoService {
*/ */
ServiceBody<TerminalPhotoSelectListModel> getPhotoList(String requestIp, TerminalPhotoSelectVo vo, List<StatTerm> termList); ServiceBody<TerminalPhotoSelectListModel> getPhotoList(String requestIp, TerminalPhotoSelectVo vo, List<StatTerm> termList);
/** /**
* id * id
* *
* @return * @return
@ -79,7 +79,7 @@ public interface TerminalPhotoService {
ServiceBody<String> addOrUpdatePhotoParams(PhotoParamsVo vo); ServiceBody<String> addOrUpdatePhotoParams(PhotoParamsVo vo);
ServiceBody<TerminalPhotosParamsModel> getLastedPhotoQueryParam(ReturnedPhotoParamsVo vo); ServiceBody<TerminalPhotosParamsModel> getLastedPhotoQueryParam(ReturnedPhotoParamsVo vo);
ServiceBody<GetModel> selectPhotoTimeGet(TerminalPhotoTimeGetVo vo); ServiceBody<GetModel> selectPhotoTimeGet(TerminalPhotoTimeGetVo vo);
@ -95,6 +95,7 @@ public interface TerminalPhotoService {
/** /**
* *
*
* @param vo * @param vo
* @return * @return
*/ */
@ -103,6 +104,7 @@ public interface TerminalPhotoService {
/** /**
* *
*
* @param file * @param file
* @param termid * @param termid
* @param cmdid * @param cmdid
@ -116,7 +118,7 @@ public interface TerminalPhotoService {
ServiceBody<TerminalPhotoListModel> getTestTerminalPhotoList(String requestIp); ServiceBody<TerminalPhotoListModel> getTestTerminalPhotoList(String requestIp);
ServiceBody<String> takeAlarm(TerminalPhotoTestVo vo); ServiceBody<String> takeAlarm(TerminalPhotoTestVo vo);
void uploadPhotos(MultipartFile[] files, String cmdid) throws Exception; void uploadPhotos(MultipartFile[] files, String cmdid) throws Exception;

Loading…
Cancel
Save