|
|
|
@ -1,12 +1,16 @@
|
|
|
|
|
package com.docus.server.common.netty.server.handler;
|
|
|
|
|
|
|
|
|
|
import com.docus.core.util.Func;
|
|
|
|
|
import com.docus.core.util.json.JSON;
|
|
|
|
|
import com.docus.server.common.CommMsg;
|
|
|
|
|
import com.docus.server.common.MsgConstants;
|
|
|
|
|
import com.docus.server.common.netty.server.ChannelRepository;
|
|
|
|
|
import com.docus.server.common.netty.server.TerminalToChannelCacheMap;
|
|
|
|
|
import com.docus.server.dto.scheduling.management.schterminator.NettyTerminatorDTO;
|
|
|
|
|
import com.docus.server.enums.BusyStateEnum;
|
|
|
|
|
import com.docus.server.enums.OnlineStateEnum;
|
|
|
|
|
import com.docus.server.service.ISchTerminatorService;
|
|
|
|
|
import com.docus.server.vo.scheduling.management.schcollector.LoadSchCollectorVO;
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
|
import io.netty.channel.ChannelHandler;
|
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
@ -20,6 +24,7 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 业务消息处理
|
|
|
|
@ -32,38 +37,45 @@ public class NettyBusinessHandler extends SimpleChannelInboundHandler<CommMsg<Se
|
|
|
|
|
private static final ChannelGroup DEFAULT_CHANNEL_GROUP = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private ChannelRepository repository;
|
|
|
|
|
private TerminalToChannelCacheMap terminalToChannelCacheMap;
|
|
|
|
|
@Resource
|
|
|
|
|
private ISchTerminatorService iSchTerminatorService;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void channelRead0(ChannelHandlerContext channelHandlerContext, CommMsg commMsg) throws Exception {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String messageType = commMsg.getMessageType();
|
|
|
|
|
String messageTime = commMsg.getMessageTime();
|
|
|
|
|
Serializable messageContent = commMsg.getContent();
|
|
|
|
|
String messageContent = (String) commMsg.getContent();
|
|
|
|
|
|
|
|
|
|
log.info("======== 【调度器服务端-收到终端消息】, 消息时间={}, 消息类型={}, 消息内容={}", messageTime, messageType, messageContent + " ======== ");
|
|
|
|
|
|
|
|
|
|
if (messageType.equals(MsgConstants.TERMINATOR_RESTART)) {
|
|
|
|
|
log.info("接受到终端重启命令,内容{}", messageContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (messageType.equals(MsgConstants.COLLECTOR_RESTART)) {
|
|
|
|
|
} else if (messageType.equals(MsgConstants.COLLECTOR_RESTART)) {
|
|
|
|
|
log.info("收到采集器重启命令,内容{}", messageContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (messageType.equals(MsgConstants.VIRTUAL_RESTART)) {
|
|
|
|
|
} else if (messageType.equals(MsgConstants.VIRTUAL_RESTART)) {
|
|
|
|
|
log.info("收到虚拟机重启命令,内容{}", messageContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (messageType.equals(MsgConstants.UPDATE_COLLECTOR_FILE)) {
|
|
|
|
|
} else if (messageType.equals(MsgConstants.UPDATE_COLLECTOR_FILE)) {
|
|
|
|
|
log.info("收到更新采集器文件命令,内容{}", messageContent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (messageType.equals(MsgConstants.UPDATE_COLLECTOR_CONFIG)) {
|
|
|
|
|
} else if (messageType.equals(MsgConstants.UPDATE_COLLECTOR_CONFIG)) {
|
|
|
|
|
log.info("收到更新采集器配置命令,内容{}", messageContent);
|
|
|
|
|
} else if (messageType.equals(MsgConstants.SCH_DISTRIBUTE_TASKS)) {
|
|
|
|
|
log.info("收到采集调度器下发任务命令,内容{}", messageContent);
|
|
|
|
|
} else if (messageType.equals(MsgConstants.HAS_COLLECTOR_COUNT)) {
|
|
|
|
|
log.info("收到终端客户端返回的【采集器数量】消息,内容{}", messageContent);
|
|
|
|
|
|
|
|
|
|
String terminalIp = terminalToChannelCacheMap.getClientKey(channelHandlerContext.channel());
|
|
|
|
|
|
|
|
|
|
NettyTerminatorDTO nettyTerminatorDTO = terminalToChannelCacheMap.getTerminatorByIp(terminalIp);
|
|
|
|
|
|
|
|
|
|
if (Func.isNotBlank(messageContent)) {
|
|
|
|
|
nettyTerminatorDTO.getLoadSchCollectorVOList().clear();
|
|
|
|
|
List<LoadSchCollectorVO> loadSchCollectorVOList = JSON.fromJSONWithGeneric(messageContent, new TypeReference<List<LoadSchCollectorVO>>() {
|
|
|
|
|
});
|
|
|
|
|
nettyTerminatorDTO.getLoadSchCollectorVOList().addAll(loadSchCollectorVOList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (messageType.equals(MsgConstants.SCH_DISTRIBUTE_TASKS)) {
|
|
|
|
|
log.info("收到采集调度器下发任务命令,内容{}", messageContent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -78,7 +90,7 @@ public class NettyBusinessHandler extends SimpleChannelInboundHandler<CommMsg<Se
|
|
|
|
|
String clientIp = ipSocket.getAddress().getHostAddress();
|
|
|
|
|
log.info("【采集器-终端IP】:{},连接上线,IP地址信息:{}", clientIp, clientIp);
|
|
|
|
|
|
|
|
|
|
String clientId = repository.getClientKey(ctx.channel());
|
|
|
|
|
String clientId = terminalToChannelCacheMap.getClientKey(ctx.channel());
|
|
|
|
|
|
|
|
|
|
if (Func.isBlank(clientId)) {
|
|
|
|
|
|
|
|
|
@ -87,7 +99,7 @@ public class NettyBusinessHandler extends SimpleChannelInboundHandler<CommMsg<Se
|
|
|
|
|
nettyTerminatorDTO.setBusyState(BusyStateEnum.IDLE);
|
|
|
|
|
nettyTerminatorDTO.setOnlineState(OnlineStateEnum.OFFLINE);
|
|
|
|
|
|
|
|
|
|
repository.put(nettyTerminatorDTO, ctx.channel());
|
|
|
|
|
terminalToChannelCacheMap.put(nettyTerminatorDTO, ctx.channel());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEFAULT_CHANNEL_GROUP.add(ctx.channel());
|
|
|
|
@ -102,11 +114,11 @@ public class NettyBusinessHandler extends SimpleChannelInboundHandler<CommMsg<Se
|
|
|
|
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
|
|
|
|
Channel channel = ctx.channel();
|
|
|
|
|
System.out.println(channel.remoteAddress() + " 下线," + "【调度器-终端】在线数量:" + DEFAULT_CHANNEL_GROUP.size());
|
|
|
|
|
String clientId = repository.getClientKey(channel);
|
|
|
|
|
String clientId = terminalToChannelCacheMap.getClientKey(channel);
|
|
|
|
|
log.error("客户端下线,终端连接:{}", clientId);
|
|
|
|
|
//移除终端,终端离线
|
|
|
|
|
if (clientId != null) {
|
|
|
|
|
repository.remove(clientId);
|
|
|
|
|
terminalToChannelCacheMap.remove(clientId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|