Я пытаюсь создать простой HTTP-сервер, используя netty 4.1.16.Final.
Ниже приведен код для HTTP-сервера -
EventLoopGroup masterGroup = new NioEventLoopGroup();
EventLoopGroup slaveGroup = new NioEventLoopGroup();
final ServerBootstrap bootstrap =
new ServerBootstrap()
.group(masterGroup, slaveGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast("codec", new HttpServerCodec());
ch.pipeline().addLast("aggregator",
new HttpObjectAggregator(512 * 1024));
ch.pipeline().addLast("request",
new HTTPSimpleChannelInboundHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
channel = bootstrap.bind(8080).sync();
код для класса HTTP Handler HTTPSimpleChannelInboundHandler
ниже -
public class HTTPSimpleChannelInboundHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
HttpResponseStatus responseStatus = OK;
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, responseStatus, Unpooled.copiedBuffer("My Netty".getBytes()));
response.headers().add(request.headers());
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
if (isKeepAlive(request)) {
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
}
if (is100ContinueExpected(request)) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
}
ctx.writeAndFlush(response);
}
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
Приведенный выше код работает нормально.Но когда я комментировал приведенную ниже строку в коде сервера, я не получил ответ.
ch.pipeline().addLast("aggregator", new HttpObjectAggregator(512 * 1024));
Ниже приведен журнал, полученный от сервера -
01:37:14.806 [nioEventLoopGroup-3-2] DEBUG io.netty.channel.DefaultChannelPipeline - Discarded inbound message DefaultHttpRequest(decodeResult: success, version: HTTP/1.1)
GET /test HTTP/1.1
Host: localhost:5055
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: _ga=GA1.1.177481759.1523295602; Idea-46064427=2276f52b-2928-4410-8f4c-c7561bd33457
Connection: keep-alive
Upgrade-Insecure-Requests: 1 that reached at the tail of the pipeline. Please check your pipeline configuration.
01:37:14.806 [nioEventLoopGroup-3-2] DEBUG io.netty.channel.DefaultChannelPipeline - Discarded inbound message EmptyLastHttpContent that reached at the tail of the pipeline. Please check your pipeline configuration.
Любая причина, почемуHttpObjectAggregator является обязательным в ChannelPipeline?