Explorar el Código

线程池修改

hanqingsong hace 4 años
padre
commit
8875964720

+ 2 - 0
src/main/java/com/chinaitop/depot/intelligent/grainsituation/mapper/TGasDataMapper.xml

@@ -413,9 +413,11 @@
413 413
                 and store_code = #{storeCode}
414 414
               </if>
415 415
             </where>
416
+            GROUP BY store_code
416 417
         ) tg
417 418
     INNER JOIN t_gas_data tgd ON tg.result_time = tgd.result_time
418 419
     AND tg.org_id = tgd.org_id
419 420
     AND tg.store_code = tgd.store_code
421
+    ORDER BY tgd.store_code
420 422
   </select>
421 423
 </mapper>

+ 19 - 24
src/main/java/com/chinaitop/depot/intelligent/grainsituation/service/impl/GasDetectionServiceImpl.java

@@ -1,9 +1,6 @@
1 1
 package com.chinaitop.depot.intelligent.grainsituation.service.impl;
2 2
 
3
-import com.chinaitop.depot.intelligent.basicdata.mapper.TDcsqtinfoMapper;
4 3
 import com.chinaitop.depot.intelligent.basicdata.mapper.TQtdevinfoMapper;
5
-import com.chinaitop.depot.intelligent.basicdata.model.TDcsqtinfo;
6
-import com.chinaitop.depot.intelligent.basicdata.model.TDcsqtinfoExample;
7 4
 import com.chinaitop.depot.intelligent.basicdata.model.TQtdevinfo;
8 5
 import com.chinaitop.depot.intelligent.basicdata.model.TQtdevinfoExample;
9 6
 import com.chinaitop.depot.intelligent.common.ConstantClass;
@@ -20,15 +17,13 @@ import com.chinaitop.depot.intelligent.utils.UuidUtils;
20 17
 import com.google.common.collect.Lists;
21 18
 import com.google.common.collect.Maps;
22 19
 import org.apache.commons.lang3.StringUtils;
23
-import org.springframework.beans.factory.annotation.Autowired;
24 20
 import org.springframework.stereotype.Service;
25 21
 import org.springframework.transaction.annotation.Transactional;
26
-import reactor.util.annotation.Nullable;
27 22
 
28 23
 import javax.annotation.Resource;
29 24
 import java.util.*;
30 25
 
31
-@Service
26
+@Service("gasDetectionService")
32 27
 public class GasDetectionServiceImpl implements GasDetectionService {
33 28
     @Resource
34 29
     private TQtdevinfoValueMapper tQtdevinfoValueMapper;
@@ -121,6 +116,7 @@ public class GasDetectionServiceImpl implements GasDetectionService {
121 116
             Date endDate = DateUtils.stringToData(stringFactory.joinString(1, endTime, DateUtils.endTime));
122 117
             criteria.andResultTimeBetween(startDate, endDate);
123 118
         }
119
+        example.setOrderByClause("result_time desc");
124 120
         return gasDataMapper.selectByExample(example);
125 121
     }
126 122
 
