背景
- 在服务端使用Netty的一个标准做法:
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.WARN))
.childHandler(new MyServerInitalizer());
ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
channelFuture.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
- Netty使用抽象类AbstractBootstrap的方法channel(NioServerSocketChannel.class)是怎么获取到NioServerSocketChannel实例的?
核心类体系结构
- 把Netty源码关于如何获取NioServerSocketChannel的实例的核心类拆解出来,结构图如下:
- Channel代码
public interface Channel { }
- ChannelFactory代码
public interface ChannelFactory<T extends Channel>{
T newChannel();
}
- ReflectiveChannelFactory代码
public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T> {
private final Class<? extends T> clazz;
public ReflectiveChannelFactory(Class<? extends T> clazz) {
if (clazz == null) {
throw new NullPointerException("clazz");
}
this.clazz = clazz;
}
@Override
public T newChannel() {
try {
return clazz.getConstructor().newInstance();
} catch (Throwable t) {
throw new ChannelException("Unable to create Channel from class " + clazz, t);
}
}
@Override
public String toString() {
return StringUtil.simpleClassName(clazz) + ".class";
}
}
- AbstractBootstrap代码
public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C extends Channel> {
public volatile ChannelFactory<? extends C> channelFactory;
public B channel(Class<? extends C> channelClass) {
if (channelClass == null) {
throw new NullPointerException("channelClass");
}
return channelFactory(new ReflectiveChannelFactory<C>(channelClass));
}
public B channelFactory(ChannelFactory<? extends C> channelFactory) {
if (channelFactory == null) {
throw new NullPointerException("channelFactory");
}
if (this.channelFactory != null) {
throw new IllegalStateException("channelFactory set already");
}
this.channelFactory = channelFactory;
return (B) this;
}
}
- ServerBootstrap代码
public class ServerBootstrap extends AbstractBootstrap<ServerBootstrap, ServerChannel> {
}
- ServerScoketChannel代码
public interface ServerSocketChannel extends ServerChannel { }
- ServerChannel代码
public interface ServerChannel extends Channel { }
- NioServerSocketChannel代码
public class NioServerSocketChannel implements ServerSocketChannel { }
测试
- 测试代码
public class ReflectionTest {
public static void main(String[] args) {
ServerBootstrap serverBootstrap = new ServerBootstrap();
ChannelFactory beforeChannelFactory = serverBootstrap.channelFactory;
// null
System.out.println(beforeChannelFactory);
serverBootstrap.channel(NioServerSocketChannel.class);
ChannelFactory afterChannelFactory = serverBootstrap.channelFactory;
// NioServerSocketChannel.class
System.out.println(afterChannelFactory);
// com.slabs.netty.reflect.NioServerSocketChannel@4c873330
ServerChannel serverChannel = serverBootstrap.channelFactory.newChannel();
System.out.println(serverChannel);
}
}
-
测试结果
-
反射 + 泛型 + 工厂方法设计模式 实现把NioServerSocketChannel.class类型赋值给抽象类AbstractBootstrap的属性channelFactory。
-
关键代码:channelFactory(new ReflectiveChannelFactory(channelClass))
-
通过channelFactory.newChannel()的方法就可以拿到:NioServerSocketChannel实例对象。
-
通过这个反射获取NioServerSocketChannel实例的小demo,熟悉Netty的核心架构体系。