|
@@ -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,30 +23,36 @@ import java.net.InetSocketAddress; |
|
@@ -22,30 +23,36 @@ 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()
|
|
|
|
37
|
- .group(boss, work)
|
|
|
|
38
|
- .channel(NioServerSocketChannel.class)
|
|
|
|
39
|
- .localAddress(new InetSocketAddress(11211))
|
|
|
|
40
|
- .childHandler(new HeartbeatInitializer());
|
40
|
+ ServerBootstrap bootstrap = new ServerBootstrap()
|
|
|
|
41
|
+ .group(boss, work)
|
|
|
|
42
|
+ .channel(NioServerSocketChannel.class)
|
|
|
|
43
|
+ .localAddress(new InetSocketAddress(11211))
|
|
|
|
44
|
+ .childHandler(new HeartbeatInitializer());
|
|
|
|
45
|
+
|
|
|
|
46
|
+ ChannelFuture future = bootstrap.bind().sync();
|
|
|
|
47
|
+ return future;
|
|
|
|
48
|
+ }
|
|
41
|
|
49
|
|
|
42
|
- ChannelFuture future = bootstrap.bind().sync();
|
|
|
|
43
|
- future.channel().closeFuture().sync();
|
|
|
|
44
|
- return future;
|
|
|
|
45
|
- } catch (InterruptedException e) {
|
|
|
|
46
|
- } finally {
|
|
|
|
47
|
- }
|
|
|
|
48
|
|
50
|
|
|
49
|
- return null;
|
51
|
+ /**
|
|
|
|
52
|
+ * 销毁
|
|
|
|
53
|
+ */
|
|
|
|
54
|
+ public void destroy(){
|
|
|
|
55
|
+ boss.shutdownGracefully() ;
|
|
|
|
56
|
+ work.shutdownGracefully() ;
|
|
50
|
}
|
57
|
}
|
|
51
|
} |
58
|
} |