소스 검색

1.优化推送—— qlm

qlm 4 년 전
부모
커밋
9eef87bc85

+ 16 - 1
src/main/java/com/zx/dataservice/controller/CallBackController.java

@@ -1,5 +1,6 @@
 package com.zx.dataservice.controller;
 
+import com.alibaba.fastjson.JSON;
 import com.jfinal.plugin.activerecord.Db;
 import com.jfinal.plugin.activerecord.Record;
 import com.zx.dataservice.utils.ZXOptions;
@@ -52,7 +53,7 @@ public class CallBackController {
 //                .addHeader("Content-Type", "application/x-www-form-urlencoded")
 //                .build();
 //        String str = ConnectionFactory.execute(executeRequest);
-        return s1;
+        return "true";
     }
 
 
@@ -125,5 +126,19 @@ public class CallBackController {
         return true;
     }
 
+    @GetMapping("/testis")
+    @ResponseBody
+    public boolean testis(String key){
+        try {
+            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+            for (int j = 0; j < webArr.length; j++) {
+                new WebSocketController().sendSysMsg(webArr[j],key);
+            }
+        } catch (Exception e) {
+            if(e.toString().length()>500){                LOG.error(e.toString().substring(0,450));            }else {                LOG.error(e.toString());            }
+        }
+        return true;
+    }
+
 
 }

+ 58 - 7
src/main/java/com/zx/dataservice/controller/WebSocketController.java

@@ -4,17 +4,22 @@ package com.zx.dataservice.controller;
  * Created by qlm on 2020-12-14.
  */
 
+import com.zx.dataservice.utils.DateUtils;
 import org.springframework.stereotype.Component;
 
 import javax.websocket.*;
 import javax.websocket.server.PathParam;
 import javax.websocket.server.ServerEndpoint;
 import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
-
+import java.lang.reflect.Field;
+import java.net.InetSocketAddress;
+import javax.websocket.RemoteEndpoint.Async;
 
 @ServerEndpoint("/websocket/{userId}")
 @Component
@@ -24,6 +29,47 @@ public class WebSocketController {
 
     private static ConcurrentHashMap<String, Session> map = new ConcurrentHashMap<>();//根据用户找session
 
+
+    public static InetSocketAddress getRemoteAddress(Session session) {
+        if (session == null) {
+            return null;
+        }
+        Async async = session.getAsyncRemote();
+
+        //在Tomcat 8.0.x版本有效
+//		InetSocketAddress addr = (InetSocketAddress) getFieldInstance(async,"base#sos#socketWrapper#socket#sc#remoteAddress");
+        //在Tomcat 8.5以上版本有效
+        InetSocketAddress addr = (InetSocketAddress) getFieldInstance(async,"base#socketWrapper#socket#sc#remoteAddress");
+        return addr;
+    }
+
+
+    private static Object getFieldInstance(Object obj, String fieldPath) {
+        String fields[] = fieldPath.split("#");
+        for (String field : fields) {
+            obj = getField(obj, obj.getClass(), field);
+            if (obj == null) {
+                return null;
+            }
+        }
+
+        return obj;
+    }
+
+    private static Object getField(Object obj, Class<?> clazz, String fieldName) {
+        for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
+            try {
+                Field field;
+                field = clazz.getDeclaredField(fieldName);
+                field.setAccessible(true);
+                return field.get(obj);
+            } catch (Exception e) {
+            }
+        }
+
+        return null;
+    }
+
     /**
      * 连接建立成功时调用
      * @param userId 登录用户id
@@ -33,11 +79,11 @@ public class WebSocketController {
     @OnOpen
     public void onOpen(Session session, @PathParam("userId") String userId) throws IOException{
         String[] a = userId.split("&");
-
         String mUserId = a[0];
         map.put(mUserId + "",session);       //添加到链接map
         try{
-            System.out.println(mUserId+"连接成功");
+            InetSocketAddress remoteAddress = getRemoteAddress(session);
+            System.out.println(remoteAddress+DateUtils.getCurrentTimeSSS() +mUserId+"连接成功");
             sendUnreadMsg(session);
         }
         catch (Exception e){
@@ -52,7 +98,7 @@ public class WebSocketController {
             synchronized(session){
                 if(session.isOpen()){
                     try {
-                        session.getBasicRemote().sendText("连接成功");
+                        session.getBasicRemote().sendText("testws"+DateUtils.getCurrentTimeSSS()+"连接成功");
                     }catch (Exception e) {
                         e.printStackTrace();
                     }
@@ -105,8 +151,10 @@ public class WebSocketController {
      */
     @OnMessage
     public void onMessage(String requestJson, Session session) {
-
-        System.out.println("啦啦啦");
+        System.out.println(requestJson);
+        if("close".equals(requestJson)){
+            onClose(session);
+        }
 
     }
 
