Explorar o código

1.自动订阅
———— by qlm

qlm %!s(int64=4) %!d(string=hai) anos
pai
achega
f7876c7c55

+ 36 - 0
src/main/java/com/xc/config/ApplicationRunnerImpl.java

@@ -0,0 +1,36 @@
+package com.xc.config;
+
+import com.xc.service.InitStockListService;
+import com.xc.utils.PropertiesUtil;
+import com.xc.utils.websocket.WsClient;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.ContextRefreshedEvent;
+import org.springframework.stereotype.Component;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+@Component
+public class ApplicationRunnerImpl implements ApplicationListener<ContextRefreshedEvent> {
+
+    @Autowired
+    private InitStockListService initStockListService;
+
+
+    @Override
+    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
+
+        if (contextRefreshedEvent.getApplicationContext().getParent() == null) {
+            String url = PropertiesUtil.getProperty("ws.url");
+            try {
+                WsClient myClient = new WsClient(new URI(url));
+                myClient.connect();
+                WsClient.getInstance(initStockListService);
+                System.out.println("订阅成功555");
+            } catch (URISyntaxException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}

+ 11 - 10
src/main/java/com/xc/controller/WebSocketTestController.java

@@ -1,6 +1,7 @@
 package com.xc.controller;
 
 import com.alibaba.fastjson.JSONObject;
+import com.xc.utils.HttpRequest;
 import com.xc.utils.websocket.WsClient;
 import com.xc.service.InitStockListService;
 import com.xc.utils.PropertiesUtil;
@@ -38,10 +39,10 @@ public class WebSocketTestController {
             WsClient.getInstance(initStockListService);
             WsClient myClient = new WsClient(new URI(url));
             myClient.connect();
-            while (!myClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)) {
-                System.out.println("连接中···请稍后");
-                Thread.sleep(1000);
-            }
+//            while (!myClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)) {
+//                System.out.println("连接中···请稍后");
+//                Thread.sleep(1000);
+//            }
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -53,11 +54,7 @@ public class WebSocketTestController {
     @ResponseBody
     public String killws() {
         try {
-
-            String url = PropertiesUtil.getProperty("ws.url")+ UUID.randomUUID().toString().replace("-","");
-            WsClient myClient = new WsClient(new URI(url));
-            myClient.closeWebSocket();
-
+            WsClient.getThis().send("close");
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -69,6 +66,9 @@ public class WebSocketTestController {
     @ResponseBody
     public String init() {
         try {
+//            String url = PropertiesUtil.getProperty("httpdata.url");
+//            String code = HttpRequest.sendGet(url + "remMap?key=" + "BTC", "");
+//            System.out.println(code);
             initStockListService.initStockToRedis();
 
         } catch (Exception e) {
@@ -115,7 +115,8 @@ public class WebSocketTestController {
 
             List<StockVO> stockVOList = new ArrayList<>();
             stockVOList.add(stockVO);
-            initStockListService.updateListInfo(stockVOList);
+            initStockListService.updateInfo(stockVO);
+//            initStockListService.updateListInfo(stockVOList);
 
 
         } catch (Exception e) {

+ 44 - 32
src/main/java/com/xc/utils/websocket/WsClient.java

@@ -26,8 +26,15 @@ public class WsClient extends WebSocketClient {
         return instance;
     }
 
-    public  WsClient(URI serverUri) {
+    public static WsClient getThis() {
+        return instance;
+    }
+
+
+    public WsClient(URI serverUri) {
         super(serverUri);
+        instance = this;
+
     }
 
     @Override
@@ -56,37 +63,42 @@ public class WsClient extends WebSocketClient {
     @Override
     public void onMessage(String arg0) {
         try {
-            if(StringUtils.isNotBlank(arg0)){
-                // 存储信息至redis
-                JSONObject parse = (JSONObject) JSONObject.parse(arg0);
-                StockVO stockVO = new StockVO();
-                stockVO.setId(parse.getInteger("id"));
-                stockVO.setName(parse.getString("name"));
-                stockVO.setCode(parse.getString("code"));
-                stockVO.setSpell(parse.getString("spell"));
-                stockVO.setGid(parse.getString("gid"));
-                stockVO.setNowPrice(parse.get("nowPrice")+"");
-                stockVO.setHcrate(new BigDecimal(parse.get("hcrate")+""));
-                stockVO.setToday_max(parse.get("today_max")+"");
-                stockVO.setToday_min(parse.get("today_min")+"");
-                stockVO.setBusiness_balance(parse.get("business_balance")+"");
-                stockVO.setBusiness_amount(parse.get("business_amount")+"");
-                stockVO.setPreclose_px(parse.get("preclose")+"");
-                stockVO.setOpen_px(parse.get("open")+"");
-                stockVO.setTransState(parse.getInteger("transState"));
-                stockVO.setDepositAmt(parse.getInteger("depositAmt"));
-                stockVO.setMinImg(parse.getString("minImg"));
-                stockVO.setDayImg(parse.getString("dayImg"));
-                stockVO.setWeekImg(parse.getString("weekImg"));
-                stockVO.setMonthImg(parse.getString("monthImg"));
-                stockVO.setChange(parse.get("change")+"");
-                List<StockVO> stockVOList = new ArrayList<>();
-                stockVOList.add(stockVO);
-                initStockListServiceBiz.updateListInfo(stockVOList);
-                initStockListServiceBiz.updateInfo(stockVO);
-                // 处理数据 并 推送给 客户
-                new WebSocketController().sendSysMsg("all",arg0);
-            }
+
+                if(StringUtils.isNotBlank(arg0)){
+                    if(arg0.contains("testws")){
+                        System.out.println(arg0);
+                    }
+                    // 存储信息至redis
+                    JSONObject parse = (JSONObject) JSONObject.parse(arg0);
+                    StockVO stockVO = new StockVO();
+                    stockVO.setId(parse.getInteger("id"));
+                    stockVO.setName(parse.getString("name"));
+                    stockVO.setCode(parse.getString("code"));
+                    stockVO.setSpell(parse.getString("spell"));
+                    stockVO.setGid(parse.getString("gid"));
+                    stockVO.setNowPrice(parse.get("nowPrice")+"");
+                    stockVO.setHcrate(new BigDecimal(parse.get("hcrate")+""));
+                    stockVO.setToday_max(parse.get("today_max")+"");
+                    stockVO.setToday_min(parse.get("today_min")+"");
+                    stockVO.setBusiness_balance(parse.get("business_balance")+"");
+                    stockVO.setBusiness_amount(parse.get("business_amount")+"");
+                    stockVO.setPreclose_px(parse.get("preclose")+"");
+                    stockVO.setOpen_px(parse.get("open")+"");
+                    stockVO.setTransState(parse.getInteger("transState"));
+                    stockVO.setDepositAmt(parse.getInteger("depositAmt"));
+                    stockVO.setMinImg(parse.getString("minImg"));
+                    stockVO.setDayImg(parse.getString("dayImg"));
+                    stockVO.setWeekImg(parse.getString("weekImg"));
+                    stockVO.setMonthImg(parse.getString("monthImg"));
+                    stockVO.setChange(parse.get("change")+"");
+                    List<StockVO> stockVOList = new ArrayList<>();
+                    stockVOList.add(stockVO);
+                    initStockListServiceBiz.updateListInfo(stockVOList);
+                    initStockListServiceBiz.updateInfo(stockVO);
+                    // 处理数据 并 推送给 客户
+                    new WebSocketController().sendSysMsg("all",arg0);
+                }
+
         }catch (Exception e){
 
         }

+ 5 - 2
src/main/resources/stock2guo.properties

@@ -154,8 +154,11 @@ jisuapi.news.url=https://api.jisuapi.com/news/get
 jisuapi.news.channel=股票
 
 #websocket 订阅地址 (数据解析服务器)
-#ws.url=ws://127.0.0.1:8080/websocket/
-ws.url=ws://101.32.215.72:8080/data_server_java/websocket/
+ws.url=ws://127.0.0.1:8080/websocket/zxdata987654
+#测试
+#ws.url=ws://101.32.215.72:8080/data_server_java/websocket/zxdata987654
+#正式
+#ws.url=ws://101.32.215.72:8080/data_server_java/websocket/zxdata987654+
 
 httpdata.url=http://101.32.215.72:8080/data_server_java/