Browse Source

fix(获取消息数量):处理内存被占用的情况。

yuj 7 năm trước cách đây
mục cha
commit
f7592edf2c

+ 65 - 22
src/main/java/com/uas/platform/b2b/controller/SnapshotController.java

@@ -1,5 +1,6 @@
 package com.uas.platform.b2b.controller;
 package com.uas.platform.b2b.controller;
 
 
+import com.uas.platform.b2b.core.util.BoundedExecutor;
 import com.uas.platform.b2b.core.util.ThreadUtils;
 import com.uas.platform.b2b.core.util.ThreadUtils;
 import com.uas.platform.b2b.model.Role;
 import com.uas.platform.b2b.model.Role;
 import com.uas.platform.b2b.model.Snapshot;
 import com.uas.platform.b2b.model.Snapshot;
@@ -20,6 +21,9 @@ import org.springframework.web.bind.annotation.RestController;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 
 /**
 /**
  * 平台消息、任务快照
  * 平台消息、任务快照
@@ -31,11 +35,19 @@ import java.util.concurrent.ConcurrentHashMap;
 @RequestMapping("/snapshot")
 @RequestMapping("/snapshot")
 public class SnapshotController {
 public class SnapshotController {
 
 
-	@Autowired
-	private SnapshotService snapshotService;
+	private final SnapshotService snapshotService;
 
 
-	@Autowired
-    private OrderRedDotService redDotService;
+    private final OrderRedDotService redDotService;
+
+    private final BoundedExecutor executor;
+
+    @Autowired
+    public SnapshotController(SnapshotService snapshotService, OrderRedDotService redDotService) {
+        this.snapshotService = snapshotService;
+        this.redDotService = redDotService;
+        ExecutorService executorService = Executors.newCachedThreadPool();
+        executor = new BoundedExecutor(executorService, 1600);
+    }
 
 
     /**
     /**
      * 日志
      * 日志
@@ -53,7 +65,7 @@ public class SnapshotController {
 	    Long start = System.currentTimeMillis();
 	    Long start = System.currentTimeMillis();
 		ModelMap modelMap = new ModelMap();
 		ModelMap modelMap = new ModelMap();
 		final Long enUU = SystemSession.getUser().getEnterprise().getUu();
 		final Long enUU = SystemSession.getUser().getEnterprise().getUu();
-		ThreadUtils.task(()-> {
+        ThreadUtils.task(()-> {
 			List<Snapshot> snapshots = snapshotService.findByEnUU(enUU);
 			List<Snapshot> snapshots = snapshotService.findByEnUU(enUU);
 			if (!CollectionUtils.isEmpty(snapshots)) {
 			if (!CollectionUtils.isEmpty(snapshots)) {
 				snapshots.stream()
 				snapshots.stream()
@@ -73,7 +85,7 @@ public class SnapshotController {
 	 */
 	 */
 	@RequestMapping(value = "/unread/all", method = RequestMethod.GET, headers = "Accept=application/json")
 	@RequestMapping(value = "/unread/all", method = RequestMethod.GET, headers = "Accept=application/json")
 	@ResponseStatus(value = HttpStatus.OK)
 	@ResponseStatus(value = HttpStatus.OK)
