作者 crossoverJie

:sparkles: Introducing new features.优化启动

1 package com.crossoverjie.netty.action; 1 package com.crossoverjie.netty.action;
2 2
  3 +import com.crossoverjie.netty.action.server.HeartBeatServer;
3 import io.netty.channel.ChannelFuture; 4 import io.netty.channel.ChannelFuture;
4 import org.slf4j.Logger; 5 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
@@ -16,6 +17,8 @@ public class Application implements CommandLineRunner{ @@ -16,6 +17,8 @@ public class Application implements CommandLineRunner{
16 17
17 private final static Logger LOGGER = LoggerFactory.getLogger(Application.class); 18 private final static Logger LOGGER = LoggerFactory.getLogger(Application.class);
18 19
  20 + @Autowired
  21 + private HeartBeatServer heartBeatServer ;
19 22
20 public static void main(String[] args) { 23 public static void main(String[] args) {
21 SpringApplication.run(Application.class, args); 24 SpringApplication.run(Application.class, args);
@@ -24,6 +27,15 @@ public class Application implements CommandLineRunner{ @@ -24,6 +27,15 @@ public class Application implements CommandLineRunner{
24 27
25 @Override 28 @Override
26 public void run(String... args) throws Exception { 29 public void run(String... args) throws Exception {
  30 + ChannelFuture future = heartBeatServer.start();
27 31
  32 + Runtime.getRuntime().addShutdownHook(new Thread(){
  33 + @Override
  34 + public void run() {
  35 + heartBeatServer.destroy();
  36 + }
  37 + });
  38 +
  39 + future.channel().closeFuture().syncUninterruptibly() ;
28 } 40 }
29 } 41 }
@@ -12,6 +12,7 @@ import io.netty.handler.timeout.IdleStateHandler; @@ -12,6 +12,7 @@ import io.netty.handler.timeout.IdleStateHandler;
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 16
16 import java.net.InetSocketAddress; 17 import java.net.InetSocketAddress;
17 18
@@ -22,16 +23,19 @@ import java.net.InetSocketAddress; @@ -22,16 +23,19 @@ import java.net.InetSocketAddress;
22 * Date: 21/05/2018 00:30 23 * Date: 21/05/2018 00:30
23 * @since JDK 1.8 24 * @since JDK 1.8
24 */ 25 */
25 -@Configuration 26 +@Component
26 public class HeartBeatServer { 27 public class HeartBeatServer {
27 28
28 - @Bean  
29 - public ChannelFuture buildFuture() { 29 + private EventLoopGroup boss = new NioEventLoopGroup();
  30 + private EventLoopGroup work = new NioEventLoopGroup();
30 31
31 - EventLoopGroup boss = new NioEventLoopGroup();  
32 - EventLoopGroup work = new NioEventLoopGroup();  
33 32
34 - try { 33 + /**
  34 + * 启动 Netty
  35 + * @return
  36 + * @throws InterruptedException
  37 + */
  38 + public ChannelFuture start() throws InterruptedException {
35 39
36 ServerBootstrap bootstrap = new ServerBootstrap() 40 ServerBootstrap bootstrap = new ServerBootstrap()
37 .group(boss, work) 41 .group(boss, work)
@@ -40,12 +44,15 @@ public class HeartBeatServer { @@ -40,12 +44,15 @@ public class HeartBeatServer {
40 .childHandler(new HeartbeatInitializer()); 44 .childHandler(new HeartbeatInitializer());
41 45
42 ChannelFuture future = bootstrap.bind().sync(); 46 ChannelFuture future = bootstrap.bind().sync();
43 - future.channel().closeFuture().sync();  
44 return future; 47 return future;
45 - } catch (InterruptedException e) {  
46 - } finally {  
47 } 48 }
48 49
49 - return null; 50 +
  51 + /**
  52 + * 销毁
  53 + */
  54 + public void destroy(){
  55 + boss.shutdownGracefully() ;
  56 + work.shutdownGracefully() ;
50 } 57 }
51 } 58 }