作者 crossoverJie

:recycle: 重构代码

@@ -13,29 +13,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -13,29 +13,13 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
13 * @author crossoverJie 13 * @author crossoverJie
14 */ 14 */
15 @SpringBootApplication 15 @SpringBootApplication
16 -public class Application implements CommandLineRunner{ 16 +public class Application{
17 17
18 private final static Logger LOGGER = LoggerFactory.getLogger(Application.class); 18 private final static Logger LOGGER = LoggerFactory.getLogger(Application.class);
19 19
20 - @Autowired  
21 - private HeartBeatServer heartBeatServer ;  
22 -  
23 public static void main(String[] args) { 20 public static void main(String[] args) {
24 SpringApplication.run(Application.class, args); 21 SpringApplication.run(Application.class, args);
25 LOGGER.info("启动成功"); 22 LOGGER.info("启动成功");
26 } 23 }
27 24
28 - @Override  
29 - public void run(String... args) throws Exception {  
30 - ChannelFuture future = heartBeatServer.start();  
31 -  
32 - Runtime.getRuntime().addShutdownHook(new Thread(){  
33 - @Override  
34 - public void run() {  
35 - heartBeatServer.destroy();  
36 - }  
37 - });  
38 -  
39 - future.channel().closeFuture().syncUninterruptibly() ;  
40 - }  
41 } 25 }
@@ -2,6 +2,7 @@ package com.crossoverjie.netty.action.handle; @@ -2,6 +2,7 @@ package com.crossoverjie.netty.action.handle;
2 2
3 import com.crossoverjie.netty.action.pojo.CustomProtocol; 3 import com.crossoverjie.netty.action.pojo.CustomProtocol;
4 import io.netty.buffer.Unpooled; 4 import io.netty.buffer.Unpooled;
  5 +import io.netty.channel.ChannelFutureListener;
5 import io.netty.channel.ChannelHandlerContext; 6 import io.netty.channel.ChannelHandlerContext;
6 import io.netty.channel.SimpleChannelInboundHandler; 7 import io.netty.channel.SimpleChannelInboundHandler;
7 import io.netty.handler.timeout.IdleState; 8 import io.netty.handler.timeout.IdleState;
@@ -32,7 +33,8 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<CustomPro @@ -32,7 +33,8 @@ public class HeartBeatSimpleHandle extends SimpleChannelInboundHandler<CustomPro
32 LOGGER.info("已经5秒没有收到信息!"); 33 LOGGER.info("已经5秒没有收到信息!");
33 //向客户端发送消息 34 //向客户端发送消息
34 CustomProtocol customProtocol = new CustomProtocol(12345L,"pong") ; 35 CustomProtocol customProtocol = new CustomProtocol(12345L,"pong") ;
35 - ctx.writeAndFlush(Unpooled.copiedBuffer(customProtocol.toString(), CharsetUtil.UTF_8)) ; 36 + ctx.writeAndFlush(Unpooled.copiedBuffer(customProtocol.toString(), CharsetUtil.UTF_8))
  37 + .addListener(ChannelFutureListener.CLOSE_ON_FAILURE) ;
36 } 38 }
37 39
38 40
1 package com.crossoverjie.netty.action.server; 1 package com.crossoverjie.netty.action.server;
2 2
  3 +import com.crossoverjie.netty.action.Application;
3 import com.crossoverjie.netty.action.channel.init.HeartbeatInitializer; 4 import com.crossoverjie.netty.action.channel.init.HeartbeatInitializer;
4 import io.netty.bootstrap.ServerBootstrap; 5 import io.netty.bootstrap.ServerBootstrap;
5 -import io.netty.channel.Channel;  
6 -import io.netty.channel.ChannelFuture;  
7 -import io.netty.channel.ChannelInitializer;  
8 -import io.netty.channel.EventLoopGroup; 6 +import io.netty.channel.*;
9 import io.netty.channel.nio.NioEventLoopGroup; 7 import io.netty.channel.nio.NioEventLoopGroup;
10 import io.netty.channel.socket.nio.NioServerSocketChannel; 8 import io.netty.channel.socket.nio.NioServerSocketChannel;
11 import io.netty.handler.timeout.IdleStateHandler; 9 import io.netty.handler.timeout.IdleStateHandler;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
12 import org.springframework.beans.factory.annotation.Configurable; 12 import org.springframework.beans.factory.annotation.Configurable;
13 import org.springframework.context.annotation.Bean; 13 import org.springframework.context.annotation.Bean;
14 import org.springframework.context.annotation.Configuration; 14 import org.springframework.context.annotation.Configuration;
15 import org.springframework.stereotype.Component; 15 import org.springframework.stereotype.Component;
16 16
  17 +import javax.annotation.PostConstruct;
  18 +import javax.annotation.PreDestroy;
17 import java.net.InetSocketAddress; 19 import java.net.InetSocketAddress;
18 20
19 /** 21 /**
@@ -26,6 +28,8 @@ import java.net.InetSocketAddress; @@ -26,6 +28,8 @@ import java.net.InetSocketAddress;
26 @Component 28 @Component
27 public class HeartBeatServer { 29 public class HeartBeatServer {
28 30
  31 + private final static Logger LOGGER = LoggerFactory.getLogger(HeartBeatServer.class);
  32 +
29 private EventLoopGroup boss = new NioEventLoopGroup(); 33 private EventLoopGroup boss = new NioEventLoopGroup();
30 private EventLoopGroup work = new NioEventLoopGroup(); 34 private EventLoopGroup work = new NioEventLoopGroup();
31 35
@@ -35,15 +39,22 @@ public class HeartBeatServer { @@ -35,15 +39,22 @@ public class HeartBeatServer {
35 * @return 39 * @return
36 * @throws InterruptedException 40 * @throws InterruptedException
37 */ 41 */
  42 + @PostConstruct
38 public ChannelFuture start() throws InterruptedException { 43 public ChannelFuture start() throws InterruptedException {
39 44
40 ServerBootstrap bootstrap = new ServerBootstrap() 45 ServerBootstrap bootstrap = new ServerBootstrap()
41 .group(boss, work) 46 .group(boss, work)
42 .channel(NioServerSocketChannel.class) 47 .channel(NioServerSocketChannel.class)
43 .localAddress(new InetSocketAddress(11211)) 48 .localAddress(new InetSocketAddress(11211))
  49 + //保持长连接
  50 + .childOption(ChannelOption.SO_KEEPALIVE,true)
44 .childHandler(new HeartbeatInitializer()); 51 .childHandler(new HeartbeatInitializer());
45 52
46 ChannelFuture future = bootstrap.bind().sync(); 53 ChannelFuture future = bootstrap.bind().sync();
  54 + if (future.isSuccess()){
  55 + LOGGER.info("启动 Netty 成功");
  56 + }
  57 +
47 return future; 58 return future;
48 } 59 }
49 60
@@ -51,8 +62,10 @@ public class HeartBeatServer { @@ -51,8 +62,10 @@ public class HeartBeatServer {
51 /** 62 /**
52 * 销毁 63 * 销毁
53 */ 64 */
  65 + @PreDestroy
54 public void destroy(){ 66 public void destroy(){
55 - boss.shutdownGracefully() ;  
56 - work.shutdownGracefully() ; 67 + boss.shutdownGracefully().syncUninterruptibly() ;
  68 + work.shutdownGracefully().syncUninterruptibly() ;
  69 + LOGGER.info("关闭 Netty 成功");
57 } 70 }
58 } 71 }