@@ -141,29 +137,28 @@ public class GasDetectionServiceImpl implements GasDetectionService {
141 137
             String storeCode = gasConfig.getStoreCode();
142 138
             // 数据list
143 139
             ArrayList<TGasData> gases = Lists.newArrayList();
144
-            objects1.forEach(data -> {
145
-                if (data.size() > 12) {
146
-                    TGasData gasData = new TGasData();
147
-                    gasData.setNowO2(data.get(0)); // 氧气实时
148
-                    gasData.setNowPh3(data.get(1)); // 磷化氢实时
149
-                    gasData.setSouthO2(data.get(2)); // 南检测结果 氧气
150
-                    gasData.setSouthPh3(data.get(3)); // 南检测结果 磷化氢
151
-                    gasData.setEastO2(data.get(4)); // 东检测结果 氧气
152
-                    gasData.setEastPh3(data.get(5)); // 东检测结果 磷化氢
153
-                    gasData.setNorthO2(data.get(6)); // 北检测结果 氧气
154
-                    gasData.setNorthPh3(data.get(7)); // 北检测结果 磷化氢
155
-                    gasData.setWestO2(data.get(8)); // 西检测结果 氧气
156
-                    gasData.setWestPh3(data.get(9)); // 西检测结果 磷化氢
157
-                    gasData.setInO2(data.get(10)); // 中检测结果 氧气
158
-                    gasData.setInPh3(data.get(11)); // 中检测结果 磷化氢
159
-                    gases.add(gasData);
160
-                }
161
-            });
140
+            for (List<Float> data : objects1) {
141
+                TGasData gasData = new TGasData();
142
+                gasData.setNowPh3(data.get(0)); // 磷化氢实时
143
+                gasData.setNowO2(data.get(1)); // 氧气实时
144
+                gasData.setSouthO2(data.get(2)); // 南检测结果 氧气
145
+                gasData.setSouthPh3(data.get(3)); // 南检测结果 磷化氢
146
+                gasData.setEastO2(data.get(4)); // 东检测结果 氧气
147
+                gasData.setEastPh3(data.get(5)); // 东检测结果 磷化氢
148
+                gasData.setNorthO2(data.get(6)); // 北检测结果 氧气
149
+                gasData.setNorthPh3(data.get(7)); // 北检测结果 磷化氢
150
+                gasData.setWestO2(data.get(8)); // 西检测结果 氧气
151
+                gasData.setWestPh3(data.get(9)); // 西检测结果 磷化氢
152
+                gasData.setInO2(data.get(10)); // 中检测结果 氧气
153
+                gasData.setInPh3(data.get(11)); // 中检测结果 磷化氢
154
+                gases.add(gasData);
155
+            }
162 156
             // 保存对象
163 157
             TGasData saveOnlyGasData = new TGasData();
164 158
             saveOnlyGasData.setId(UuidUtils.getCode()); // id
165 159
             saveOnlyGasData.setOrgId(orgId); // 组织机构编码
166 160
             saveOnlyGasData.setStoreCode(storeCode); // 仓房编码
161
+            saveOnlyGasData.setResultTime(DateUtils.getDate());
167 162
             // 计算平均值
168 163
             float avgNowO2 = (float) gases.stream().mapToDouble(TGasData::getNowO2).average().getAsDouble();
169 164
             float avgNowPh3 = (float) gases.stream().mapToDouble(TGasData::getNowPh3).average().getAsDouble();

+ 13 - 15
src/main/java/com/chinaitop/depot/intelligent/socket/ServerThread.java

@@ -2,6 +2,7 @@ package com.chinaitop.depot.intelligent.socket;
2 2
 
3 3
 import com.alibaba.fastjson.JSON;
4 4
 import com.chinaitop.depot.intelligent.grainsituation.service.GasDetectionService;
5
+import com.chinaitop.depot.intelligent.utils.GetBeanUtil;
5 6
 import com.google.common.collect.Lists;
6 7
 import org.slf4j.Logger;
7 8
 import org.slf4j.LoggerFactory;
@@ -21,8 +22,6 @@ public class ServerThread implements Runnable {
21 22
     private final static Logger log = LoggerFactory.getLogger(ServerThread.class);
22 23
 
23 24
     private Socket socket;
24
-    @Resource
25
-    private GasDetectionService gasDetectionService;
26 25
 
27 26
     ServerThread(Socket socket) {
28 27
         this.socket = socket;
@@ -30,17 +29,17 @@ public class ServerThread implements Runnable {
30 29
 
31 30
     @Override
32 31
     public void run() {
33
-        InputStream inputStream = null; // 字节输入流
32
+//        InputStream inputStream = null; // 字节输入流
34 33
         try {
35 34
             synchronized (this) {
36
-                inputStream = socket.getInputStream();
35
+                InputStream inputStream = socket.getInputStream();
37 36
                 int read; // 读取数据变量
38 37
                 // 缓存返回数据
39 38
                 Integer[] data = null; // 动态扩容数组初始化
40
-                // 获取5983次实时数据,初始化
39
+                // 获取5967次实时数据,初始化
41 40
                 int count = 0;
42
-                // 获取数据次数位 117位*51+16 为一组数据
43
-                int whileCount = 0X175F;
41
+                // 获取数据次数位 117位*51 为一组数据
42
+                int whileCount = 0X174F;
44 43
                 //循环读取客户端信息
45 44
                 while (true) {
46 45
                     count++;
@@ -48,7 +47,7 @@ public class ServerThread implements Runnable {
48 47
                     read = inputStream.read();
49 48
                     //获取客户端的ip地址及发送数据
50 49
 //                    System.out.println("服务器端接收:" + "{'from_client':'" + socket.getInetAddress().getHostAddress() + "','data':'" + read + "'}");
51
-//                    System.out.println("服务器端接收:" + read);
50
+//                    System.out.println(read);
52 51
 
53 52
                     // 动态扩容数组
54 53
                     if (data == null) { //若数组为空,定义数组的长度为1
@@ -94,9 +93,9 @@ public class ServerThread implements Runnable {
94 93
         } finally {
95 94
             //关闭资源
96 95
             try {
97
-                if (inputStream != null) {
96
+                /*if (inputStream != null) {
98 97
                     inputStream.close();
99
-                }
98
+                }*/
100 99
                 if (socket != null) {
101 100
                     socket.close();
102 101
                 }
@@ -107,16 +106,14 @@ public class ServerThread implements Runnable {
107 106
     }
108 107
 
109 108
     private void gasDataHandle(Integer[] data, String ip) {
110
-        // 1.将数据前16位排除
111
-        Integer[] integers = Arrays.copyOfRange(data, 16, data.length);
112 109
         // 2.将数据分成51组,每组117个位
113 110
         int bitFromData = 0;
114 111
         int bitToData = 117;
115 112
         Integer[] groupData;
116 113
         List<Integer[]> list = new ArrayList<>();
117
-        for (int i = 0; i < integers.length; i++) {
118
-            if (i % 116 == 0 && bitToData <= integers.length) {
119
-                groupData = Arrays.copyOfRange(integers, bitFromData, bitToData);
114
+        for (int i = 0; i < data.length; i++) {
115
+            if (i % 116 == 0 && bitToData <= data.length) {
116
+                groupData = Arrays.copyOfRange(data, bitFromData, bitToData);
120 117
                 bitFromData += 117;
121 118
                 bitToData += 117;
122 119
                 list.add(groupData);
@@ -163,6 +160,7 @@ public class ServerThread implements Runnable {
163 160
         });
164 161
         log.info("处理后的PLC测气返回数据: {}", JSON.toJSON(objects1));
165 162
         // 将数据保存到数据库
163
+        GasDetectionService gasDetectionService = (GasDetectionService) GetBeanUtil.getBean("gasDetectionService");
166 164
         gasDetectionService.saveGasData(objects1, ip);
167 165
     }
168 166
 

+ 13 - 3
src/main/java/com/chinaitop/depot/intelligent/socket/SocketServer.java

@@ -40,6 +40,8 @@ public class SocketServer {
40 40
     private static final int tempCount = 0x40;
41 41
     // 本类注入ServerSocket
42 42
     private ServerSocket serverSocket;
43
+    private ExecutorService executorService;//线程池
44
+    private final int PoolSize = 8;//单个CPU线程池大小
43 45
 
44 46
     public SocketServer() throws IOException {
45 47
         /*
@@ -54,6 +56,9 @@ public class SocketServer {
54 56
          */
55 57
         InetAddress localHost = InetAddress.getLocalHost(); // 获取本机ip(监听地址为服务器地址需要PLC修改)
56 58
         serverSocket = new ServerSocket(port, backlog, localHost); // 连接请求队列的长度为3
59
+        // //Runtime的availableProcessor()方法返回当前系统的CPU数目
60
+        int i = Runtime.getRuntime().availableProcessors();
61
+        executorService = Executors.newFixedThreadPool(i * PoolSize);
57 62
         log.info("SocketServer服务器启动!");
58 63
     }
59 64
 
@@ -64,7 +69,7 @@ public class SocketServer {
64 69
      *             TODO 仓上控制柜 1:测气,0:自然窗,0:轴流窗,0:轴流风机,
65 70
      *             TODO 仓下控制柜 0:通风口
66 71
      */
67
-    public String service(String ip, String orders, int type) {
72
+    public /*synchronized*/ String service(String ip, String orders, int type) {
68 73
         String resultPLC = "";
69 74
         Socket socket;
70 75
         try {
@@ -92,10 +97,15 @@ public class SocketServer {
92 97
                     }
93 98
                     if (type == 1 && !orders.equals(ConstantClass.TYPE_ON)) {
94 99
                         // 创建线程池对象 // TODO 优化方案 1.多线程使用线程池,防止服务器宕机
95
-                        ExecutorService executorService = Executors.newFixedThreadPool(8); // 创建固定大小线程池
100
+
101
+                        /*ExecutorService executorService = Executors.newFixedThreadPool(16); // 创建固定大小线程池
96 102
                         ServerThread serverThread = new ServerThread(socket);
97 103
                         // 启动线程等待返回(测气)
98
-                        executorService.submit(serverThread);
104
+                        executorService.submit(serverThread);*/
105
+
106
+//                        executorService.execute(new ServerThread(socket));
107
+                        executorService.submit(new ServerThread(socket));
108
+
99 109
 //                        new Thread(new ServerThread(socket)).start(); // 启动线程等待返回(测气)
100 110
                         resultPLC = ConstantClass.C_SOH_S;
101 111
                     } else {

+ 27 - 0
src/main/java/com/chinaitop/depot/intelligent/utils/GetBeanUtil.java

@@ -0,0 +1,27 @@
1
+package com.chinaitop.depot.intelligent.utils;
2
+
3
+import org.springframework.context.ApplicationContext;
4
+import org.springframework.context.ApplicationContextAware;
5
+import org.springframework.stereotype.Component;
6
+
7
+/**
8
+ * @author qingsong.han
9
+ * @description:
10
+ * @create 2021-10-17 12:40
11
+ */
12
+@Component
13
+public class GetBeanUtil implements ApplicationContextAware {
14
+    private static ApplicationContext applicationContext;
15
+
16
+    public void setApplicationContext(ApplicationContext context) {
17
+        GetBeanUtil.applicationContext = context;
18
+    }
19
+
20
+    public static Object getBean(String name) {
21
+        return applicationContext.getBean(name);
22
+    }
23
+
24
+    public static ApplicationContext getApplicationContext() {
25
+        return applicationContext;
26
+    }
27
+}