forked from mood321/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNioClient.java
More file actions
75 lines (71 loc) · 2.16 KB
/
Copy pathNioClient.java
File metadata and controls
75 lines (71 loc) · 2.16 KB
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
package lbtech.json;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Netty 客户端代码
*
*/
public class NioClient {
/**
* 日志对象
*/
protected Logger logger = LoggerFactory.getLogger(NioClient.class);
private static String host = "127.0.0.1";
private static int port = 9991;
public static Channel channel = null;
public static void main(String args[]) {
ExecutorService executorService = Executors.newFixedThreadPool(10000);
for(int i=0;i<1;i++) {
executorService.execute(new Runnable() {
public void run() {
start();
}
});
System.out.println("当前是第:" + i + " 个");
}
}
/**
* 启动
*/
public static void start(){
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap(); // (1)
b.group(workerGroup); // (2)
b.channel(NioSocketChannel.class); // (3)
b.option(ChannelOption.SO_KEEPALIVE, true); // (4)
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("encode",new StringEncoder());
ch.pipeline().addLast("decode",new StringDecoder());
ch.pipeline().addLast(new NioClientHandler());
channel = ch;
}
});
// Start the client.
ChannelFuture f = b.connect(host, port);
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
}
}
}