【问题标题】:Netty 4 Calls being blocked , Simple Telnet ServerNetty 4 呼叫被阻止,简单的 Telnet 服务器
【发布时间】:2016-03-02 22:15:40
【问题描述】:

我正在尝试使用 Netty Telnet 服务器使用 netty api 来检查是否可以观察到真正的异步行为。

下面是使用的三个类

TelnetServer.java

public class TelnetServer {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
          EventLoopGroup bossGroup = new NioEventLoopGroup(1);
          EventLoopGroup workerGroup = new NioEventLoopGroup();
          try {
              ServerBootstrap b = new ServerBootstrap();
              b.group(bossGroup, workerGroup)
               .channel(NioServerSocketChannel.class)
               .handler(new LoggingHandler(LogLevel.INFO))
               .childHandler(new TelnetServerInitializer());


              b.bind(8989).sync().channel().closeFuture().sync();
          } finally {
              bossGroup.shutdownGracefully();
              workerGroup.shutdownGracefully();
          }
    }

}

TelnetServerInitializer.java

public class TelnetServerInitializer extends ChannelInitializer<SocketChannel> {

    private static final StringDecoder DECODER = new StringDecoder();
    private static final StringEncoder ENCODER = new StringEncoder();

    private static final TelnetServerHandler SERVER_HANDLER = new TelnetServerHandler();


    final EventExecutorGroup executorGroup = new DefaultEventExecutorGroup(2);

    public TelnetServerInitializer() {

    }

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();


        // Add the text line codec combination first,
        pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        // the encoder and decoder are static as these are sharable
        pipeline.addLast(DECODER);
        pipeline.addLast(ENCODER);

        // and then business logic.
        pipeline.addLast(executorGroup,"handler",SERVER_HANDLER);
    }
}

TelnetServerHandler.java

/**
 * Handles a server-side channel.
 */
@Sharable
public class TelnetServerHandler extends SimpleChannelInboundHandler<String> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // Send greeting for a new connection.
        ctx.write("Welcome to " + InetAddress.getLocalHost().getHostName() + "!\r\n");
        ctx.write("It is " + new Date() + " now.\r\n");
        ctx.flush();
        ctx.channel().config().setAutoRead(true);
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, String request) throws Exception {
        // Generate and write a response.

        System.out.println("request = "+ request);

        String response;
        boolean close = false;
        if (request.isEmpty()) {
            response = "Please type something.\r\n";
        } else if ("bye".equals(request.toLowerCase())) {
            response = "Have a good day!\r\n";
            close = true;
        } else {
            response = "Did you say '" + request + "'?\r\n";
        }

        // We do not need to write a ChannelBuffer here.
        // We know the encoder inserted at TelnetPipelineFactory will do the conversion.
        ChannelFuture future = ctx.write(response);

        Thread.sleep(10000);
        // Close the connection after sending 'Have a good day!'
        // if the client has sent 'bye'.
        if (close) {
            future.addListener(ChannelFutureListener.CLOSE);
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

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

现在当我通过 telnet 客户端连接并发送命令 hello hello hello 三次

在对 channelRead 的第一次响应完成之前,请求不会到达 channelRead

【问题讨论】:

    标签: java sockets connection netty


    【解决方案1】:

    对于每个处理程序的每个传入读取,Netty 最多使用 1 个线程,这意味着对 channelRead 的下一次调用将仅在上一次调用完成后调度。这是大多数处理程序正确工作所必需的,包括以正确的顺序发回消息。如果计算量真的很复杂,另一种解决方案是为消息使用自定义线程池。

    如果其他操作是其他类型的连接,您也应该将其用作异步连接。只有在每个部分都正确执行此操作时,您才能获得异步。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-12
      • 2012-02-13
      • 1970-01-01
      • 2011-10-16
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      相关资源
      最近更新 更多