Commit ec76f9b5 authored by zhouleilei's avatar zhouleilei

心跳设置

parent 5593ed05
package net.wanji.utc.hisense;
import net.wanji.utc.hisense.netty.TcpClient;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
......@@ -33,6 +34,6 @@ public class HisenseApplication implements CommandLineRunner {
public void run(String... args) throws Exception {
//UdpClient.connection(localPort, remoteProt);
//TcpClient.connection(remoteIp, remoteProt);
//TcpClient.connection("127.0.0.1", remoteProt);
TcpClient.connection("127.0.0.1", remoteProt);
}
}
......@@ -7,14 +7,12 @@ import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import net.wanji.utc.hisense.cache.netty.NettyMessageCache;
import net.wanji.utc.hisense.common.enums.xml.MessageTypeEnum;
import net.wanji.utc.hisense.netty.codec.MessageDecoder;
import net.wanji.utc.hisense.netty.codec.MessageEnCoder;
import net.wanji.utc.hisense.netty.handler.TcpClientHandler;
import net.wanji.utc.hisense.pojo.xml.pojo.messagecontent.HeartBeatRequest;
import net.wanji.utc.hisense.util.XMLUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
......@@ -22,7 +20,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Objects;
/**
* @author duanruiming
......@@ -46,6 +43,8 @@ public class TcpClient {
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new MessageDecoder());
ch.pipeline().addLast(new MessageEnCoder());
ch.pipeline().addLast(new IdleStateHandler(10,
10, 10));
ch.pipeline().addLast(new TcpClientHandler()); // 添加自定义的处理器
}
});
......@@ -112,18 +111,4 @@ public class TcpClient {
});
}
@Scheduled(initialDelay = 1000 * 30, fixedRate = 1000 * 20)
public void hearBeat() throws Exception {
try {
HeartBeatRequest heartBeatRequest = new HeartBeatRequest();
heartBeatRequest.setMessageType(MessageTypeEnum.HEARTBEAT_STATUS.getType());
log.error("心跳消息:{}", XMLUtils.convertToXml(heartBeatRequest));
if (Objects.nonNull(tcpChannelFuture)) {
tcpChannelFuture.channel().writeAndFlush(XMLUtils.strToHex(XMLUtils.convertToXml(heartBeatRequest)));
}
} catch (Exception e) {
log.error("每20秒心跳发送失败:", e);
throw new Exception(e);
}
}
}
......@@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import net.wanji.common.framework.spring.ServiceBeanContext;
import net.wanji.databus.po.CrossInfoPO;
......@@ -11,15 +12,19 @@ import net.wanji.utc.hisense.cache.CrossInfoCache;
import net.wanji.utc.hisense.cache.SignalDataCache;
import net.wanji.utc.hisense.cache.netty.NettyMessageCache;
import net.wanji.utc.hisense.common.enums.CommandResultSignEnum;
import net.wanji.utc.hisense.common.enums.xml.MessageTypeEnum;
import net.wanji.utc.hisense.netty.commandsign.CommandResultSign;
import net.wanji.utc.hisense.netty.pojo.CommandPojo;
import net.wanji.utc.hisense.netty.response.CommandResponseFactory;
import net.wanji.utc.hisense.pojo.convert.RunningLightsStatusPojo;
import net.wanji.utc.hisense.pojo.netty.MessageResultPojo;
import net.wanji.utc.hisense.pojo.xml.pojo.messagecontent.HeartBeatRequest;
import net.wanji.utc.hisense.util.XMLUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
......@@ -110,4 +115,26 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<DatagramPacket
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
super.userEventTriggered(ctx, evt);
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
log.info("已经 10 秒没有发送信息,给服务端发送心跳!");
//向服务端发送消息
try {
SocketAddress socketAddress = ctx.channel().remoteAddress();
log.info(socketAddress.toString());
//向服务端发送心跳
HeartBeatRequest heartBeatRequest = new HeartBeatRequest();
heartBeatRequest.setMessageType(MessageTypeEnum.HEARTBEAT_STATUS.getType());
log.error("心跳消息:{}", XMLUtils.convertToXml(heartBeatRequest));
ctx.channel().writeAndFlush(XMLUtils.strToHex(XMLUtils.convertToXml(heartBeatRequest)));
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment