Top Description Fields Constructors Methods
io.netty.handler.codec.http

public Class HttpServerKeepAliveHandler

extends ChannelDuplexHandler
Class Inheritance
Imports
io.netty.channel.ChannelDuplexHandler, .ChannelFutureListener, .ChannelHandlerContext, .ChannelPipeline, .ChannelPromise

HttpServerKeepAliveHandler helps close persistent connections when appropriate.

The server channel is expected to set the proper 'Connection' header if it can handle persistent connections. HttpServerKeepAliveHandler will automatically close the channel for any LastHttpContent that corresponds to a client request for closing the connection, or if the HttpResponse associated with that LastHttpContent requested closing the connection or didn't have a self defined message length.

Since HttpServerKeepAliveHandler expects HttpObjects it should be added after HttpServerCodec but before any other handlers that might send a HttpResponse.

 ChannelPipeline p = ...;
 ...
 p.addLast("serverCodec", new HttpServerCodec());
 p.addLast("httpKeepAlive", new HttpServerKeepAliveHandler());
 p.addLast("aggregator", new HttpObjectAggregator(1048576));
 ...
 p.addLast("handler", new HttpRequestHandler());
 

Field Summary

Modifier and TypeField and Description
private static final String
private int
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
private static boolean
private static boolean
private static boolean

Returns:

true if the response has a self defined message length.
isSelfDefinedMessageLength
(HttpResponse
The HttpResponse to check
response
)

Keep-alive only works if the client can detect when the message has ended without relying on the connection being closed.

private boolean
private void
public void
write(ChannelHandlerContext
the ChannelHandlerContext for which the write operation is made
ctx
,
Object
the message to write
msg
,
ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
Inherited from io.netty.channel.ChannelDuplexHandler:
bindcloseconnectderegisterdisconnectflushread

Field Detail

MULTIPART_PREFIXback to summary
private static final String MULTIPART_PREFIX
pendingResponsesback to summary
private int pendingResponses
persistentConnectionback to summary
private boolean persistentConnection

Constructor Detail

HttpServerKeepAliveHandlerback to summary
public HttpServerKeepAliveHandler()

Method Detail

channelReadback to summary
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception

Overrides io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Implements io.netty.channel.ChannelInboundHandler.channelRead.

Doc from io.netty.channel.ChannelInboundHandlerAdapter.channelRead.

Calls ChannelHandlerContext#fireChannelRead(Object) to forward to the next ChannelInboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Annotations
@Override
isInformationalback to summary
private static boolean isInformational(HttpResponse response)
isMultipartback to summary
private static boolean isMultipart(HttpResponse response)
isSelfDefinedMessageLengthback to summary
private static boolean isSelfDefinedMessageLength(HttpResponse response)

Keep-alive only works if the client can detect when the message has ended without relying on the connection being closed.

Parameters
response:HttpResponse

The HttpResponse to check

Returns:boolean

true if the response has a self defined message length.

shouldKeepAliveback to summary
private boolean shouldKeepAlive()
trackResponseback to summary
private void trackResponse(HttpResponse response)
writeback to summary
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelDuplexHandler.write.

Implements io.netty.channel.ChannelOutboundHandler.write.

Doc from io.netty.channel.ChannelDuplexHandler.write.

Calls ChannelHandlerContext#write(Object, ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelHandlerContext for which the write operation is made

msg:Object

Doc from io.netty.channel.ChannelOutboundHandler.write.

the message to write

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.write.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.write.

thrown if an error occurs