yang_shj
2 years ago
3 changed files with 66 additions and 316 deletions
@ -1,93 +0,0 @@
|
||||
/* |
||||
package com.hnac.hzims.operational.config.ws; |
||||
|
||||
import cn.hutool.core.collection.CollectionUtil; |
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hnac.hzims.operational.config.service.StFocusPropertiesService; |
||||
import com.hnac.hzims.operational.config.vo.StationRealVo; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.scheduling.annotation.EnableScheduling; |
||||
import org.springframework.scheduling.annotation.Scheduled; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.net.URI; |
||||
import java.net.URISyntaxException; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
import java.util.stream.Stream; |
||||
|
||||
@Slf4j |
||||
@EnableScheduling |
||||
@Component |
||||
public class WebSocketClientConfig { |
||||
|
||||
@Value("${hzims.config.ws-url}") |
||||
String wsUrl; |
||||
|
||||
List<WebsocketConfigClient> clientList; |
||||
|
||||
@Autowired |
||||
private StFocusPropertiesService stFocusPropertiesService; |
||||
|
||||
|
||||
private static final Integer MAX_SEND = 40; |
||||
|
||||
*/ |
||||
/** |
||||
* 定时30秒推送一次消息 |
||||
*//*
|
||||
|
||||
@Scheduled(cron = "0/30 * * * * ?") |
||||
private void keepAlive() throws InterruptedException, URISyntaxException { |
||||
*/ |
||||
/*try { |
||||
List<StationRealVo> message = stFocusPropertiesService.getStationRealIds(); |
||||
if (CollectionUtil.isEmpty(message)) { |
||||
return; |
||||
} |
||||
int limit = countStep(message.size()); |
||||
List<List<StationRealVo>> list = Stream.iterate(0, n -> n + 1).limit(limit).parallel().map(a -> message.stream().skip(a * MAX_SEND).limit(MAX_SEND).parallel().collect(Collectors.toList())).collect(Collectors.toList()); |
||||
if (CollectionUtil.isEmpty(list)) { |
||||
return; |
||||
} |
||||
if (CollectionUtil.isEmpty(clientList)) { |
||||
clientList = new ArrayList<>(); |
||||
for (int i = 0; i < limit; i++) { |
||||
WebsocketConfigClient client = new WebsocketConfigClient(new URI(wsUrl)); |
||||
if (client.connectBlocking()) { |
||||
String json = JSONObject.toJSONString(list.get(i)); |
||||
client.send(json); |
||||
clientList.add(client); |
||||
} |
||||
} |
||||
return; |
||||
} |
||||
if(CollectionUtil.isNotEmpty(clientList.stream().filter(o->!o.isOpen()).collect(Collectors.toList()))){ |
||||
clientList.clear(); |
||||
return; |
||||
} |
||||
for (int i = 0; i < limit; i++) { |
||||
String json = JSONObject.toJSONString(list.get(i)); |
||||
WebsocketConfigClient client = clientList.get(i); |
||||
client.send(json); |
||||
} |
||||
}catch (Exception e){ |
||||
log.error("[WebsocketConfigClient] 错误={}",e.toString()); |
||||
}*//*
|
||||
|
||||
} |
||||
|
||||
|
||||
*/ |
||||
/** |
||||
* 计算切分次数 |
||||
*//*
|
||||
|
||||
private static Integer countStep(Integer size) { |
||||
return (size + MAX_SEND - 1) / MAX_SEND; |
||||
} |
||||
} |
||||
*/ |
@ -1,124 +0,0 @@
|
||||
/* |
||||
package com.hnac.hzims.operational.config.ws; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.alibaba.fastjson.TypeReference; |
||||
import com.hnac.hzims.operational.config.service.StAlamRecordService; |
||||
import com.hnac.hzims.operational.station.service.IRealMonitorService; |
||||
import jodd.util.StringUtil; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.collections4.MapUtils; |
||||
import org.java_websocket.client.WebSocketClient; |
||||
import org.java_websocket.handshake.ServerHandshake; |
||||
import org.springblade.core.tool.utils.SpringUtil; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import javax.net.ssl.SSLContext; |
||||
import javax.net.ssl.SSLSocketFactory; |
||||
import javax.net.ssl.TrustManager; |
||||
import javax.net.ssl.X509TrustManager; |
||||
import java.net.URI; |
||||
import java.security.SecureRandom; |
||||
import java.security.cert.X509Certificate; |
||||
import java.util.Map; |
||||
import java.util.concurrent.CompletableFuture; |
||||
|
||||
|
||||
@Slf4j |
||||
public class WebsocketConfigClient extends WebSocketClient { |
||||
|
||||
@Value("${hzims.config.ws-url}") |
||||
String wsUrl; |
||||
|
||||
StAlamRecordService stAlamRecordService; |
||||
|
||||
private static final String MSG_SUCCESS_STATUS = "200"; |
||||
|
||||
|
||||
public WebsocketConfigClient(URI serverUri) { |
||||
super(serverUri); |
||||
stAlamRecordService = SpringUtil.getBean(StAlamRecordService.class); |
||||
if (serverUri.toString().contains("wss://") && serverUri.toString().contains("/data")) { |
||||
trustAllHosts(this); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onOpen(ServerHandshake handshakedata) { |
||||
System.out.println("onOpen"); |
||||
} |
||||
|
||||
@Override |
||||
public void onMessage(String message) { |
||||
try { |
||||
manageHandleMsg(message); |
||||
} catch (Exception e) { |
||||
log.error("websocketConfigClient error{}",message,e); |
||||
} |
||||
log.error("onMessage"); |
||||
} |
||||
|
||||
@Override |
||||
public void onClose(int code, String reason, boolean remote) { |
||||
log.error("-----------------------------------------------,123,onClose"); |
||||
} |
||||
|
||||
@Override |
||||
public void onError(Exception ex) { |
||||
log.error("------------------onError"); |
||||
} |
||||
|
||||
|
||||
private void manageHandleMsg(String message) { |
||||
if(StringUtil.isBlank(message)) { |
||||
return; |
||||
} |
||||
// 将信息转换成json
|
||||
JSONObject msg = JSONObject.parseObject(message); |
||||
String status = msg.getString("status"); |
||||
String result = msg.getString("result"); |
||||
if(StringUtil.isBlank(result)){ |
||||
return; |
||||
} |
||||
if(StringUtil.isBlank(status) || !MSG_SUCCESS_STATUS.equals(status)){ |
||||
return; |
||||
} |
||||
// 将结果响应结果转换成嵌套Map格式
|
||||
Map<String, Map<String, String>> resultMap = JSONObject.parseObject(result, new TypeReference<Map<String, Map<String, String>>>(){}); |
||||
if(MapUtils.isEmpty(resultMap)) { |
||||
return; |
||||
} |
||||
// 存储redis
|
||||
stAlamRecordService.receiveDataStorage(resultMap); |
||||
} |
||||
|
||||
|
||||
|
||||
void trustAllHosts(WebsocketConfigClient appClient) { |
||||
log.info("[websocket] wss 连接 ----- "); |
||||
try { |
||||
// wss需添加
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS"); |
||||
sslContext.init(null, new TrustManager[]{new X509TrustManager() { |
||||
@Override |
||||
public void checkClientTrusted(X509Certificate[] chain, |
||||
String authType) { |
||||
} |
||||
|
||||
@Override |
||||
public void checkServerTrusted(X509Certificate[] chain, String authType) { |
||||
} |
||||
|
||||
@Override |
||||
public X509Certificate[] getAcceptedIssuers() { |
||||
return new X509Certificate[0]; |
||||
} |
||||
}}, new SecureRandom()); |
||||
SSLSocketFactory factory = sslContext.getSocketFactory(); |
||||
appClient.setSocket(factory.createSocket()); |
||||
} catch (Exception e) { |
||||
log.error("[websocket] trustAllHosts 错误 - " + e.toString()); |
||||
} |
||||
} |
||||
|
||||
} |
||||
*/ |
Loading…
Reference in new issue