Netty源码分析之ChannelOption与AttributeKey组件分析
第一步看源码过程分析

我们来看看第一段的内容
//ChannelOption init启动我们分析ChannelOption的作用
final Map<ChannelOption<?>, Object> options = options0();
synchronized (options) {
//将我们传递的参数信息都添加到Channel里面
setChannelOptions(channel, options, logger);
}
好的我们看看ChannelOption的作用以及组件顾名思义,它的意思肯定是Channel的配置信息那么它到底配置了什么信息呢 ?我们接着往下看它的Doc是怎么说的然后它的类里面有若干个配置信息信息主要是一些TCP的信息:
/**
* A {@link ChannelOption} allows to configure a {@link ChannelConfig} in a type-safe
* way. Which {@link ChannelOption} is supported depends on the actual implementation
* of {@link ChannelConfig} and may depend on the nature of the transport it belongs
* to.
*
* @param <T> the type of the value which is valid for the {@link ChannelOption}
* 主要配置的是关于网络的一些TCP的设置项配置信息 T 表示的就是某一个设置对应的类型
*/

构造函数里面的参数,
追到它的super里面我们发现,里面提供了一个id,name,同时还有一个id 的唯一标识 同时它的操作时线程安全类型的
/**
* Creates a new instance.
*/
protected AbstractConstant(int id, String name) {
this.id = id;
this.name = name;
this.uniquifier = uniqueIdGenerator.getAndIncrement();
}
同时我们好在ChannelOption 类中 发现了一个ConstantPool的常量我们看一下他作用
/**
* A pool of {@link Constant}s.
*
* @param <T> the type of the constant
* ,T表示类型 创建管理常量池
*/
它说的很清楚,他是这个类的常量池同时我们看一下DefaultChannelConfig 这个类
public <T> T getOption(ChannelOption<T> option) {
if (option == null) {
throw new NullPointerException("option");
}
if (option == CONNECT_TIMEOUT_MILLIS) {
return (T) Integer.valueOf(getConnectTimeoutMillis());
}
if (option == MAX_MESSAGES_PER_READ) {
return (T) Integer.valueOf(getMaxMessagesPerRead());
}
if (option == WRITE_SPIN_COUNT) {
return (T) Integer.valueOf(getWriteSpinCount());
}
if (option == ALLOCATOR) {
return (T) getAllocator();
}
也就是说我们前面传的泛型 ChannelOption 这个泛型它会根据所传的类型进行判断并完成赋值操作.
我们看看AttributeKey这个类的作用
他是在什么时候出现呢?也是我们的Init方法里面
//AttributeKey 业务数据随着业务流程实现了.数据流转进行 也就是Bootstorp中启动的参数数据的传递
final Map<AttributeKey<?>, Object> attrs = attrs0();
synchronized (attrs) {
for (Entry<AttributeKey<?>, Object> e: attrs.entrySet()) {
@SuppressWarnings("unchecked")
AttributeKey<Object> key = (AttributeKey<Object>) e.getKey();
channel.attr(key).set(e.getValue());
}
}
AttributeKey 类型作用主要是用来传递我们的业务参数,也就相当于activiti里面的参数也就是启动的时候存进来,启动之后在某一地方可以用
如我们的例子里面一样 :

892




被折叠的 条评论
为什么被折叠?



