1
zj
2024-07-21 5c1e682edc2bc6c89cf0f34f93a438d1da274e64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package org.example.WsBean;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.example.pojo.Currency;
import org.example.server.impl.CurrencySerivceImpl;
import org.example.wsClient.BitgetClient;
import org.example.wsClient.GateClient;
import org.example.wsClient.KucoinClient;
import org.example.wsClient.MexcClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
/**
 * @ClassDescription: 客户端请求类
 * @JdkVersion: 1.8
 * @Created: 2023/8/31 16:13
 */
@Slf4j
@Configuration
public class MexcWsBean {
 
    @Autowired
    private CurrencySerivceImpl currencyService;
 
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
 
//    @Bean
//    public void mexcWebsocketRunClientMap() {
//        List<Currency> mexc = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "mexc"));
//        if (!CollectionUtils.isEmpty(mexc)) {
//            int batchSize = 30; // 每个线程处理的数据量
//            int totalSize = mexc.size();
//            int threadCount = (int) Math.ceil((double) totalSize / batchSize); // 计算需要的线程数
//
//            for (int i = 0; i < threadCount; i++) {
//                int fromIndex = i * batchSize;
//                int toIndex = Math.min(fromIndex + batchSize, totalSize);
//                List<Currency> sublist = mexc.subList(fromIndex, toIndex);
//
//                // 使用自定义线程池提交任务
//                threadPoolTaskExecutor.execute(new MexcClient(sublist)::start);
//            }
//
//        }
//    }
 
//    @Bean
//    public void gateWebsocketRunClientMap() {
//        List<Currency> mexc = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "gate"));
//        if (!CollectionUtils.isEmpty(mexc)) {
//            int batchSize = 100; // 每个线程处理的数据量
//            int totalSize = mexc.size();
//            int threadCount = (int) Math.ceil((double) totalSize / batchSize); // 计算需要的线程数
//
//            for (int i = 0; i < threadCount; i++) {
//                int fromIndex = i * batchSize;
//                int toIndex = Math.min(fromIndex + batchSize, totalSize);
//                List<Currency> sublist = mexc.subList(fromIndex, toIndex);
//
//                // 使用自定义线程池提交任务
//                threadPoolTaskExecutor.execute(new GateClient(sublist)::start);
//            }
//
//        }
//    }
 
    @Bean
    public void bitgetWebsocketRunClientMap() throws JSONException, JsonProcessingException {
        List<Currency> mexc = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "bitget"));
        if (!CollectionUtils.isEmpty(mexc)) {
            int batchSize = 100; // 每个线程处理的数据量
            int totalSize = mexc.size();
            int threadCount = (int) Math.ceil((double) totalSize / batchSize); // 计算需要的线程数
 
            for (int i = 0; i < threadCount; i++) {
                int fromIndex = i * batchSize;
                int toIndex = Math.min(fromIndex + batchSize, totalSize);
                List<Currency> sublist = mexc.subList(fromIndex, toIndex);
                String parameter = getParameter(sublist);
                // 使用自定义线程池提交任务
                threadPoolTaskExecutor.execute(new BitgetClient(parameter)::start);
            }
 
        }
    }
//
//    @Bean
//    public void kucoinWebsocketRunClientMap() throws Exception {
//        List<Currency> mexc = currencyService.list(new LambdaQueryWrapper<Currency>().eq(Currency::getSource, "kucoin"));
//        if (!CollectionUtils.isEmpty(mexc)) {
//            String result = doPost();
//            JSONObject jsonObject = new JSONObject(result);
//            String token = jsonObject.getJSONObject("data").getString("token");
//            int batchSize = 100; // 每个线程处理的数据量
//            int totalSize = mexc.size();
//            int threadCount = (int) Math.ceil((double) totalSize / batchSize); // 计算需要的线程数
//
//            for (int i = 0; i < threadCount; i++) {
//                int fromIndex = i * batchSize;
//                int toIndex = Math.min(fromIndex + batchSize, totalSize);
//                List<Currency> sublist = mexc.subList(fromIndex, toIndex);
//
//                // 使用自定义线程池提交任务
//                threadPoolTaskExecutor.execute(new KucoinClient(sublist,token)::start);
//            }
//
//        }
//    }
 
    public static String doPost() throws Exception {
        String url = "https://api.kucoin.com/api/v1/bullet-public";
        HttpPost httpPost = new HttpPost(url);
        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        httpPost.setEntity(new UrlEncodedFormEntity(nvps));
        HttpResponse response = defaultHttpClient.execute(httpPost);
        HttpEntity respEntity = response.getEntity();
        String text = EntityUtils.toString(respEntity, "UTF-8");
        defaultHttpClient.getConnectionManager().shutdown();
        return text;
    }
 
    public String getParameter(List<Currency> list) throws JsonProcessingException, JSONException {
        // 创建一个ObjectMapper实例
        ObjectMapper mapper = new ObjectMapper();
        List<String> symbolList = list.stream().map(Currency::getSymbol).collect(Collectors.toList());
        // 使用Map构建JSON对象
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("op", "subscribe");
        List<Map<String, String>> mapList = new ArrayList<>();
        symbolList.forEach(f->{
            Map<String, String> argsMap = new HashMap<>();
            argsMap.put("instType", "SPOT");
            argsMap.put("channel", "books15");
            argsMap.put("instId", f);
            mapList.add(argsMap);
        });
        jsonMap.put("args", mapList);
 
        // 将Map转换为JSON字符串
        String jsonString = mapper.writeValueAsString(jsonMap);
        return jsonString;
 
    }
}