Forráskód Böngészése

Merge branch 'master' of http://git.cc-lotus.info/sckj/yl-service

sunkuosheng 1 hónapja
szülő
commit
10ff7e5f13

+ 37 - 0
ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/utils/ReceiveService.java

@@ -0,0 +1,37 @@
+package com.ruoyi.system.utils;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.Socket;
+
+/**
+ * 接收消息
+ * @author
+ */
+public class ReceiveService implements Runnable{
+    private Socket socket;
+
+    public ReceiveService(Socket socket){
+        this.socket = socket;
+    }
+    @Override
+    public void run() {
+        InputStream inputStream;
+        InputStreamReader inputStreamReader;
+        BufferedReader bufferedReader;
+        try{
+            inputStream = socket.getInputStream();
+            inputStreamReader = new InputStreamReader(inputStream);
+            bufferedReader = new BufferedReader(inputStreamReader);
+            String result = "";
+            while((result = bufferedReader.readLine()) != null){
+                System.out.println("【【【【【【【【【【消息1:" + result);
+                int x = Integer.parseInt(result,16);
+                System.out.println("【【【【【【【【【【消息2:" + x);
+            }
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+    }
+}

+ 85 - 0
ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/utils/SocketServer.java

@@ -0,0 +1,85 @@
+package com.ruoyi.system.utils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+public class SocketServer {
+    // socket
+    ServerSocket serverSocket;
+    Socket socket;
+    ThreadPoolExecutor poolExecutor;
+    ArrayList<Socket> sockets = new ArrayList<>();
+    /**
+     * 线程数
+     */
+    private int poolSize = 5;
+    /**
+     * 线程最大容量
+     */
+    private int maxPoolSize = 5000;
+    /**
+     * 线程等待时间
+     */
+    private int keepAliveTime = 200;
+    private int queueSize = maxPoolSize - poolSize;
+
+    public SocketServer() {
+        poolExecutor = new ThreadPoolExecutor(poolSize, maxPoolSize, keepAliveTime, TimeUnit.MILLISECONDS,
+                new ArrayBlockingQueue<>(queueSize), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
+        try {
+            //1、绑定端口之前应先设置相应参数,否则设置不起作用
+            serverSocket = new ServerSocket();
+            //启用端口重用
+            serverSocket.setReuseAddress(true);
+            serverSocket.bind(new InetSocketAddress("121.36.73.159", 8098));
+        }catch(Exception e){
+            e.printStackTrace();
+        }
+    }
+
+    public class AcceptSocket implements Runnable{
+        @Override
+        public void run() {
+            try {
+                /**
+                 * 试试去掉while看能不能发送消息,实验证明去掉只能发送消息给第一个客户端,故不能去掉,否则创建连接时,除了第一个客户端后面的客户端将不会执行到括弧中内容,
+                 * 但是加上之后,从服务端发送消息发送不出去了
+                 */
+                while(true){
+                    socket = serverSocket.accept();
+                    sockets.add(socket);
+                    System.out.println("【【【【【【【【【【创建新的连接"+ socket.getPort());
+                    System.out.println("【【【【【【【【【【当前服务端线程数:"+poolExecutor.getPoolSize());
+                    //接收消息
+                    poolExecutor.execute(new ReceiveService(socket));
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    public void start(){
+        System.out.println("【【【【【【【【【【服务器启动....");
+        System.out.println("【【【【【【【【【【当前服务端线程数:"+poolExecutor.getPoolSize());
+        try{
+            poolExecutor.execute(new AcceptSocket());
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+    }
+
+//    public static void main(String[] args) {
+//        new SocketServer().start();
+//    }
+}

+ 7 - 0
ruoyi-modules/mz-system-ext/src/main/java/com/ruoyi/system/service/impl/SysDeptJlServiceImpl.java

@@ -7,6 +7,7 @@ import com.ruoyi.common.security.utils.SysDeptJlUtils;
 import com.ruoyi.system.domain.SysDeptJl;
 import com.ruoyi.system.mapper.SysDeptJlMapper;
 import com.ruoyi.system.service.ISysDeptJlService;
+import com.ruoyi.system.utils.SocketServer;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -298,6 +299,12 @@ public class SysDeptJlServiceImpl implements ISysDeptJlService {
         loadingDeptCache();
     }
 
+    @PostConstruct
+    public void initSocket()
+    {
+        new SocketServer().start();
+    }
+
     @Override
     public void loadingDeptCache()
     {