瀏覽代碼

关闭WebSocket服务。

lighter 4 年之前
父節點
當前提交
337fc9e181

+ 1 - 1
pom.xml

@@ -10,7 +10,7 @@
     </parent>
     <groupId>thyyxxk</groupId>
     <artifactId>wxservice-server</artifactId>
-    <version>5.6</version>
+    <version>5.8</version>
     <name>wxservice-server</name>
     <description>server for wxservice-web</description>
 

+ 3 - 0
src/main/java/thyyxxk/wxservice_server/WxserviceServerApplication.java

@@ -8,6 +8,9 @@ import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
 import thyyxxk.wxservice_server.utils.PropertiesUtil;
 
+/**
+ * @author dj
+ */
 @EnableTransactionManagement
 @EnableRetry
 @EnableScheduling

+ 36 - 36
src/main/java/thyyxxk/wxservice_server/controller/WebSocketController.java

@@ -1,36 +1,36 @@
-package thyyxxk.wxservice_server.controller;
-
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-import thyyxxk.wxservice_server.entity.ResultVo;
-import thyyxxk.wxservice_server.utils.ResultVoUtil;
-import thyyxxk.wxservice_server.websocket.WebSocketServer;
-
-/**
- * @description: websocket控制台
- * @author: DingJie
- * @create: 2021-05-18 11:40:31
- **/
-@RestController
-@RequestMapping("/webSocket")
-public class WebSocketController {
-
-    @GetMapping("/getOnlineCount")
-    public ResultVo<String> getOnlineCount() {
-        String data = "当前在线人数:" + WebSocketServer.getOnlineCount();
-        return ResultVoUtil.success(data);
-    }
-
-    @GetMapping("/systemBreak")
-    public ResultVo<String> systemBreak() {
-        int count = WebSocketServer.systemBreak();
-        return ResultVoUtil.success("操作成功,成功关闭连接数:" + count);
-    }
-
-    @GetMapping("/systemRecover")
-    public ResultVo<String> systemRecover() {
-        WebSocketServer.systemRecover();
-        return ResultVoUtil.success("操作成功,WebSocket服务已恢复。");
-    }
-}
+//package thyyxxk.wxservice_server.controller;
+//
+//import org.springframework.web.bind.annotation.GetMapping;
+//import org.springframework.web.bind.annotation.RequestMapping;
+//import org.springframework.web.bind.annotation.RestController;
+//import thyyxxk.wxservice_server.entity.ResultVo;
+//import thyyxxk.wxservice_server.utils.ResultVoUtil;
+//import thyyxxk.wxservice_server.websocket.WebSocketServer;
+//
+///**
+// * @description: websocket控制台
+// * @author: DingJie
+// * @create: 2021-05-18 11:40:31
+// **/
+//@RestController
+//@RequestMapping("/webSocket")
+//public class WebSocketController {
+//
+//    @GetMapping("/getOnlineCount")
+//    public ResultVo<String> getOnlineCount() {
+//        String data = "当前在线人数:" + WebSocketServer.getOnlineCount();
+//        return ResultVoUtil.success(data);
+//    }
+//
+//    @GetMapping("/systemBreak")
+//    public ResultVo<String> systemBreak() {
+//        int count = WebSocketServer.systemBreak();
+//        return ResultVoUtil.success("操作成功,成功关闭连接数:" + count);
+//    }
+//
+//    @GetMapping("/systemRecover")
+//    public ResultVo<String> systemRecover() {
+//        WebSocketServer.systemRecover();
+//        return ResultVoUtil.success("操作成功,WebSocket服务已恢复。");
+//    }
+//}

+ 0 - 5
src/main/java/thyyxxk/wxservice_server/controller/WxApiController.java

@@ -65,9 +65,4 @@ public class WxApiController {
     public void pushMessage(@RequestBody @Validated PushMessageParam param) {
         service.pushMessage(param);
     }
-
-    @GetMapping("/pushTest")
-    public ResultVo<String> pushTest() {
-        return service.pushTest();
-    }
 }

+ 6 - 6
src/main/java/thyyxxk/wxservice_server/service/WxApiService.java

@@ -35,7 +35,7 @@ import thyyxxk.wxservice_server.entity.wxapi.GenMzPayQrcodeParam;
 import thyyxxk.wxservice_server.entity.wxapi.PushMessageParam;
 import thyyxxk.wxservice_server.entity.wxapi.WxPayOrder;
 import thyyxxk.wxservice_server.utils.*;
-import thyyxxk.wxservice_server.websocket.WebSocketServer;
+//import thyyxxk.wxservice_server.websocket.WebSocketServer;
 
 import java.nio.charset.StandardCharsets;
 import java.util.*;
