惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

博客园 - 三驾马车

Claude Code 官宣:可以在 IDEA 用了! idea gitee 更新已取消 解决方案 ByteBuffer和ByteBuf区别 Marshalling.getProvidedMarshallerFactory("serial") 参数有那些 ProtobufVarint32FrameDecoder和ProtobufDecoder区别 protobuf 的 Varint 编码规范 netty initChannel ch.pipeline().addLast 先后顺序很重要 ChannelInboundHandlerAdapter 的channelRead和channelReadComplete的区别 Unpooled.buffer()和Unpooled.copiedBuffer区别 ServerBootstrap 和Bootstrap 区别 childhandler 和 handler 区别 ChannelInitializer<SocketChannel> 的作用详解 ChannelHandlerAdapter 和 ChannelInboundHandlerAdapter 的区别 SimpleChannelInboundHandler 中的 messageReceived 和 channelRead0 Application run failed .ParserException: while parsing a block mapping in 'reader' openssl genrsa 自签名ssl证书 上传本地项目到新建git项目 save download pdf
ChannelHandlerAdapter 与 ChannelInboundHandler 的区别
三驾马车 · 2025-05-13 · via 博客园 - 三驾马车

ChannelHandlerAdapter 与 ChannelInboundHandler 的区别

这两个是 Netty 中不同层次的处理器相关接口/类,主要区别如下:

1. 类型与层级关系

 ChannelHandlerAdapterChannelInboundHandler
类型 抽象适配器类 接口
继承/实现关系 实现 ChannelHandler 接口 继承 ChannelHandler 接口
位于的包 io.netty.channel io.netty.channel

2. 核心区别

ChannelHandlerAdapter

  • 是一个适配器基类(空实现)

  • 提供所有 ChannelHandler 方法的默认空实现

  • 主要用于不需要处理所有事件的处理器

ChannelInboundHandler

  • 是一个专门处理入站事件的接口

  • 定义了入站事件的处理方法(如 channelRead、channelActive 等)

  • 需要实现所有入站事件处理方法

3. 典型实现关系

ChannelHandler (接口)
  ↑
ChannelHandlerAdapter (抽象类,空实现)
  ↑
ChannelInboundHandlerAdapter (实现ChannelInboundHandler)
  ↑
SimpleChannelInboundHandler (简化实现)

4. 使用场景对比

使用 ChannelHandlerAdapter 的情况

  • 需要创建自定义处理器基类

  • 需要同时处理入站和出站事件

  • 只需要处理部分事件

实现 ChannelInboundHandler 的情况

  • 专门处理入站事件(如消息接收、连接建立等)

  • 需要明确实现所有入站事件处理方法

  • 通常通过继承 ChannelInboundHandlerAdapter 来实现

5. 代码示例对比

ChannelHandlerAdapter 使用方式

public class CustomHandler extends ChannelHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        

ChannelInboundHandler 实现方式

public class InboundHandler implements ChannelInboundHandler {
    

6. 实际应用建议

  • 大多数情况下:继承 ChannelInboundHandlerAdapter(它已经实现了 ChannelInboundHandler 接口并继承自 ChannelHandlerAdapter)

  • 需要最大灵活性时:直接实现 ChannelInboundHandler 接口

  • 创建基础处理器时:继承 ChannelHandlerAdapter

关键记忆点:Adapter 是提供默认实现的基类,而 InboundHandler 是定义入站处理规范的接口。