@@ -119,9 +167,12 @@ public class WebSocketController {
             // 数据服务器 id   :
 
             Session session = map.get( id);               //发送消息给对方
+
             if(session!=null&&session.isOpen()){
                 synchronized(session){
-                    session.getBasicRemote().sendText(xMessage);
+                    if(session.isOpen()){
+                        session.getBasicRemote().sendText(xMessage);
+                    }
                 }
             }
         }catch(IOException e) {

+ 20 - 12
src/main/java/com/zx/dataservice/service/impl/ChoiceBondServiceImpl.java

@@ -384,9 +384,11 @@ public class ChoiceBondServiceImpl implements ChoiceBondService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try{
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){
@@ -408,9 +410,11 @@ public class ChoiceBondServiceImpl implements ChoiceBondService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){
@@ -440,9 +444,11 @@ public class ChoiceBondServiceImpl implements ChoiceBondService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                         try{
-                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                            for (int j = 0; j < webArr.length; j++) {
-                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            if(ZXOptions.get("ws.key")!=null){
+                                final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                                for (int j = 0; j < webArr.length; j++) {
+                                    new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                                }
                             }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                         }catch (Exception e){
@@ -463,9 +469,11 @@ public class ChoiceBondServiceImpl implements ChoiceBondService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){

+ 30 - 18
src/main/java/com/zx/dataservice/service/impl/ChoiceStockServiceImpl.java

@@ -383,9 +383,11 @@ public class ChoiceStockServiceImpl implements ChoiceStockService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){
@@ -407,9 +409,11 @@ public class ChoiceStockServiceImpl implements ChoiceStockService {
 //                   HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){
@@ -442,9 +446,11 @@ public class ChoiceStockServiceImpl implements ChoiceStockService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){
@@ -467,9 +473,11 @@ public class ChoiceStockServiceImpl implements ChoiceStockService {
 //                   HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){
@@ -661,9 +669,11 @@ public class ChoiceStockServiceImpl implements ChoiceStockService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){
@@ -686,9 +696,11 @@ public class ChoiceStockServiceImpl implements ChoiceStockService {
 //                   HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){

+ 20 - 12
src/main/java/com/zx/dataservice/service/impl/ChoiceTempIndexServiceImpl.java

@@ -377,9 +377,11 @@ public class ChoiceTempIndexServiceImpl implements ChoiceTempIndexService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){
@@ -401,9 +403,11 @@ public class ChoiceTempIndexServiceImpl implements ChoiceTempIndexService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){
@@ -436,9 +440,11 @@ public class ChoiceTempIndexServiceImpl implements ChoiceTempIndexService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){
@@ -463,9 +469,11 @@ public class ChoiceTempIndexServiceImpl implements ChoiceTempIndexService {
 //                    HttpRequest.httpPostWithjson(url, JSON.toJSONString(subList));
 //                    if(BuyAndSellUtils.isTransState()){
                     try {
-                        final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
-                        for (int j = 0; j < webArr.length; j++) {
-                            new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                        if(ZXOptions.get("ws.key")!=null){
+                            final String[] webArr = ZXOptions.get("ws.key").split(";");//websocket key
+                            for (int j = 0; j < webArr.length; j++) {
+                                new WebSocketController().sendSysMsg(webArr[j],JSON.toJSONString(subRedisList));
+                            }
                         }
 //                            HttpRequest.httpPostWithjson(urlRedis, JSON.toJSONString(subRedisList));
                     }catch (Exception e){

+ 1 - 1
src/main/java/com/zx/dataservice/utils/FileBondUtil.java

@@ -80,7 +80,7 @@ public class FileBondUtil {
                             fileNameSet = new HashSet<>();
                         }
 //                        return files;
-                        if(10 == files.size()) {
+                        if(40 == files.size()) {
                             return files;
                         }
                     }

+ 1 - 1
src/main/java/com/zx/dataservice/utils/FileStockUtil.java

@@ -80,7 +80,7 @@ public class FileStockUtil {
                             fileNameSet = new HashSet<>();
                         }
 //                        return files;
-                        if(10 == files.size()) {
+                        if(40 == files.size()) {
                             return files;
                         }
                     }

+ 4 - 1
src/main/java/com/zx/dataservice/utils/FileTempIndexUtil.java

@@ -79,7 +79,10 @@ public class FileTempIndexUtil {
                         if(10000 < fileNameSet.size()){
                             fileNameSet = new HashSet<>();
                         }
-                        return files;
+                        if(40 == files.size()) {
+                            return files;
+                        }
+//                        return files;
                     }
                 }
             }