问题描述
Netty的JavaDocs解释ChannelLocal与ThreadLocal类似,但是我对它的用法有一些疑问. ThreadLocal是带有访问实例特定对象的静态方法的静态类. ChannelLocal不是静态的,具有静态的内部映射或静态的方法.该文档没有提供访问ChannelLocal或将对象放入ChannelLocal的示例,因此我希望有人可以让我对它的用法有所了解.
The JavaDocs for Netty explain ChannelLocal to be similar to ThreadLocal, however I've got some questions about it's usage. ThreadLocal is a static class with static methods that access instance-specific objects. ChannelLocal is not static, have a static internal map or have static methods. The documentation doesn't include an example of accessing ChannelLocal or placing an object into ChannelLocal, so I was hoping someone could give me some insight into it's usage.
谢谢!
推荐答案
ChannelLocal用于将某些数据分配给Channel.
The ChannelLocal is used to assign some data to a Channel.
这是一个例子:
// Declare
public static final ChannelLocal<Integer> data = new ChannelLocal<Integer>();
// Set
data.set(e.getChannel(), 1);
// Get
int a = data.get(e.getChannel());
下面是一些现实生活中的例子:
Here's a couple of real life example:
http://eucalyptus.sourcearchive.com/documentation /1.6.2/ServiceSinkHandler_8java-source.html
这篇关于Netty ChannelLocal的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!