|
@@ -1,6 +1,8 @@
|
|
|
package com.fdkankan.jp.xspace.listener;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.thread.ThreadUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.fdkankan.common.constant.CommonSuccessStatus;
|
|
|
import com.fdkankan.dingtalk.DingTalkSendUtils;
|
|
|
import com.fdkankan.jp.xspace.common.constant.NasPathConstant;
|
|
@@ -14,16 +16,19 @@ import com.fdkankan.jp.xspace.service.IUnityService;
|
|
|
import com.fdkankan.jp.xspace.service.IUserService;
|
|
|
import com.rabbitmq.client.Channel;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.tomcat.util.threads.ThreadPoolExecutor;
|
|
|
import org.springframework.amqp.core.Message;
|
|
|
import org.springframework.amqp.rabbit.annotation.Queue;
|
|
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Date;
|
|
|
+import java.util.concurrent.*;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -44,6 +49,10 @@ public class RabbitMqListener {
|
|
|
"**账号**: %s\n\n" +
|
|
|
"**工程目录**: %s\n\n";
|
|
|
|
|
|
+// public static final
|
|
|
+
|
|
|
+ public static ExecutorService executorService = Executors.newFixedThreadPool(1);
|
|
|
+
|
|
|
@Value("${spring.profiles.active}")
|
|
|
private String profile;
|
|
|
@Resource
|
|
@@ -61,40 +70,52 @@ public class RabbitMqListener {
|
|
|
* queuesToDeclare = @Queue("${queue.modeling.modeling-test}"), 如果队列不不存在会自动创建队列
|
|
|
* concurrency = "3" 设置消费线程数,每个线程每次只拉取一条消息消费
|
|
|
*/
|
|
|
-// @RabbitListener(
|
|
|
-// queuesToDeclare = @Queue(RabbitmqConstant.QUEUE_PACK_XSPACE),
|
|
|
-// concurrency = "1"
|
|
|
-// )
|
|
|
+ @RabbitListener(
|
|
|
+ queuesToDeclare = @Queue(RabbitmqConstant.QUEUE_PACK_XSPACE),
|
|
|
+ concurrency = "1"
|
|
|
+ )
|
|
|
public void packXspace(Channel channel, Message message) throws Exception {
|
|
|
String messageId = message.getMessageProperties().getMessageId();
|
|
|
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
|
|
|
log.info("开始消费消息,id:{},content:{}", messageId, msg);
|
|
|
- SceneXspaceMqDTO dto = BeanUtil.toBean(msg, SceneXspaceMqDTO.class);
|
|
|
- SceneXspace bean = BeanUtil.copyProperties(dto, SceneXspace.class);
|
|
|
+ SceneXspace bean = JSON.parseObject(msg, SceneXspace.class);
|
|
|
String errorReason = null;
|
|
|
|
|
|
+ Future submit = executorService.submit(()-> unityService.packXspace(bean));
|
|
|
+
|
|
|
try {
|
|
|
- unityService.packXspace(bean);
|
|
|
+ submit.get(5L, TimeUnit.SECONDS);
|
|
|
bean.setStatus(CommonSuccessStatus.SUCCESS.code());
|
|
|
- }catch (PackException e){
|
|
|
+ }catch (TimeoutException e){
|
|
|
bean.setStatus(CommonSuccessStatus.FAIL.code());
|
|
|
- errorReason = e.getMessage();
|
|
|
+ errorReason = "打包超时";
|
|
|
+ submit.cancel(true);
|
|
|
} catch (Exception e) {
|
|
|
- bean.setStatus(CommonSuccessStatus.FAIL.code());
|
|
|
- errorReason = "后端报错";
|
|
|
- }
|
|
|
-
|
|
|
- if(bean.getStatus() == CommonSuccessStatus.FAIL.code()){
|
|
|
- User user = userService.getById(bean.getUserId());
|
|
|
- String workPath = NasPathConstant.UNITY_WORK_PATH + bean.getNum() + "/" + bean.getSerial() + "/";
|
|
|
- String content = String.format(DINGTALK_MSG_PATTERN, profile, errorReason, bean.getNum(), user.getUserName(), workPath);
|
|
|
- dingTalkSendUtils.sendActioncardMsgToDingRobot(content, "场景同步失败");
|
|
|
+ Throwable cause = e.getCause();
|
|
|
+ if(cause instanceof PackException){
|
|
|
+ bean.setStatus(CommonSuccessStatus.FAIL.code());
|
|
|
+ errorReason = cause.getMessage();
|
|
|
+ } else{
|
|
|
+ bean.setStatus(CommonSuccessStatus.FAIL.code());
|
|
|
+ errorReason = "后端报错";
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//更新状态
|
|
|
bean.setUpdateTime(new Date());
|
|
|
sceneXspaceService.updateById(bean);
|
|
|
|
|
|
+ try {
|
|
|
+ if(bean.getStatus() == CommonSuccessStatus.FAIL.code()){
|
|
|
+ User user = userService.getById(bean.getUserId());
|
|
|
+ String workPath = NasPathConstant.UNITY_WORK_PATH + bean.getNum() + "/" + bean.getSerial() + "/";
|
|
|
+ String content = String.format(DINGTALK_MSG_PATTERN, profile, errorReason, bean.getNum(), user.getUserName(), workPath);
|
|
|
+ dingTalkSendUtils.sendActioncardMsgToDingRobot(content, "场景同步失败");
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("发送钉钉失败", e);
|
|
|
+ }
|
|
|
+
|
|
|
log.info("结束消费消息,id:{}", messageId);
|
|
|
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
|
|
}
|