Netty服务器中未调用RequestHandler

时间:2019-03-29 21:19:59

标签: java netty

我正在Netty Http服务器上工作。我已经创建并注册了处理程序。但我看不到请求击中处理程序

这是主要班级

public class NettyServer {

    private int port;

    private NettyServer(int port) {
        this.port = port;
    }

    public static void main(String[] args) throws Exception {
        int port;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        } else {
            port = 8080;
        }
        new NettyServer(port).run();
    }

    private void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new HttpMessageHandler(),new CalculatorOperationHandler());
                }
            }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture f = b.bind(port).sync();
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}

HttpMessageHandler.java

public class HttpMessageHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
        System.out.println("hello");
        String uri = msg.uri();
        HttpMethod httpMethod = msg.method();
        HttpHeaders headers = msg.headers();

        if (HttpMethod.GET == httpMethod) {

            String[] uriComponents = uri.split("[?]");
            String endpoint = uriComponents[0];
            String[] queryParams = uriComponents[1].split("&");

            if ("/calculate".equalsIgnoreCase(endpoint)) {

                String[] firstQueryParam = queryParams[0].split("=");
                String[] secondQueryParam = queryParams[1].split("=");

                Integer a = Integer.valueOf(firstQueryParam[1]);
                Integer b = Integer.valueOf(secondQueryParam[1]);
                String operator = headers.get("operator");

                Operation operation = new Operation(a, b, operator);
                ctx.fireChannelRead(operation);
            }
        } else {
            throw new UnsupportedOperationException("HTTP method not supported");
        }

    }
}

当我调用localhost:8080/calculate?a=1&b=2

时,在控制台上看不到“ hello”字样

这是怎么了?

1 个答案:

答案 0 :(得分:2)

您的问题是由管道中缺少处理程序引起的。

目前,您的管道中只有2个处理程序:

  • HttpMessageHandler,可处理FullHttpRequest个对象
  • CalculatorOperationHandler,可处理Operation个对象

当来自浏览器的数据进入时,它作为ByteBuf对象进入,但是您不处理该对象!

要从ByteBuf转换为FullHttpRequest,您需要在管道中添加其他可以执行此操作的处理程序。

您需要的第一个处理程序是HttpServerCodec,此类将ByteBuf对象转换为HTTP交换的一部分的对象,例如标头,请求正文的尾部标头。

然后,您需要添加一个HttpObjectAggregator,它将上面的对象合并为一个FullHttpRequest,因此您只需要处理1个对象。

ch.pipeline().addLast(
    new HttpServerCodec(),
    new HttpObjectAggregator(65536), // Handle POST/PUT requests up 64KB
    new HttpMessageHandler(),
    new CalculatorOperationHandler()
);

如果要查看任何层之间的流量,也可以添加new LoggingHandler(LogLevel.INFO)