从 ChannelFuture 获取响应状态

Get response status from ChannelFuture

提问人:smie 提问时间:11/17/2023 更新时间:11/17/2023 访问量:19

问:

下面是一个示例服务器

package org.test;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.*;

public class ListenerExample {
    public static void main(String[] args) throws Exception {
        // Configure the server.
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.option(ChannelOption.SO_BACKLOG, 1024);
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            final ChannelPipeline pipeline = ch.pipeline();
                            pipeline.addLast(new HttpServerCodec());
                            pipeline.addLast(new HttpObjectAggregator(Integer.MAX_VALUE));
                            pipeline.addLast(new HttpContentDecompressor(true));
                            pipeline.addLast(new ListenerExampleHandler());
                        }
                    });

            Channel ch = b.bind(8081).sync().channel();

            ch.closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    private static class ListenerExampleHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
            System.out.println("New HTTP message: " + msg.uri());
            // Emulate long request handler
            Thread.sleep(2000);
            //
            final var isKeepAlive = HttpUtil.isKeepAlive(msg);
            // Response
            final var content = "PONG".getBytes();
            final var response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(content));
            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.length);
            response.headers().set(HttpHeaderNames.CONNECTION, isKeepAlive ? HttpHeaderValues.KEEP_ALIVE : HttpHeaderValues.CLOSE);
            //
            ctx.writeAndFlush(response).addListener((ChannelFuture future) -> {
                if (!isKeepAlive || !future.isSuccess()) {
                    future.channel().close();
                }

                System.out.println("SUCCESS: " + (future.isDone() && future.isSuccess()));
                System.out.println("ERROR: " + (future.isDone() && future.cause() != null) + ". " + future.cause());
                System.out.println("CANCELED: " + (future.isDone() && future.isCancelled()));
            });
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
            cause.printStackTrace();
            ctx.close();
        }
    }
}

我需要在 writeAndFlush 之后获取消息状态。

我尝试添加侦听器,但当客户端重置连接时它不起作用。

例如:使用 curl 我向服务器发送 get 请求。在服务器控制台日志中,我看到有关新request()的消息,然后我终止了curl(Control + C)。 结果服务器返回:New HTTP message: /

SUCCESS: true
ERROR: false. null
CANCELED: false
java.net.SocketException: Connection reset
    at java.base/sun.nio.ch.SocketChannelImpl.throwConnectionReset(SocketChannelImpl.java:401)
    at java.base/sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:434)
    at io.netty.buffer.PooledByteBuf.setBytes(PooledByteBuf.java:258)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1132)

为什么我会得到以及我如何在侦听器中得到错误?SUCCESS: true

Java Netty

评论


答: 暂无答案