|
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
|
} |