@@ -248,7 +248,7 @@ public class WxApiService {
         SaveGhFeeResponse data = template.postForObject(hrgApiUrl + "/payRegistrationFormHaiCi", param, SaveGhFeeResponse.class);
         assert data != null;
         log.info("保存挂号信息: 参数:{},结果:{}", param, data);
-        if (data.getResultCode() == 0) {
+        if (data.getResultCode() == 0 || data.getMessage().contains("挂号成功")) {
             return ResultVoUtil.success("保存挂号信息成功。");
         }
         log.info("保存挂号信息失败,将自动退款。");
@@ -483,10 +483,10 @@ public class WxApiService {
             content.replace("touser", openId);
             String res = template.postForObject(wxUrl, content, String.class);
             log.info("推送消息:内容:{},结果:{}", content, res);
-            JSONObject resObj = JSONObject.parseObject(res);
-            if (resObj.getIntValue("errcode") == 0) {
-                WebSocketServer.sendMessage(openId, "您有一条新的本院公众号消息。");
-            }
+//            JSONObject resObj = JSONObject.parseObject(res);
+//            if (resObj.getIntValue("errcode") == 0) {
+//                WebSocketServer.sendMessage(openId, "您有一条新的本院公众号消息。");
+//            }
         }
     }
 

+ 18 - 18
src/main/java/thyyxxk/wxservice_server/websocket/WebSocketConfig.java

@@ -1,18 +1,18 @@
-package thyyxxk.wxservice_server.websocket;
-
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.web.socket.server.standard.ServerEndpointExporter;
-
-/**
- * @description: websocket config
- * @author: DingJie
- * @create: 2021-05-18 11:20:06
- **/
-@Configuration
-public class WebSocketConfig {
-    @Bean
-    public ServerEndpointExporter serverEndpointExporter() {
-        return new ServerEndpointExporter();
-    }
-}
+//package thyyxxk.wxservice_server.websocket;
+//
+//import org.springframework.context.annotation.Bean;
+//import org.springframework.context.annotation.Configuration;
+//import org.springframework.web.socket.server.standard.ServerEndpointExporter;
+//
+///**
+// * @description: websocket config
+// * @author: DingJie
+// * @create: 2021-05-18 11:20:06
+// **/
+//@Configuration
+//public class WebSocketConfig {
+//    @Bean
+//    public ServerEndpointExporter serverEndpointExporter() {
+//        return new ServerEndpointExporter();
+//    }
+//}

+ 110 - 110
src/main/java/thyyxxk/wxservice_server/websocket/WebSocketServer.java

@@ -1,110 +1,110 @@
-package thyyxxk.wxservice_server.websocket;
-
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.stereotype.Component;
-import thyyxxk.wxservice_server.constant.Constants;
-
-import javax.websocket.*;
-import javax.websocket.server.PathParam;
-import javax.websocket.server.ServerEndpoint;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicInteger;
-
-/**
- * @description: websocket config
- * @author: DingJie
- * @create: 2021-05-18 11:20:43
- **/
-@Slf4j
-@ServerEndpoint("/websocket/{openId}")
-@Component
-public class WebSocketServer {
-    private static final AtomicInteger ONLINE_COUNT = new AtomicInteger();
-    private static ConcurrentHashMap<String, Session> SESSION_MAP = new ConcurrentHashMap<>(Constants.Capacity.DEFAULT);
-
-    public static void sendMessage(String openId, String message) {
-        if (null == SESSION_MAP) {
-            return;
-        }
-        Session session = SESSION_MAP.get(openId);
-        if (null == session) {
-            return;
-        }
-        try {
-            session.getBasicRemote().sendText(message);
-        } catch (Exception e) {
-            log.error("发送socket消息出错。【openId: {},message: {}, exception: {}】", openId, message, e.getMessage());
-            e.printStackTrace();
-        }
-    }
-
-    public static int getOnlineCount() {
-        return ONLINE_COUNT.get();
-    }
-
-    public static int systemBreak() {
-        int count = 0;
-        if (null == SESSION_MAP) {
-            return count;
-        }
-        for (Map.Entry<String, Session> entry : SESSION_MAP.entrySet()) {
-            if (entry.getValue().isOpen()) {
-                try {
-                    CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
-                    entry.getValue().close(closeReason);
-                    count++;
-                } catch (Exception e) {
-                    log.error("关闭WebSocket发生错误>>> session: {},错误:{}", entry.getValue().getId(), e.getMessage());
-                }
-            }
-        }
-        SESSION_MAP = null;
-        ONLINE_COUNT.set(0);
-        return count;
-    }
-
-    public static void systemRecover() {
-        if (null == SESSION_MAP) {
-            SESSION_MAP = new ConcurrentHashMap<>(Constants.Capacity.DEFAULT);
-        }
-    }
-
-    @OnOpen
-    public void onOpen(Session session, @PathParam("openId") String openId) {
-        if (null == SESSION_MAP) {
-            if (session.isOpen()) {
-                try {
-                    CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
-                    session.close(closeReason);
-                } catch (Exception e) {
-                    log.error("关闭WebSocket发生错误>>> session: {},错误:{}", session.getId(), e.getMessage());
-                }
-            }
-        } else {
-            SESSION_MAP.put(openId, session);
-            ONLINE_COUNT.incrementAndGet();
-        }
-    }
-
-    @OnClose
-    public void onClose(@PathParam("openId") String openId) {
-        if (null != SESSION_MAP) {
-            SESSION_MAP.remove(openId);
-        }
-        ONLINE_COUNT.decrementAndGet();
-    }
-
-    @OnError
-    public void onError(Session session, Throwable error) {
-        log.error("WebSocket发生错误>>> session: {},错误:{} ", session.getId(), error.getMessage());
-        if (session.isOpen()) {
-            try {
-                CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
-                session.close(closeReason);
-            } catch (Exception e) {
-                log.error("关闭WebSocket发生错误>>> session: {},错误:{}", session.getId(), e.getMessage());
-            }
-        }
-    }
-}
+//package thyyxxk.wxservice_server.websocket;
+//
+//import lombok.extern.slf4j.Slf4j;
+//import org.springframework.stereotype.Component;
+//import thyyxxk.wxservice_server.constant.Constants;
+//
+//import javax.websocket.*;
+//import javax.websocket.server.PathParam;
+//import javax.websocket.server.ServerEndpoint;
+//import java.util.Map;
+//import java.util.concurrent.ConcurrentHashMap;
+//import java.util.concurrent.atomic.AtomicInteger;
+//
+///**
+// * @description: websocket config
+// * @author: DingJie
+// * @create: 2021-05-18 11:20:43
+// **/
+//@Slf4j
+//@ServerEndpoint("/websocket/{openId}")
+//@Component
+//public class WebSocketServer {
+//    private static final AtomicInteger ONLINE_COUNT = new AtomicInteger();
+//    private static ConcurrentHashMap<String, Session> SESSION_MAP = new ConcurrentHashMap<>(Constants.Capacity.DEFAULT);
+//
+//    public static void sendMessage(String openId, String message) {
+//        if (null == SESSION_MAP) {
+//            return;
+//        }
+//        Session session = SESSION_MAP.get(openId);
+//        if (null == session) {
+//            return;
+//        }
+//        try {
+//            session.getBasicRemote().sendText(message);
+//        } catch (Exception e) {
+//            log.error("发送socket消息出错。【openId: {},message: {}, exception: {}】", openId, message, e.getMessage());
+//            e.printStackTrace();
+//        }
+//    }
+//
+//    public static int getOnlineCount() {
+//        return ONLINE_COUNT.get();
+//    }
+//
+//    public static int systemBreak() {
+//        int count = 0;
+//        if (null == SESSION_MAP) {
+//            return count;
+//        }
+//        for (Map.Entry<String, Session> entry : SESSION_MAP.entrySet()) {
+//            if (entry.getValue().isOpen()) {
+//                try {
+//                    CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
+//                    entry.getValue().close(closeReason);
+//                    count++;
+//                } catch (Exception e) {
+//                    log.error("关闭WebSocket发生错误>>> session: {},错误:{}", entry.getValue().getId(), e.getMessage());
+//                }
+//            }
+//        }
+//        SESSION_MAP = null;
+//        ONLINE_COUNT.set(0);
+//        return count;
+//    }
+//
+//    public static void systemRecover() {
+//        if (null == SESSION_MAP) {
+//            SESSION_MAP = new ConcurrentHashMap<>(Constants.Capacity.DEFAULT);
+//        }
+//    }
+//
+//    @OnOpen
+//    public void onOpen(Session session, @PathParam("openId") String openId) {
+//        if (null == SESSION_MAP) {
+//            if (session.isOpen()) {
+//                try {
+//                    CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
+//                    session.close(closeReason);
+//                } catch (Exception e) {
+//                    log.error("关闭WebSocket发生错误>>> session: {},错误:{}", session.getId(), e.getMessage());
+//                }
+//            }
+//        } else {
+//            SESSION_MAP.put(openId, session);
+//            ONLINE_COUNT.incrementAndGet();
+//        }
+//    }
+//
+//    @OnClose
+//    public void onClose(@PathParam("openId") String openId) {
+//        if (null != SESSION_MAP) {
+//            SESSION_MAP.remove(openId);
+//        }
+//        ONLINE_COUNT.decrementAndGet();
+//    }
+//
+//    @OnError
+//    public void onError(Session session, Throwable error) {
+//        log.error("WebSocket发生错误>>> session: {},错误:{} ", session.getId(), error.getMessage());
+//        if (session.isOpen()) {
+//            try {
+//                CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "服务更新。");
+//                session.close(closeReason);
+//            } catch (Exception e) {
+//                log.error("关闭WebSocket发生错误>>> session: {},错误:{}", session.getId(), e.getMessage());
+//            }
+//        }
+//    }
+//}

+ 1 - 1
src/main/resources/application-prod.yml

@@ -1,5 +1,5 @@
 server:
-  port: 8085
+  port: 8083
   servlet:
     context-path: /wxserver
 spring:

+ 2 - 1
src/test/java/thyyxxk/wxservice_server/WxserviceServerApplicationTests.java

@@ -3,7 +3,8 @@ package thyyxxk.wxservice_server;
 import org.junit.jupiter.api.Test;
 import org.springframework.boot.test.context.SpringBootTest;
 
-@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+@SpringBootTest
 class WxserviceServerApplicationTests {
 
     @Test