作者 crossoverJie

:recycle: 重构代码

... ... @@ -13,29 +13,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @author crossoverJie
*/
@SpringBootApplication
public class Application implements CommandLineRunner{
public class Application{
private final static Logger LOGGER = LoggerFactory.getLogger(Application.class);
@Autowired
private HeartBeatServer heartBeatServer ;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
LOGGER.info("启动成功");
}
@Override
public void run(String... args) throws Exception {
ChannelFuture future = heartBeatServer.start();
Runtime.getRuntime().addShutdownHook(new Thread(){
@Override
public void run() {
heartBeatServer.destroy();
}
});
future.channel().closeFuture().syncUninterruptibly() ;
}
}
\ No newline at end of file
... ...
... ... @@ -2,6 +2,7 @@ package com.crossoverjie.netty.action.handle;
import com.crossoverjie.netty.action.pojo.CustomProtocol;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleState;
... ... @@ -32,7 +33,8 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<CustomPro
LOGGER.info("已经5秒没有收到信息!");
//向客户端发送消息
CustomProtocol customProtocol = new CustomProtocol(12345L,"pong") ;
ctx.writeAndFlush(Unpooled.copiedBuffer(customProtocol.toString(), CharsetUtil.UTF_8)) ;
ctx.writeAndFlush(Unpooled.copiedBuffer(customProtocol.toString(), CharsetUtil.UTF_8))
.addListener(ChannelFutureListener.CLOSE_ON_FAILURE) ;
}
... ...
package com.crossoverjie.netty.action.server;
import com.crossoverjie.netty.action.Application;
import com.crossoverjie.netty.action.channel.init.HeartbeatInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.InetSocketAddress;
/**
... ... @@ -26,6 +28,8 @@ import java.net.InetSocketAddress;
@Component
public class HeartBeatServer {
private final static Logger LOGGER = LoggerFactory.getLogger(HeartBeatServer.class);
private EventLoopGroup boss = new NioEventLoopGroup();
private EventLoopGroup work = new NioEventLoopGroup();
... ... @@ -35,15 +39,22 @@ public class HeartBeatServer {
* @return
* @throws InterruptedException
*/
@PostConstruct
public ChannelFuture start() throws InterruptedException {
ServerBootstrap bootstrap = new ServerBootstrap()
.group(boss, work)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(11211))
//保持长连接
.childOption(ChannelOption.SO_KEEPALIVE,true)
.childHandler(new HeartbeatInitializer());
ChannelFuture future = bootstrap.bind().sync();
if (future.isSuccess()){
LOGGER.info("启动 Netty 成功");
}
return future;
}
... ... @@ -51,8 +62,10 @@ public class HeartBeatServer {
/**
* 销毁
*/
@PreDestroy
public void destroy(){
boss.shutdownGracefully() ;
work.shutdownGracefully() ;
boss.shutdownGracefully().syncUninterruptibly() ;
work.shutdownGracefully().syncUninterruptibly() ;
LOGGER.info("关闭 Netty 成功");
}
}
... ...