Quellcode durchsuchen

do't throw exceptions if components do not exist while updating goods' index

sunyj vor 8 Jahren
Ursprung
Commit
e5aa8d4835

+ 3 - 2
mall-search/src/main/java/com/uas/search/dao/GoodsDao.java

@@ -1,5 +1,6 @@
 package com.uas.search.dao;
 
+import com.uas.search.exception.DataNotFoundException;
 import com.uas.search.model.*;
 import com.uas.search.util.CollectionUtils;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -55,7 +56,7 @@ public class GoodsDao {
     private Goods findByGoId(Long goId) {
         TradeGoods tradeGoods = tradeGoodsDao.findOne(goId);
         if (tradeGoods == null) {
-            throw new IllegalStateException("批次不存在:" + goId);
+            throw new DataNotFoundException("批次不存在:" + goId);
         }
         return findByTradeGoods(tradeGoods);
     }
@@ -91,7 +92,7 @@ public class GoodsDao {
     public List<Goods> findByCmpId(Long cmpId) {
         Component component = componentDao.findOne(cmpId);
         if (component == null) {
-            throw new IllegalStateException("器件不存在:" + cmpId);
+            throw new DataNotFoundException("器件不存在:" + cmpId);
         }
         return findByComponent(component);
     }

+ 29 - 0
mall-search/src/main/java/com/uas/search/exception/DataNotFoundException.java

@@ -0,0 +1,29 @@
+package com.uas.search.exception;
+
+/**
+ * 从数据库取数据时,数据不存在
+ *
+ * @author sunyj
+ * @since 2018/1/25 9:13
+ */
+public class DataNotFoundException extends RuntimeException {
+
+    public DataNotFoundException() {
+    }
+
+    public DataNotFoundException(String message) {
+        super(message);
+    }
+
+    public DataNotFoundException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public DataNotFoundException(Throwable cause) {
+        super(cause);
+    }
+
+    public DataNotFoundException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
+        super(message, cause, enableSuppression, writableStackTrace);
+    }
+}

+ 12 - 6
mall-search/src/main/java/com/uas/search/service/impl/IndexServiceImpl.java

@@ -9,6 +9,7 @@ import com.uas.search.constant.model.PageInfo;
 import com.uas.search.constant.model.PageParams;
 import com.uas.search.constant.model.SPage;
 import com.uas.search.dao.*;
+import com.uas.search.exception.DataNotFoundException;
 import com.uas.search.jms.JmsListener;
 import com.uas.search.jms.QueueMessageParser;
 import com.uas.search.model.*;
@@ -714,13 +715,18 @@ public class IndexServiceImpl implements IndexService {
                 delete(object);
                 Goods goodsObject = (Goods) object;
                 if (goodsObject.getComponent() != null && goodsObject.getComponent().getId() != null) {
-                    // 如果是器件,再重新写入
-                    List<Goods> goodsesList = goodsDao.find((Goods) object);
-                    for (Goods goods : goodsesList) {
-                        Object maintainedObject = save(goods);
-                        if (maintainedObject != null) {
-                            maintainedObjects.add(maintainedObject);
+                    try {
+                        // 如果是器件,再重新写入
+                        List<Goods> goodsesList = goodsDao.find((Goods) object);
+                        for (Goods goods : goodsesList) {
+                            Object maintainedObject = save(goods);
+                            if (maintainedObject != null) {
+                                maintainedObjects.add(maintainedObject);
+                            }
                         }
+                    } catch (DataNotFoundException e) {
+                        // 删除操作时,器件可能已经不存在,此时是正常情况,不必抛异常
+                        logger.warn("器件已删除,不必重新写入相关批次和器件" ,e);
                     }
                 }
             } else {