public abstract class MessageToMessageEncoder<I> extends ChannelOutboundHandlerAdapter
ChannelOutboundHandlerAdapter which encodes from one message to an other message
For example here is an implementation which decodes an Integer to an String.
public class IntegerToStringEncoder extends
MessageToMessageEncoder<Integer> {
@Override
public void encode(ChannelHandlerContext ctx, Integer message, List<Object> out)
throws Exception {
out.add(message.toString());
}
}
Be aware that you need to call ReferenceCounted.retain() on messages that are just passed through if they
are of type ReferenceCounted. This is needed as the MessageToMessageEncoder will call
ReferenceCounted.release() on encoded messages.ChannelHandler.Sharable| 限定符 | 构造器和说明 |
|---|---|
protected |
MessageToMessageEncoder()
Create a new instance which will try to detect the types to match out of the type parameter of the class.
|
protected |
MessageToMessageEncoder(java.lang.Class<? extends I> outboundMessageType)
Create a new instance
|
| 限定符和类型 | 方法和说明 |
|---|---|
boolean |
acceptOutboundMessage(java.lang.Object msg)
Returns
true if the given message should be handled. |
protected abstract void |
encode(ChannelHandlerContext ctx,
I msg,
java.util.List<java.lang.Object> out)
Encode from one message to an other.
|
void |
write(ChannelHandlerContext ctx,
java.lang.Object msg,
ChannelPromise promise)
Calls
ChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline. |
bind, close, connect, deregister, disconnect, flush, readensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexceptionCaught, handlerAdded, handlerRemovedprotected MessageToMessageEncoder()
protected MessageToMessageEncoder(java.lang.Class<? extends I> outboundMessageType)
outboundMessageType - The type of messages to match and so encodepublic boolean acceptOutboundMessage(java.lang.Object msg)
throws java.lang.Exception
true if the given message should be handled. If false it will be passed to the next
ChannelOutboundHandler in the ChannelPipeline.java.lang.Exceptionpublic void write(ChannelHandlerContext ctx, java.lang.Object msg, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerAdapterChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.write 在接口中 ChannelOutboundHandlerwrite 在类中 ChannelOutboundHandlerAdapterctx - the ChannelHandlerContext for which the write operation is mademsg - the message to writepromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occursprotected abstract void encode(ChannelHandlerContext ctx, I msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
ctx - the ChannelHandlerContext which this MessageToMessageEncoder belongs tomsg - the message to encode to an other oneout - the List into which the encoded msg should be added
needs to do some kind of aggregationjava.lang.Exception - is thrown if an error occurs