Authored by 张帅

异步处理图片尺寸

... ... @@ -36,7 +36,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.stream.Collectors;
/**
... ... @@ -89,7 +89,10 @@ public class GrassArticleServiceImpl implements IGrassArticleService{
@Value("${uic.service.url:http://uic.yohoops.org/uic}")
private String uicUrl;
private ExecutorService taskExecutor = Executors.newFixedThreadPool(6);
@Override
@Database(ForceMaster = true)
public void publishArticle(GrassArticleReq req) throws PlatformException {
logger.info("enter publishArticle, req is {}", req);
publishCheck(req);
... ... @@ -153,6 +156,7 @@ public class GrassArticleServiceImpl implements IGrassArticleService{
contentList.add(content);
}
grassArticleBlockDao.insertArticleContents(contentList);
updateArticleImageSize(article.getId());
logger.info("insert grass article content success, ariticle id is {}, contentList size is {}", article.getId(), contentList.size());
}catch (Exception e){
logger.warn("insert article images fail,ariticle id is {}, e is {}",article.getId(), e );
... ... @@ -228,10 +232,10 @@ public class GrassArticleServiceImpl implements IGrassArticleService{
switch (templatName){
case BLOCK_IMAGE:
String url = convertImageUrlWithDefault(data);
JSONObject imageInfo = getImageInfoFromQN(data);
// JSONObject imageInfo = getImageInfoFromQN(data);
dataObject.put("src",url);
dataObject.put("width",imageInfo == null ? 1 : imageInfo.getString("width"));
dataObject.put("height",imageInfo == null ? 1 : imageInfo.getString("height"));
dataObject.put("width", 1 );
dataObject.put("height",1);
break;
case BLOCK_TEXT:
dataObject.put("text",data);
... ... @@ -243,6 +247,61 @@ public class GrassArticleServiceImpl implements IGrassArticleService{
return o.toJSONString();
}
//异步处理图片原始尺寸问题
private void updateArticleImageSize(int articleId){
taskExecutor.submit(() -> {
updateImageSize(articleId);
});
}
private void updateImageSize(int articleId){
logger.info("enter updateImageSize, articleId is {}",articleId);
List<GrassArticleBlock> blockList = grassArticleBlockDao.selectByArticleId(articleId);
Map<String, Future<JSONObject>> map = Maps.newConcurrentMap();
for (GrassArticleBlock block : blockList) {
if(block.getTemplateKey().equals("text")){
continue;
}
JSONObject object = JSONObject.parseObject(block.getContentData());
JSONObject data = JSONObject.parseObject(object.getString("data"));
String url = data.getString("src");
String urlWithoutMode = url.substring(0,url.indexOf("?"));
Future<JSONObject> imageInfo = taskExecutor.submit(() -> getImageInfoFromQN(urlWithoutMode));
map.put(url, imageInfo);
}
List<GrassArticleBlock> needUpdateBlocks = Lists.newArrayList();
for (GrassArticleBlock block : blockList) {
if(block.getTemplateKey().equals("text")){
continue;
}
JSONObject object = JSONObject.parseObject(block.getContentData());
JSONObject data = JSONObject.parseObject(object.getString("data"));
String url = data.getString("src");
JSONObject imageInfo = null;
try {
imageInfo = map.get(url).get();
} catch (Exception e) {
e.printStackTrace();
}
if(imageInfo != null){
String width = imageInfo.getString("width");
String height = imageInfo.getString("height");
data.put("width", width);
data.put("height", height);
object.put("data",data);
block.setContentData(object.toString());
block.setUpdateTime(System.currentTimeMillis());
needUpdateBlocks.add(block);
}
}
logger.info("enter updateImageSize, needUpdateBlocks size is {}",needUpdateBlocks.size());
if(CollectionUtils.isNotEmpty(needUpdateBlocks)){
grassArticleBlockDao.updateBlocks(needUpdateBlocks);
}
logger.info(" updateImageSize success, articleId is {}",articleId);
}
private JSONObject getImageInfoFromQN(String data){
try{
String url = data + "?imageInfo";
... ... @@ -449,6 +508,7 @@ public class GrassArticleServiceImpl implements IGrassArticleService{
if(CollectionUtils.isNotEmpty(addList)){
grassArticleBlockDao.insertArticleContents(addList);
updateArticleImageSize(articleId);
}
//商品
... ... @@ -619,6 +679,7 @@ public class GrassArticleServiceImpl implements IGrassArticleService{
}
@Override
@Database(ForceMaster = true)
public void updateArticleDraft(GrassArticleReq req) throws PlatformException {
// 操作类型,1 编辑后保存草稿箱 2 编辑后直接发布 3 编辑后定时发布
... ... @@ -710,6 +771,7 @@ public class GrassArticleServiceImpl implements IGrassArticleService{
if(CollectionUtils.isNotEmpty(addList)){
grassArticleBlockDao.insertArticleContents(addList);
updateArticleImageSize(articleId);
}
//商品
... ... @@ -792,6 +854,7 @@ public class GrassArticleServiceImpl implements IGrassArticleService{
}
@Override
@Database(ForceMaster = true)
public void timerPublish(GrassArticleReq req) throws PlatformException {
logger.info("enter timerPublish, req is {}", req);
publishCheck(req);
... ... @@ -860,6 +923,7 @@ public class GrassArticleServiceImpl implements IGrassArticleService{
contentList.add(content);
}
grassArticleBlockDao.insertArticleContents(contentList);
updateArticleImageSize(article.getId());
logger.info("timerPublish insert grass article content success, ariticle id is {}, contentList size is {}", article.getId(), contentList.size());
}catch (Exception e){
logger.warn("timerPublish insert article images fail,ariticle id is {}, e is {}",article.getId(), e );
... ...