public abstract class MessageToMessageDecoder<I> extends ChannelInboundHandlerAdapter
ChannelInboundHandlerAdapter从一个消息解码到另一个消息。
例如,这里是将String解码为代表Integer长度的String 。
public class StringToIntegerDecoder extends
MessageToMessageDecoder<String> {
@Override
public void decode(ChannelHandlerContext ctx, String message,
List<Object> out) throws 异常 {
out.add(message.length());
}
}
要知道,你需要调用ReferenceCounted.retain()对刚刚通过,如果他们是类型传递的消息ReferenceCounted 。
这是需要的,因为MessageToMessageDecoder将在解码的消息上调用ReferenceCounted.release() 。
ChannelHandler.Sharable| Modifier | Constructor and Description |
|---|---|
protected |
MessageToMessageDecoder()
创建一个新实例,它将尝试检测类型以匹配该类的类型参数。
|
protected |
MessageToMessageDecoder(java.lang.Class<? extends I> inboundMessageType)
创建一个新的实例
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
acceptInboundMessage(java.lang.Object msg)
如果应该处理给定的消息,则返回
true 。
|
void |
channelRead(ChannelHandlerContext ctx, java.lang.Object msg)
|
protected abstract void |
decode(ChannelHandlerContext ctx, I msg, java.util.List<java.lang.Object> out)
从一条消息解码到另一条消息。
|
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredensureNotSharable, handlerAdded, handlerRemoved, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waithandlerAdded, handlerRemovedprotected MessageToMessageDecoder()
protected MessageToMessageDecoder(java.lang.Class<? extends I> inboundMessageType)
inboundMessageType - 要匹配并解码的消息类型
public boolean acceptInboundMessage(java.lang.Object msg)
throws java.lang.Exception
true 。
如果false它将被传递到ChannelPipeline中的下一个ChannelInboundHandler 。
java.lang.Exception
public void channelRead(ChannelHandlerContext ctx, java.lang.Object msg) throws java.lang.Exception
ChannelInboundHandlerAdapter
ChannelHandlerContext.fireChannelRead(Object)转发至ChannelPipeline中的下一个ChannelInboundHandler 。
子类可以重写此方法来更改行为。
channelRead在界面
ChannelInboundHandler
channelRead在课堂上
ChannelInboundHandlerAdapter
java.lang.Exception
protected abstract void decode(ChannelHandlerContext ctx, I msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
ctx - MessageToMessageDecoder所属的ChannelHandlerContext
msg - 解码到另一个的消息
out - 应该添加解码消息的
List
java.lang.Exception - 发生错误时抛出
Copyright © 2008–2018 The Netty Project. All rights reserved.