MessageToMessageDecoder and MessageToMessageEncoder.
Here is an example of a MessageToMessageCodec which just decode from Integer to Long
and encode from Long to Integer.
public class NumberCodec extends
MessageToMessageCodec<Integer, Long> {
@Override
public Long decode(ChannelHandlerContext ctx, Integer msg, List<Object> out)
throws Exception {
out.add(msg.longValue());
}
@Override
public Integer encode(ChannelHandlerContext ctx, Long msg, List<Object> out)
throws Exception {
out.add(msg.intValue());
}
}
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 MessageToMessageCodec will call
ReferenceCounted#release() on encoded / decoded messages.
| Modifier and Type | Field and Description |
|---|---|
| private final MessageToMessageDecoder | |
| private final MessageToMessageEncoder | |
| private final TypeParameterMatcher | |
| private final TypeParameterMatcher |
| Access | Constructor and Description |
|---|---|
| protected | MessageToMessageCodec()
Create a new instance which will try to detect the types to decode and encode out of the type parameter of the class. |
| protected | MessageToMessageCodec(Class<? extends INBOUND_IN>
The type of messages to decode inboundMessageType, Class<? extends OUTBOUND_IN> The type of messages to encode outboundMessageType)Create a new instance. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | acceptInboundMessage(Object
the message msg)Returns |
| public boolean | acceptOutboundMessage(Object
the message msg)Returns |
| public void | channelRead(ChannelHandlerContext ctx, Object msg)
Overrides default io. Channel has read a message from the peer.
|
| protected void | |
| protected void | |
| protected void | |
| protected void | |
| public Future | write(ChannelHandlerContext
the ctx, Object ChannelHandlerContext for which the write operation is madethe message to write msg)Overrides default io. |
| decoder | back to summary |
|---|---|
| private final MessageToMessageDecoder<Object> decoder | |
| encoder | back to summary |
|---|---|
| private final MessageToMessageEncoder<Object> encoder | |
| inboundMsgMatcher | back to summary |
|---|---|
| private final TypeParameterMatcher inboundMsgMatcher | |
| outboundMsgMatcher | back to summary |
|---|---|
| private final TypeParameterMatcher outboundMsgMatcher | |
| MessageToMessageCodec | back to summary |
|---|---|
| protected MessageToMessageCodec() Create a new instance which will try to detect the types to decode and encode out of the type parameter of the class. | |
| MessageToMessageCodec | back to summary |
|---|---|
| protected MessageToMessageCodec(Class<? extends INBOUND_IN> inboundMessageType, Class<? extends OUTBOUND_IN> outboundMessageType) Create a new instance. | |
| acceptInboundMessage | back to summary |
|---|---|
| public boolean acceptInboundMessage(Object msg) throws Exception Returns
| |
| acceptOutboundMessage | back to summary |
|---|---|
| public boolean acceptOutboundMessage(Object msg) throws Exception Returns
| |
| channelRead | back to summary |
|---|---|
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Overrides default io. Doc from io. Invoked when the current
| |
| decode | back to summary |
|---|---|
| protected void decode(ChannelHandlerContext ctx, INBOUND_IN msg) throws Exception | |
| decodeAndClose | back to summary |
|---|---|
| protected void decodeAndClose(ChannelHandlerContext ctx, INBOUND_IN msg) throws Exception | |
| encode | back to summary |
|---|---|
| protected void encode(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception | |
| encodeAndClose | back to summary |
|---|---|
| protected void encodeAndClose(ChannelHandlerContext ctx, OUTBOUND_IN msg, List<Object> out) throws Exception | |
| write | back to summary |
|---|---|
| public Future Overrides default io. Doc from io. Called once a write operation is made. The write operation will write the messages through the
| |