-	public ModelMap getUnreadCount() {
+	public ModelMap getUnreadCount() throws InterruptedException {
         Long start = System.currentTimeMillis();
         Long start = System.currentTimeMillis();
 		/**
 		/**
 		 * 采用ConcurrentHashMap编写 防止HashMap的put方法在多线程并发的情况下数据丢失
 		 * 采用ConcurrentHashMap编写 防止HashMap的put方法在多线程并发的情况下数据丢失
@@ -81,8 +93,9 @@ public class SnapshotController {
 		final ConcurrentHashMap<String, Integer> hashMap = new ConcurrentHashMap<>(15);
 		final ConcurrentHashMap<String, Integer> hashMap = new ConcurrentHashMap<>(15);
 		final User user = SystemSession.getUser();
 		final User user = SystemSession.getUser();
 		ModelMap modelMap = new ModelMap();
 		ModelMap modelMap = new ModelMap();
+        final CountDownLatch threadsSignal = new CountDownLatch(15);
 		//并行处理
 		//并行处理
-		ThreadUtils.task(() -> {
+		executor.submitTask(() -> {
             //客户资料
             //客户资料
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             final int[] count = {0};
             final int[] count = {0};
@@ -90,7 +103,9 @@ public class SnapshotController {
             list.forEach(objects -> count[0] = count[0] + Integer.valueOf(objects[0].toString()));
             list.forEach(objects -> count[0] = count[0] + Integer.valueOf(objects[0].toString()));
             hashMap.put("customer", count[0]);
             hashMap.put("customer", count[0]);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //供应商绩效考核
             //供应商绩效考核
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             final int[] count = {0};
             final int[] count = {0};
@@ -98,55 +113,73 @@ public class SnapshotController {
             list.forEach(objects -> count[0] = count[0] + Integer.valueOf(objects[0].toString()));
             list.forEach(objects -> count[0] = count[0] + Integer.valueOf(objects[0].toString()));
             hashMap.put("vendorPerformanceAssess", count[0]);
             hashMap.put("vendorPerformanceAssess", count[0]);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //客户采购询价
             //客户采购询价
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> map = redDotService.getInquiryCount(Role.SELLER);
             Map<String, Integer> map = redDotService.getInquiryCount(Role.SELLER);
             hashMap.put("inquiry", map.containsKey("all") ? map.get("all") : 0);
             hashMap.put("inquiry", map.containsKey("all") ? map.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //客户模具询价
             //客户模具询价
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> count = redDotService.getInquiryMouldCount(Role.SELLER);
             Map<String, Integer> count = redDotService.getInquiryMouldCount(Role.SELLER);
             hashMap.put("mould", count.containsKey("all") ? count.get("all") : 0);
             hashMap.put("mould", count.containsKey("all") ? count.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //客户招标
             //客户招标
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> count = redDotService.getTenderCount(Role.SELLER);
             Map<String, Integer> count = redDotService.getTenderCount(Role.SELLER);
             hashMap.put("tender", count.containsKey("all") ? count.get("all") : 0);
             hashMap.put("tender", count.containsKey("all") ? count.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //客户采购订单
             //客户采购订单
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> count = redDotService.getSaleOrderCount(Role.SELLER);
             Map<String, Integer> count = redDotService.getSaleOrderCount(Role.SELLER);
             hashMap.put("order", count.containsKey("all") ? count.get("all") : 0);
             hashMap.put("order", count.containsKey("all") ? count.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //客户采购变更单
             //客户采购变更单
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> map = redDotService.getChangeCount(Role.SELLER);
             Map<String, Integer> map = redDotService.getChangeCount(Role.SELLER);
             hashMap.put("change", map.containsKey("all") ? map.get("all") : 0);
             hashMap.put("change", map.containsKey("all") ? map.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //客户委外单
             //客户委外单
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> count = redDotService.getMakeOrderCount(Role.SELLER);
             Map<String, Integer> count = redDotService.getMakeOrderCount(Role.SELLER);
             hashMap.put("makeOrder", count.containsKey("all") ? count.get("all") : 0);
             hashMap.put("makeOrder", count.containsKey("all") ? count.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //客户发货提醒
             //客户发货提醒
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> count = redDotService.getNoticeCount(Role.SELLER);
             Map<String, Integer> count = redDotService.getNoticeCount(Role.SELLER);
             hashMap.put("notice", count.containsKey("all") ? count.get("all") : 0);
             hashMap.put("notice", count.containsKey("all") ? count.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //客户打样申请
             //客户打样申请
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> count = redDotService.getProofingCount(Role.SELLER);
             Map<String, Integer> count = redDotService.getProofingCount(Role.SELLER);
             hashMap.put("sample", count.containsKey("all") ? count.get("all") : 0);
             hashMap.put("sample", count.containsKey("all") ? count.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //供应商资料
             //供应商资料
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             List<Object[]> list = redDotService.getVendorCount(Role.BUYER);
             List<Object[]> list = redDotService.getVendorCount(Role.BUYER);
@@ -154,25 +187,33 @@ public class SnapshotController {
             list.forEach(objects -> count[0] = count[0] + Integer.valueOf(objects[0].toString()));
             list.forEach(objects -> count[0] = count[0] + Integer.valueOf(objects[0].toString()));
             hashMap.put("purcVendor", count[0]);
             hashMap.put("purcVendor", count[0]);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //采购招标
             //采购招标
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> count = redDotService.getTenderCount(Role.BUYER);
             Map<String, Integer> count = redDotService.getTenderCount(Role.BUYER);
             hashMap.put("tender", count.containsKey("all") ? count.get("all") : 0);
             hashMap.put("tender", count.containsKey("all") ? count.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //采购询价
             //采购询价
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> map = redDotService.getInquiryCount(Role.BUYER);
             Map<String, Integer> map = redDotService.getInquiryCount(Role.BUYER);
             hashMap.put("purcInquiry", map.containsKey("all") ? map.get("all") : 0);
             hashMap.put("purcInquiry", map.containsKey("all") ? map.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //采购订单
             //采购订单
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             Map<String, Integer> count = redDotService.getSaleOrderCount(Role.BUYER);
             Map<String, Integer> count = redDotService.getSaleOrderCount(Role.BUYER);
             hashMap.put("purcOrder", count.containsKey("all") ? count.get("all") : 0);
             hashMap.put("purcOrder", count.containsKey("all") ? count.get("all") : 0);
             SystemSession.clear();
             SystemSession.clear();
-		}).task(() -> {
+            threadsSignal.countDown();
+		});
+		executor.submitTask(() -> {
             //应收对账单
             //应收对账单
             SystemSession.setUser(user);
             SystemSession.setUser(user);
             List<Object[]> list = redDotService.getApCheckCount(Role.BUYER);
             List<Object[]> list = redDotService.getApCheckCount(Role.BUYER);
@@ -185,7 +226,9 @@ public class SnapshotController {
             });
             });
             hashMap.put("apCheck", count[0]);
             hashMap.put("apCheck", count[0]);
             SystemSession.clear();
             SystemSession.clear();
-		}).run();
+            threadsSignal.countDown();
+		});
+        threadsSignal.await();
 		modelMap.addAllAttributes(hashMap);
 		modelMap.addAllAttributes(hashMap);
         logger.info("getUnreadCount:cost -> " + (System.currentTimeMillis() - start));
         logger.info("getUnreadCount:cost -> " + (System.currentTimeMillis() - start));
 		return modelMap;
 		return modelMap;