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

dev
huangfeng 6 days ago
parent 7986230a59
commit 1084a6c3ed

@ -119,8 +119,7 @@ public class TerminalPhotoController extends BaseController {
} else {
return ResponseReult.error(serviceBody.getCode(), serviceBody.getMsg());
}
}
finally {
} finally {
workingMap.remove(vo.getRequestid());
}
}

@ -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="thumb" jdbcType="VARCHAR" property="thumb" />
<result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
<result column="width" jdbcType="INTEGER" property="markWidth" />
<result column="height" jdbcType="INTEGER" property="markHeight" />
<result column="width" jdbcType="INTEGER" property="width" />
<result column="height" jdbcType="INTEGER" property="height" />
<result column="color" jdbcType="VARCHAR" property="color" />
<result column="boder_width" jdbcType="SMALLINT" property="boderWidth" />
<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;
@Resource
MqttService mqttService;
@Resource
AsyncService asyncService;
@Autowired
RequestResultsDao requestResultsDao;
@ -105,7 +107,7 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
private String photoAddress;
//服务器中的位置
private static final String C_PATH = "/home/xymp/photos/";
public static final String C_PATH = "/home/xymp/photos/";
/**
* idid
@ -432,13 +434,16 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
if (term == null) {
throw new ApiException(cmdid + "该装置不存在");
}
List<PhotoPrepareModel> list = new ArrayList<>();
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 hexC = "";
Long photoId;
@ -458,55 +463,19 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService {
record.setPresetId(preset);
record.setPhotoTime(BigInteger.valueOf(time));
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) {
throw new ApiException("文件名不符合规范,解析失败");
}
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);
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);
}
return item;
}
/**

@ -95,6 +95,7 @@ public interface TerminalPhotoService {
/**
*
*
* @param vo
* @return
*/
@ -103,6 +104,7 @@ public interface TerminalPhotoService {
/**
*
*
* @param file
* @param termid
* @param cmdid

Loading…
Cancel
Save