Class MessageAggregator<I, S, C extends ByteBufHolder, O extends ByteBufHolder>
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.handler.codec.MessageToMessageDecoder<I>
io.netty.handler.codec.MessageAggregator<I,S,C,O>
- Type Parameters:
I- the type that covers both start message and content messageS- the type of the start messageC- the type of the content message (must be a subtype ofByteBufHolder)O- the type of the aggregated message (must be a subtype ofSandByteBufHolder)
- All Implemented Interfaces:
ChannelHandler, ChannelInboundHandler
- Direct Known Subclasses:
AbstractMemcacheObjectAggregator, HttpObjectAggregator, RedisBulkStringAggregator, StompSubframeAggregator, WebSocketFrameAggregator
public abstract class MessageAggregator<I, S, C extends ByteBufHolder, O extends ByteBufHolder>
extends MessageToMessageDecoder<I>
An abstract
ChannelHandler that aggregates a series of message objects into a single aggregated message.
'A series of messages' is composed of the following:
- a single start message which optionally contains the first part of the content, and
- 1 or more content messages.
isLastContentMessage(ByteBufHolder)
return true for, the aggregator will finish the aggregation and produce the aggregated message and expect
another start message.
-
Nested Class Summary
Nested classes/interfaces inherited from interface ChannelHandler
ChannelHandler.Sharable -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedMessageAggregator(int maxContentLength) Creates a new instance.protectedMessageAggregator(int maxContentLength, Class<? extends I> inboundMessageType) -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif the given message should be handled.protected voidTransfers the information provided by the specified content message to the specified aggregated message.protected abstract ObeginAggregation(S start, ByteBuf content) Creates a new aggregated message from the specified start message and the specified content.voidCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline.protected abstract booleanDetermine if the channel should be closed after the result ofnewContinueResponse(Object, int, ChannelPipeline)is written.protected final ChannelHandlerContextctx()protected voiddecode(ChannelHandlerContext ctx, I msg, List<Object> out) Decode from one message to an other.protected voidfinishAggregation(O aggregated) Invoked when the specifiedaggregatedmessage is about to be passed to the next handler in the pipeline.protected voidhandleOversizedMessage(ChannelHandlerContext ctx, S oversized) Invoked when an incoming request exceeds the maximum content length.voidDo nothing by default, sub-classes may override this method.voidDo nothing by default, sub-classes may override this method.protected abstract booleanDetermine if all objects for the current request/response should be ignored or not.protected abstract booleanisAggregated(I msg) Returnstrueif and only if the specified message is already aggregated.protected abstract booleanisContentLengthInvalid(S start, int maxContentLength) Determine if the messagestart's content length is known, and if it greater thanmaxContentLength.protected abstract booleanisContentMessage(I msg) Returnstrueif and only if the specified message is a content message.final booleanDeprecated.This method will be removed in future releases.protected abstract booleanisLastContentMessage(C msg) Returnstrueif and only if the specified message is the last content message.protected abstract booleanisStartMessage(I msg) Returnstrueif and only if the specified message is a start message.final intReturns the maximum allowed length of the aggregated message in bytes.final intReturns the maximum number of components in the cumulation buffer.protected abstract ObjectnewContinueResponse(S start, int maxContentLength, ChannelPipeline pipeline) Returns the 'continue response' for the specified start message if necessary.protected final voidfinal voidsetMaxCumulationBufferComponents(int maxCumulationBufferComponents) Sets the maximum number of components in the cumulation buffer.Methods inherited from class MessageToMessageDecoder
channelReadMethods inherited from class ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredMethods inherited from class ChannelHandlerAdapter
ensureNotSharable, isSharable
-
Constructor Details
-
MessageAggregator
protected MessageAggregator(int maxContentLength) Creates a new instance.- Parameters:
maxContentLength- the maximum length of the aggregated content. If the length of the aggregated content exceeds this value,handleOversizedMessage(ChannelHandlerContext, Object)will be called.
-
MessageAggregator
-
-
Method Details
-
acceptInboundMessage
Description copied from class:MessageToMessageDecoderReturnstrueif the given message should be handled. Iffalseit will be passed to the nextChannelInboundHandlerin theChannelPipeline.- Overrides:
acceptInboundMessagein classMessageToMessageDecoder<I>- Throws:
Exception
-
isStartMessage
-
isContentMessage
-
isLastContentMessage
Returnstrueif and only if the specified message is the last content message. Typically, this method is implemented as a singlereturnstatement withinstanceof:return msg instanceof MyLastContentMessage;
or withinstanceofand boolean field check:return msg instanceof MyContentMessage invalid input: '&'invalid input: '&' msg.isLastFragment();
- Throws:
Exception
-
isAggregated
-
maxContentLength
public final int maxContentLength()Returns the maximum allowed length of the aggregated message in bytes. -
maxCumulationBufferComponents
public final int maxCumulationBufferComponents()Returns the maximum number of components in the cumulation buffer. If the number of the components in the cumulation buffer exceeds this value, the components of the cumulation buffer are consolidated into a single component, involving memory copies. The default value of this property is 1024. -
setMaxCumulationBufferComponents
public final void setMaxCumulationBufferComponents(int maxCumulationBufferComponents) Sets the maximum number of components in the cumulation buffer. If the number of the components in the cumulation buffer exceeds this value, the components of the cumulation buffer are consolidated into a single component, involving memory copies. The default value of this property is 1024 and its minimum allowed value is2. -
isHandlingOversizedMessage
Deprecated.This method will be removed in future releases. -
ctx
-
decode
Description copied from class:MessageToMessageDecoderDecode from one message to an other. This method will be called for each written message that can be handled by this decoder.- Specified by:
decodein classMessageToMessageDecoder<I>- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToMessageDecoderbelongs tomsg- the message to decode to an other oneout- theListto which decoded messages should be added- Throws:
Exception- is thrown if an error occurs
-
isContentLengthInvalid
Determine if the messagestart's content length is known, and if it greater thanmaxContentLength.- Parameters:
start- The message which may indicate the content length.maxContentLength- The maximum allowed content length.- Returns:
trueif the messagestart's content length is known, and if it greater thanmaxContentLength.falseotherwise.- Throws:
Exception
-
newContinueResponse
protected abstract Object newContinueResponse(S start, int maxContentLength, ChannelPipeline pipeline) throws Exception Returns the 'continue response' for the specified start message if necessary. For example, this method is useful to handle an HTTP 100-continue header.- Returns:
- the 'continue response', or
nullif there's no message to send - Throws:
Exception
-
closeAfterContinueResponse
Determine if the channel should be closed after the result ofnewContinueResponse(Object, int, ChannelPipeline)is written.- Parameters:
msg- The return value fromnewContinueResponse(Object, int, ChannelPipeline).- Returns:
trueif the channel should be closed after the result ofnewContinueResponse(Object, int, ChannelPipeline)is written.falseotherwise.- Throws:
Exception
-
ignoreContentAfterContinueResponse
Determine if all objects for the current request/response should be ignored or not. Messages will stop being ignored the next timeisContentMessage(Object)returnstrue.- Parameters:
msg- The return value fromnewContinueResponse(Object, int, ChannelPipeline).- Returns:
trueif all objects for the current request/response should be ignored or not.falseotherwise.- Throws:
Exception
-
beginAggregation
Creates a new aggregated message from the specified start message and the specified content. If the start message implementsByteBufHolder, its content is appended to the specifiedcontent. This aggregator will continue to append the received content to the specifiedcontent.- Throws:
Exception
-
aggregate
Transfers the information provided by the specified content message to the specified aggregated message. Note that the content of the specified content message has been appended to the content of the specified aggregated message already, so that you don't need to. Use this method to transfer the additional information that the content message provides toaggregated.- Throws:
Exception
-
finishAggregation
-
handleOversizedMessage
Invoked when an incoming request exceeds the maximum content length. The default behvaior is to trigger anexceptionCaught()event with aTooLongFrameException.- Parameters:
ctx- theChannelHandlerContextoversized- the accumulated message up to this point, whose type isSorO- Throws:
Exception
-
channelReadComplete
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelReadComplete()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelReadCompletein interfaceChannelInboundHandler- Overrides:
channelReadCompletein classMessageToMessageDecoder<I>- Throws:
Exception
-
channelInactive
Description copied from class:ChannelInboundHandlerAdapterCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
channelInactivein interfaceChannelInboundHandler- Overrides:
channelInactivein classChannelInboundHandlerAdapter- Throws:
Exception
-
handlerAdded
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
Exception
-
handlerRemoved
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classChannelHandlerAdapter- Throws:
Exception
-
releaseCurrentMessage
protected final void releaseCurrentMessage()
-