channelList, String topicFormat) {
74 | for (String sub : channelList) {
75 | //如果不存在说明该交易所新增加了交易对 需要订阅该交易对
76 | if (!channelCache.contains(sub)) {
77 | klineClient.addSub(formatChannel(topicFormat, sub));
78 | }
79 | }
80 | channelCache = channelList;
81 | //交易所删除交易对 删除的这边就不做处理了
82 | }
83 |
84 | /**
85 | * 拼接订阅主题
86 | */
87 | private String formatChannel(String topic, String channel) {
88 | if (topic.equalsIgnoreCase(Topic.KLINE_SUB)) {
89 | return String.format(topic, channel, Topic.PERIOD[0]);
90 | }
91 | return String.format(topic, channel);
92 | }
93 | }
94 |
95 |
--------------------------------------------------------------------------------
/src/main/java/com/oujiong/exchange/client/scheduler/HuobiTrigger.java:
--------------------------------------------------------------------------------
1 | package com.oujiong.exchange.client.scheduler;
2 |
3 | import com.oujiong.exchange.client.huobi.service.HuobiProMainService;
4 | import lombok.extern.slf4j.Slf4j;
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.scheduling.annotation.Scheduled;
7 | import org.springframework.stereotype.Component;
8 |
9 | import javax.annotation.PostConstruct;
10 |
11 | /**
12 | * @author xub
13 | * @Description: 火币网定时任务获取时时虚拟币数据
14 | * @date 2019/7/27 下午9:50
15 | */
16 | @Component
17 | @Slf4j
18 | public class HuobiTrigger {
19 |
20 | @Autowired
21 | private HuobiProMainService huobiProMainService;
22 |
23 | /**
24 | * 首次启动并订阅火币websocket数据
25 | */
26 | @PostConstruct
27 | public void firstSub() {
28 | try {
29 | huobiProMainService.start();
30 | } catch (Exception e) {
31 | log.error("huobi 首次启动订阅异常", e);
32 | }
33 | }
34 |
35 | /**
36 | * 上面首次启动就已经可以本地websokcet就已经和火币的websokcet连接成功,时时获取数据了。
37 | *
38 | * 但是因为火币网的交易对可能会新增和减少,所以这里通过定时任务获取最新交易对数据,并进行订阅
39 | */
40 | @Scheduled(cron = "0 0 */1 * * *")
41 | public void doSubHuoBiPro() {
42 | try {
43 | huobiProMainService.refreshSubData();
44 | } catch (Exception e) {
45 | log.error("刷新火币网交易对缓存, 并尝试重新订阅数据, error.", e);
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/com/oujiong/exchange/client/utils/GZipUtils.java:
--------------------------------------------------------------------------------
1 | package com.oujiong.exchange.client.utils;
2 |
3 | import java.io.*;
4 | import java.util.zip.GZIPInputStream;
5 |
6 | /**
7 | * @Description: GZip 压缩辅助类(火币网数据需要解压)
8 | *
9 | * @author xub
10 | * @date 2019/7/30 下午7:07
11 | */
12 | public class GZipUtils {
13 |
14 | public static final int BUFFER = 1024;
15 |
16 | /**
17 | * 数据解压缩
18 | *
19 | * @param data
20 | */
21 | public static byte[] decompress(byte[] data) throws Exception {
22 | ByteArrayInputStream bais = new ByteArrayInputStream(data);
23 | ByteArrayOutputStream baos = new ByteArrayOutputStream();
24 |
25 | // 解压缩
26 | decompress(bais, baos);
27 | data = baos.toByteArray();
28 | baos.flush();
29 | baos.close();
30 | bais.close();
31 | return data;
32 | }
33 |
34 |
35 | /**
36 | * 数据解压缩
37 | *
38 | * @param is
39 | * @param os
40 | */
41 | public static void decompress(InputStream is, OutputStream os) throws Exception {
42 | GZIPInputStream gis = new GZIPInputStream(is);
43 | int count;
44 | byte data[] = new byte[BUFFER];
45 | while ((count = gis.read(data, 0, BUFFER)) != -1) {
46 | os.write(data, 0, count);
47 | }
48 | gis.close();
49 | }
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/com/oujiong/exchange/client/utils/MonitorTask.java:
--------------------------------------------------------------------------------
1 | package com.oujiong.exchange.client.utils;
2 |
3 | import com.oujiong.exchange.client.common.AbstractWebSocketClient;
4 | import lombok.extern.slf4j.Slf4j;
5 |
6 |
7 | /**
8 | * @Description: 防止 因为某种原因与火币网的websokcet连接被断开 所以需要有线程不定时查看查看,
9 | *
10 | * 当5秒中没有获取数据火币网的消息,就认为与火币websocket失去连接 ,需要重新连接
11 | * @author xub
12 | * @date 2019/7/30 下午7:31
13 | */
14 | @Slf4j
15 | public class MonitorTask implements Runnable {
16 |
17 | private long startTime = System.currentTimeMillis();
18 |
19 | private long checkTime = 5000L;
20 |
21 | private AbstractWebSocketClient client;
22 |
23 | public MonitorTask(AbstractWebSocketClient client) {
24 | this.client = client;
25 | }
26 |
27 | /**
28 | * 每次获取消息都会更新startTime
29 | */
30 | public void updateTime() {
31 | this.startTime = System.currentTimeMillis();
32 | }
33 |
34 | @Override
35 | public void run() {
36 | if (System.currentTimeMillis() - startTime > checkTime) {
37 | client.reConnect();
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | server:
2 | port: 8080
3 |
--------------------------------------------------------------------------------