Seeing the headline, you might think, what does this have to do with Netty? Indeed, if you are completely using Netty, then you may not need to know selector at all. However, it must be mentioned that the implementation of the Netty underlying NIO is also based on Java selector, which is the encapsulation of selector. Therefore, I personally think that understanding the good selector for the use and understanding of Netty are a lot of helpful. Of course, if you really don't care about these, just want to use Netty on it. So below, you can skip:)
The author for selector is also new to learn. A lot of new people talked to me before, and they talked about the use and the start-up of a framework or a new open source tool. They will find it difficult to get started and consume events. However, I have never felt this way. Here just, borrow selector learning process, communicate with you, I started the process of www.it165.net.
To use a tool, it is natural to understand its positioning, the principle of problem solving, or the workflow. Therefore, the author first from the Internet to understand the selector about the work flow.
NIO has a major class selector, which resembles an observer, so long as we tell selector what we need to know, we go on to do something else, and when something happens, he informs us, returns a group of Selectionkey, We read these keys, we get the socketchannel we just registered, and then we read the data from the channel, and we can read the packet, and then we could process the data.
This is an excerpt from the author of a short summary, on this small section of the basic already can explain the problem. The next thing we want to consider is, what do we need to do to implement this process? Along the description, we can imagine that a selector is required, a message-transmitting channel is required, and an event needs to be registered for identification. The channel naturally needs to be bound to an address. With such a general idea, we can go to the API to find the relevant interface.
Selector Server Sample code:
View Sourceprint?
01.
/**
02.
* Java NIO Select模式服务端样例代码
03.
*
04.
* @author lihzh
05.
* @alia OneCoder
06.
* @Blog http://www.it165.net
07.
* @date 2012-7-16 下午9:22:53
08.
*/
09.
public
class
NioSelectorServer {
10.
11.
/**
12.
* @author lihzh
13.
* @throws IOException
14.
* @alia OneCoder
15.
* @date 2012-7-16 下午9:22:53
16.
*/
17.
public
static
void
main(String[] args)
throws
IOException {
18.
// 创建一个selector选择器
19.
Selector selector = Selector.open();
20.
// 打开一个通道
21.
ServerSocketChannel socketChannel = ServerSocketChannel.open();
22.
// 绑定到9000端口
23.
socketChannel.socket().bind(
new
InetSocketAddress(
8000
));
24.
// 使设定non-blocking的方式。
25.
socketChannel.configureBlocking(
false
);
26.
// 向Selector注册Channel及我们有兴趣的事件
27.
socketChannel.register(selector, SelectionKey.OP_ACCEPT);
28.
for
(;;) {
29.
// 选择事件
30.
selector.select();
31.
// 当有客户端准备连接到服务端时,便会出发请求
32.
for
(Iterator<SelectionKey> keyIter = selector.selectedKeys()
33.
.iterator(); keyIter.hasNext();) {
34.
SelectionKey key = keyIter.next();
35.
keyIter.remove();
36.
System.out.println(key.readyOps());
37.
if
(key.isAcceptable()) {
38.
System.out.println(
"Accept"
);
39.
// 接受连接到此Channel的连接
40.
socketChannel.accept();
41.
}
42.
}
43.
}
44.
}
45.
}
Selector Client Sample code:
View Sourceprint?
01.
/**
02.
* Java NIO Selector模式,客户端代码
03.
*
04.
* @author lihzh
05.
* @alia OneCoder
06.
* @blog http://www.it165.net
07.
*/
08.
public
class
NioSelectorClient {
09.
10.
/**
11.
* @author lihzh
12.
* @throws IOException
13.
* @alia OneCoder
14.
*/
15.
public
static
void
main(String[] args)
throws
IOException {
16.
SocketChannel channel = SocketChannel.open();
17.
channel.configureBlocking(
false
);
18.
channel.connect(
new
InetSocketAddress(
"127.0.0.1"
,
8000
));
19.
}
20.
}
The code is simple, and after the server receives a connection request from the client, the "accept" message is printed.
A simple summary is that the whole channel, channel plus a selection of filters, it seems that the event is not what I want, do not want to simply regardless, want, I will save up, keep slowly deal with.
Now feel is not netty really with this mechanism, if let you go to achieve Netty first function, also have thought can think of it.
Java NIO Framework Netty Tutorial (vi) Java NIO selector mode