这几天开始看netty,作些笔记。
netty5文档中 ChannelHandler开始处介绍了attachment的使用,用来保存handler的状态信息
public class DataServerHandler extends SimpleChannelInboundHandler<Message> {
private final AttributeKey<Boolean> auth =
new AttributeKey<Boolean>("auth");
@Override
public void channelRead(ChannelHandlerContext ctx, Integer integer) {
Attribute<Boolean> attr = ctx.getAttr(auth);
//...
}
AttributeKey<T>和Attribute<T>应该就是map中的键值对,而这个map应该绑定在ChannelHandlerContext中。
文档中对ChannelHandlerContext的描述:
A ChannelHandler is provided with a ChannelHandlerContext object. A ChannelHandler is supposed to interact with the ChannelPipeline it belongs to via a context object. Using the context object, the ChannelHandler can pass events upstream or downstream, modify the pipeline dynamically, or store the information (attachment) which is specific to the handler.
一个handler有一个与之对应的context,handler通过这个context对象与pipeline互动,
使用这个上下文对象,可以实现...保存某个handler的信息(attachment)。
查看源码:
DefaultChannelHandlerContext继承了DefaultAttributeMap
final class DefaultChannelHandlerContext extends DefaultAttributeMap implements ChannelHandlerContext{...}
DefaultAttributeMap的实现:
@SuppressWarnings("rawtypes")
private static final AtomicReferenceFieldUpdater<DefaultAttributeMap, Map> updater = AtomicReferenceFieldUpdater.newUpdater(DefaultAttributeMap.class, Map.class, "map");
private volatile Map<AttributeKey<?>, Attribute<?>> map;
@Override
public <T> Attribute<T> attr(AttributeKey<T> key) {
Map<AttributeKey<?>, Attribute<?>> map = this.map;
if (map == null) {
// Not using ConcurrentHashMap due to high memory consumption.
//创建一个初始值为2的IdentityHashMap,控制内存消耗
map = new IdentityHashMap<AttributeKey<?>, Attribute<?>>(2);
if (!updater.compareAndSet(this, null, map)) {
//如果原子更新失败,即执行到这里时map被其它线程赋值,而不是预期的
//null值,那就直接使用当前值
map = this.map;
}
}
//同步块,如果获得的attribute<T>是null,那就创建一个DefaultAttribute<T>
synchronized (map) {
@SuppressWarnings("unchecked")
Attribute<T> attr = (Attribute<T>) map.get(key);
if (attr == null) {
attr = new DefaultAttribute<T>(map, key);
map.put(key, attr);
}
return attr;
}
}
//......
可见作者在此处作了严密的控制,看到这里,心里充满了安全感
另外要注意的是:
因为在 ChannelHandlerContext 中的 AttibuteMap 是绑定在该上下文中的,所以你在某个上下文中定义的 Attribute 在另一个handler的上下文中是看不到的。
Channel 中也实现了 AttributeMap,你可以使用绑定在 Channel 中的 AttributeMap,用来在handler之间传递attachment。
ctx.channel().attr(...)...;
分享到:
相关推荐
Java进阶技术-netty进阶之路
Netty进阶之路,跟着案例学Netty,李林峰大神新作。值得一读。
《Netty进阶之路:跟着案例学Netty》中的案例涵盖了Netty的启动和停止、内存、并发多线程、性能、可靠性、安全等方面,囊括了Netty绝大多数常用的功能及容易让人犯错的地方。在案例的分析过程中,还穿插讲解了Netty...
Netty进阶之路 跟着案例学Netty 整本书无密码,Netty进阶之路 跟着案例学Netty
《Netty进阶之路 跟着案例学Netty》_李林锋_2018-11-01
精选自1000多个一线业务实际案例,从原理到实践全景式讲解Netty项目实践,快速领悟Netty专家花大量时间积累的经验,提高编程水平及分析解决问题的能力,《Netty木又威指南》作者力作,众专家力荐 Netty将Java NIO...
netty4.x的hello world 范例,可以运行,直接导入工程,需要依赖包netty-all-4.0.25.final.jar,可以去MAVEN仓库下载或者百度下载。
springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合 netty做心跳检测 springboot整合...
Netty基础,用于学习Netty,参考黑马程序员的netty教程
Netty之helloworld示列,客户端,服务器示列代码,欢迎初学者学习。
Netty (netty-netty-4.0.56.Final.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...
netty-buffer-4.1.32.Final-sources.jar netty-buffer-4.1.32.Final.jar netty-build-22-sources.jar netty-build-22.jar netty-codec-4.1.32.Final-sources.jar netty-codec-4.1.32.Final.jar netty-codec-...
- Netty之父”Trustin Lee作序推荐 - 阿里巴巴中间件高级技术专家为本书中文版作序推荐 - 系统而详细地介绍了Netty的各个方面并附带了即用型的优质示例 - 附带行业一线公司的案例研究 - 极实用的Netty技术书...
Netty (netty-netty-5.0.0.Alpha2.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...
跟闪电侠学Netty:Netty即时聊天实战与底层原理-book-netty
Netty (netty-netty-3.10.6.Final.tar.gz)是一个 NIO 客户端服务器框架,可以快速轻松地开发协议服务器和客户端等网络应用程序。它极大地简化和流线了网络编程,例如 TCP 和 UDP 套接字服务器。 “快速和简单”并...
netty学习,netty进阶之路:实战,案列,进阶知识
网络编程之Netty一站式精讲.rar
Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. Netty is a NIO client server framework which ...
netty-3.2.5.Final.jar netty包