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 extendsBe aware that you need to callMessageToMessageEncoder
<Integer
> {@Override
public void encode(ChannelHandlerContext
ctx,Integer
message, List<Object> out) throwsException
{ out.add(message.toString()); } }
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
Modifier | Constructor and Description |
---|---|
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(Class<? extends I> outboundMessageType)
Create a new instance
|
Modifier and Type | Method and Description |
---|---|
boolean |
acceptOutboundMessage(Object msg)
Returns
true if the given message should be handled. |
protected abstract void |
encode(ChannelHandlerContext ctx,
I msg,
List<Object> out)
Encode from one message to an other.
|
void |
write(ChannelHandlerContext ctx,
Object msg,
ChannelPromise promise)
Calls
ChannelOutboundInvoker.write(Object, ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline . |
bind, close, connect, deregister, disconnect, flush, read
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
exceptionCaught, handlerAdded, handlerRemoved
protected MessageToMessageEncoder()
public boolean acceptOutboundMessage(Object msg) throws Exception
true
if the given message should be handled. If false
it will be passed to the next
ChannelOutboundHandler
in the ChannelPipeline
.Exception
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception
ChannelOutboundHandlerAdapter
ChannelOutboundInvoker.write(Object, ChannelPromise)
to forward
to the next ChannelOutboundHandler
in the ChannelPipeline
.
Sub-classes may override this method to change behavior.write
in interface ChannelOutboundHandler
write
in class ChannelOutboundHandlerAdapter
ctx
- the ChannelHandlerContext
for which the write operation is mademsg
- the message to writepromise
- the ChannelPromise
to notify once the operation completesException
- thrown if an error occursprotected abstract void encode(ChannelHandlerContext ctx, I msg, List<Object> out) throws 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 aggregationException
- is thrown if an error occursCopyright © 2008–2024 The Netty Project. All rights reserved.