yang_shj
8 months ago
15 changed files with 50 additions and 249 deletions
@ -1,59 +0,0 @@
|
||||
package com.hnac.hzims.alarm.ws.level; |
||||
|
||||
import com.hnac.hzims.alarm.source.service.LevelAlarmService; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springblade.core.tool.utils.ObjectUtil; |
||||
import org.springblade.core.tool.utils.StringUtil; |
||||
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; |
||||
|
||||
/** |
||||
* @author ysj |
||||
*/ |
||||
@Slf4j |
||||
@Component |
||||
@EnableScheduling |
||||
public class LevelAlarmRegular { |
||||
|
||||
@Autowired |
||||
private LevelAlarmService levelService; |
||||
|
||||
private LevelAlarmWebSocket socket; |
||||
|
||||
@Value("${hzims.level.wss-url}") |
||||
private String level_wss_url; |
||||
|
||||
// 两分钟进行一次检查链接是否存活,不存活了重新建立链接
|
||||
@Scheduled(cron = "0 0/2 * * * ?") |
||||
private void regular(){ |
||||
// 检查链接存活状态
|
||||
if(ObjectUtil.isEmpty(socket) || !socket.isOpen()){ |
||||
log.error("level websocket survival check : {}","死亡"); |
||||
this.createSocket(); |
||||
if(ObjectUtil.isNotEmpty(socket) && socket.isOpen()){ |
||||
String message = levelService.message(); |
||||
if(!StringUtil.isEmpty(message)){ |
||||
socket.send(message); |
||||
} |
||||
} |
||||
log.error("level websocket survival check : {}","重新建立链接"); |
||||
}else{ |
||||
log.error("level websocket survival check : {}","存活"); |
||||
} |
||||
} |
||||
|
||||
// 创建websocket链接
|
||||
private void createSocket() { |
||||
try{ |
||||
socket = new LevelAlarmWebSocket(new URI(level_wss_url)); |
||||
socket.connectBlocking(); |
||||
}catch (Exception e){ |
||||
log.error("level create error : {}",e.getMessage()); |
||||
} |
||||
} |
||||
} |
@ -1,139 +0,0 @@
|
||||
package com.hnac.hzims.alarm.ws.level; |
||||
|
||||
import com.hnac.hzims.alarm.config.entity.AlarmEntity; |
||||
import com.hnac.hzims.alarm.source.service.LevelAlarmService; |
||||
import com.hnac.hzims.alarm.monitor.service.AlarmSaveService; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.java_websocket.client.WebSocketClient; |
||||
import org.java_websocket.handshake.ServerHandshake; |
||||
import org.springblade.core.log.exception.ServiceException; |
||||
import org.springblade.core.tool.utils.SpringUtil; |
||||
import org.springblade.core.tool.utils.StringUtil; |
||||
|
||||
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.List; |
||||
import java.util.Optional; |
||||
|
||||
/** |
||||
* @author ysj |
||||
*/ |
||||
@Slf4j |
||||
public class LevelAlarmWebSocket extends WebSocketClient { |
||||
|
||||
|
||||
private final LevelAlarmService levelService; |
||||
|
||||
private AlarmSaveService alarmSaveService; |
||||
|
||||
/** |
||||
* 构造等级告警websocket |
||||
* @param uri |
||||
*/ |
||||
public LevelAlarmWebSocket(URI uri) { |
||||
super(uri); |
||||
levelService = SpringUtil.getBean(LevelAlarmService.class); |
||||
connection(this); |
||||
} |
||||
|
||||
// 链接到服务器回调接口
|
||||
@Override |
||||
public void onOpen(ServerHandshake handshakedata) { |
||||
log.error("systemAlarm websocket open"); |
||||
} |
||||
|
||||
// 接收到服务器消息回调接口
|
||||
@Override |
||||
public void onMessage(String message) { |
||||
if(StringUtil.isEmpty(message)){ |
||||
log.error("level alarm on message is null"); |
||||
}else{ |
||||
|
||||
// 告警数据转化
|
||||
List<AlarmEntity> alarmEntities = levelService.receiveMessage(message); |
||||
// 等级告警数据处理
|
||||
try { |
||||
//websocket 消息推送保存
|
||||
alarmSaveService.save(alarmEntities); |
||||
}catch (Exception e){ |
||||
throw new ServiceException("集中监控告警数据处理报错(levelAlarm):"+e); |
||||
} |
||||
} |
||||
} |
||||
|
||||
// 与服务器链接中断回调接口
|
||||
@Override |
||||
public void onClose(int code, String reason, boolean remote) { |
||||
log.error("level alarm websocket close"); |
||||
} |
||||
|
||||
// 与服务器通讯异常触发
|
||||
@Override |
||||
public void onError(Exception e) { |
||||
log.error("level alarm 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("level alarm 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("level alarm SSL init error : {}",e.getMessage()); |
||||
} |
||||
return SSL; |
||||
} |
||||
|
||||
} |
Binary file not shown.
Loading…
Reference in new issue