haungxing
2 years ago
15 changed files with 358 additions and 103 deletions
@ -0,0 +1,63 @@
|
||||
package com.hnac.hzims.operational.config.ws; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hnac.hzims.operational.alert.service.LevelAlarmService; |
||||
import com.hnac.hzims.operational.config.service.StFocusPropertiesService; |
||||
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; |
||||
|
||||
/** |
||||
* 等级告警获取数据长链接 |
||||
* @author ysj |
||||
*/ |
||||
@Slf4j |
||||
@Component |
||||
@EnableScheduling |
||||
public class LevelAlarmRegular{ |
||||
|
||||
@Value("${hzims.level.ws-url}") |
||||
private String level_wss_url; |
||||
|
||||
private LevelAlarmWebSocket client; |
||||
|
||||
@Autowired |
||||
private LevelAlarmService levelAlarmService; |
||||
|
||||
// 定时发送消息
|
||||
@Scheduled(cron = "0/30 * * * * ?") |
||||
private void regular(){ |
||||
// 检查链接存活状态
|
||||
if(this.block()){ |
||||
client.send(levelAlarmService.sendMessage()); |
||||
} |
||||
client = this.createClient(); |
||||
} |
||||
|
||||
// 创建websocket链接
|
||||
private LevelAlarmWebSocket createClient() { |
||||
try{ |
||||
return new LevelAlarmWebSocket(new URI(level_wss_url)); |
||||
}catch (Exception e){ |
||||
log.error("level create error : {}",e.getMessage()); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
// 检查链接存活状态
|
||||
private boolean block() { |
||||
try{ |
||||
return client.connectBlocking(); |
||||
} catch (Exception e){ |
||||
log.error("level block error : {}",e.getMessage()); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,124 @@
|
||||
package com.hnac.hzims.operational.config.ws; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hnac.hzims.operational.alert.service.LevelAlarmService; |
||||
import com.hnac.hzims.operational.config.service.StAlamRecordService; |
||||
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.scheduling.annotation.EnableScheduling; |
||||
|
||||
import javax.net.ssl.SSLContext; |
||||
import javax.net.ssl.TrustManager; |
||||
import javax.net.ssl.X509TrustManager; |
||||
import java.net.Socket; |
||||
import java.net.URI; |
||||
import java.security.SecureRandom; |
||||
import java.security.cert.X509Certificate; |
||||
import java.util.Optional; |
||||
|
||||
/** |
||||
* 等级告警获取数据长链接 |
||||
* @author ysj |
||||
*/ |
||||
@Slf4j |
||||
public class LevelAlarmWebSocket extends WebSocketClient { |
||||
|
||||
private final LevelAlarmService levelAlarmService; |
||||
|
||||
/** |
||||
* 构造等级告警websocket |
||||
* @param uri |
||||
*/ |
||||
public LevelAlarmWebSocket(URI uri) { |
||||
super(uri); |
||||
levelAlarmService = SpringUtil.getBean(LevelAlarmService.class); |
||||
connection(this); |
||||
} |
||||
|
||||
// 链接到服务器回调接口
|
||||
@Override |
||||
public void onOpen(ServerHandshake handshakedata) { |
||||
log.error("levl websocket open"); |
||||
} |
||||
|
||||
// 接收到服务器消息回调接口
|
||||
@Override |
||||
public void onMessage(String message) { |
||||
levelAlarmService.receiveMessage(message); |
||||
} |
||||
|
||||
// 与服务器链接中断回调接口
|
||||
@Override |
||||
public void onClose(int code, String reason, boolean remote) { |
||||
log.error("levl websocket close"); |
||||
} |
||||
|
||||
// 与服务器通讯异常触发
|
||||
@Override |
||||
public void onError(Exception e) { |
||||
log.error("levl websocket error : {}",e.getMessage()); |
||||
} |
||||
|
||||
/** |
||||
* 建立链接 |
||||
* @param webSocket |
||||
*/ |
||||
private void connection(LevelAlarmWebSocket webSocket) { |
||||
SSLContext context = init(); |
||||
if(Optional.ofNullable(context).isPresent()){ |
||||
Socket socket = create(context); |
||||
if(Optional.ofNullable(socket).isPresent()){ |
||||
webSocket.setSocket(socket); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 创建Socket |
||||
* @param context |
||||
* @return |
||||
*/ |
||||
private Socket create(SSLContext context) { |
||||
Socket socket = null; |
||||
try{ |
||||
socket = context.getSocketFactory().createSocket(); |
||||
}catch (Exception e){ |
||||
log.error("leve socket create error : {}",e.getMessage()); |
||||
} |
||||
return socket; |
||||
} |
||||
|
||||
/** |
||||
* 协议初始化 |
||||
* @return SSLContext |
||||
*/ |
||||
private SSLContext init() { |
||||
SSLContext SSL = null; |
||||
try{ |
||||
SSL = SSLContext.getInstance("TLS"); |
||||
SSL.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()); |
||||
}catch (Exception e){ |
||||
log.error("leve SSL init error : {}",e.getMessage()); |
||||
} |
||||
return SSL; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue