每次写Netty代码的时候第一行都会写下面的代码:

1
2
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker = new NioEventLoopGroup();

所以源码解析也从EventLoopGroup开始。该篇只是简单列举了几个接口,缺乏实现类所以功能描述上有所欠缺。

解析

EventLoopGroup

EventLoopGroup继承了EventExecutorGroup接口,而EventExecutorGroup接口继承了定时/延时任务ScheduledExecutorService接口,所以EventLoopGroup实现类会实现线程池接口。

EventLoopGroup继承关系

1
2
3
4
5
6
7
8
9
10
11
public interface EventLoopGroup extends EventExecutorGroup {
@Override
EventLoop next();
// 注册channel到EventLoop,注册完成时会通知(通过ChannelFuture)
ChannelFuture register(Channel channel);

ChannelFuture register(ChannelPromise promise);

@Deprecated
ChannelFuture register(Channel channel, ChannelPromise promise);
}

EventLoop

EventLoop继承关系

EventLoop继承了EventLoopGroup和OrderedEventExecutor。OrderedEventExecutor继承了EventExecutor接口未添加任何方法。

1
2
3
4
public interface EventLoop extends OrderedEventExecutor, EventLoopGroup {
@Override
EventLoopGroup parent();
}

EventExecutor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public interface EventExecutor extends EventExecutorGroup {

@Override
EventExecutor next();

EventExecutorGroup parent();

boolean inEventLoop();

boolean inEventLoop(Thread thread);

<V> Promise<V> newPromise();

<V> ProgressivePromise<V> newProgressivePromise();

<V> Future<V> newSucceededFuture(V result);

<V> Future<V> newFailedFuture(Throwable cause);
}

ChannelFuture

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
public interface ChannelFuture extends Future<Void> {

/**
* Returns a channel where the I/O operation associated with this
* future takes place.
*/
Channel channel();

@Override
ChannelFuture addListener(GenericFutureListener<? extends Future<? super Void>> listener);

@Override
ChannelFuture addListeners(GenericFutureListener<? extends Future<? super Void>>... listeners);

@Override
ChannelFuture removeListener(GenericFutureListener<? extends Future<? super Void>> listener);

@Override
ChannelFuture removeListeners(GenericFutureListener<? extends Future<? super Void>>... listeners);

@Override
ChannelFuture sync() throws InterruptedException;

@Override
ChannelFuture syncUninterruptibly();

@Override
ChannelFuture await() throws InterruptedException;

@Override
ChannelFuture awaitUninterruptibly();

/**
* Returns {@code true} if this {@link ChannelFuture} is a void future and so not allow to call any of the
* following methods:
* <ul>
* <li>{@link #addListener(GenericFutureListener)}</li>
* <li>{@link #addListeners(GenericFutureListener[])}</li>
* <li>{@link #await()}</li>
* <li>{@link #await(long, TimeUnit)} ()}</li>
* <li>{@link #await(long)} ()}</li>
* <li>{@link #awaitUninterruptibly()}</li>
* <li>{@link #sync()}</li>
* <li>{@link #syncUninterruptibly()}</li>
* </ul>
*/
boolean isVoid();
}

`