netty超时
Netty 超时机制的介绍
Netty 的超时类型 IdleState 主要分为:
- ALL_IDLE : 一段时间内没有数据接收或者发送
- READER_IDLE : 一段时间内没有数据接收
- WRITER_IDLE : 一段时间内没有数据发送
在 Netty 的 timeout 包下,主要类有:
IdleStateEvent : 超时的事件
IdleStateHandler : 超时状态处理
ReadTimeoutHandler : 读超时状态处理
WriteTimeoutHandler : 写超时状态处理
VSEchoServer.java 启动netty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41package netty;
public class VSEchoServer {
private int port;
public VSEchoServer(int port){
this.port = port;
}
public void do_startServer(){
//System.out.println("do_startServer");
new Thread(){
@Override
public void run(){
//System.out.println("start..");
VSEchoServerCls esc = new VSEchoServerCls();
//System.out.println("start..2");
try{
esc.do_start(port);
//System.out.println("start..3");
}catch(Exception e){
}
//System.out.println("end..");
}
}.start();
}
public static void main(String[] args) throws InterruptedException {
new VSEchoServer(8001).do_startServer();
}
}VSEchoServerCls.java 启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.util.CharsetUtil;
/**
* Echoes back any received data from a client.
*/
class VSEchoServerCls {
static final boolean SSL = System.getProperty("ssl") != null;
public static boolean flag=true;
public void do_start(int port) throws Exception {
System.out.println("do_start..1");
// Configure SSL.
final SslContext sslCtx;
if (SSL) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
} else {
sslCtx = null;
}
//System.out.println("do_start..2");
// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO)).childHandler(new VSServerInitializer());
// Start the server.
ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}
finally {
// Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
VSEchoServerHandler.java
1 |
|
VSServerInitializer.java
1 |
|
模拟读超时可以用telnet
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!