【问题标题】:How to read server response in netty channel pool?如何在 netty 通道池中读取服务器响应?
【发布时间】:2017-03-16 10:21:47
【问题描述】:

我在客户端使用通道池 API 来连接服务器。当我向服务器发送请求时,它接受并成功处理它,但是当服务器回复时,我的客户端没有得到该数据。

客户频道池:

group = new NioEventLoopGroup();
final Bootstrap b = new Bootstrap();
b.group(group)
  .channel(NioSocketChannel.class)
  .option(ChannelOption.SO_KEEPALIVE, true)
  .handler(new ClientInitializer());

poolMap = new AbstractChannelPoolMap<InetSocketAddress, SimpleChannelPool>() {

@Override
protected SimpleChannelPool newPool(InetSocketAddress key) {
  return new SimpleChannelPool(b.remoteAddress(key), new SimpleChannelPoolHandler());
}
};

ClientInitializer

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

private static final ClientHandler CLIENT_HANDLER = new ClientHandler();

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

  // Add the text line codec combination first,
  pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
  pipeline.addLast(DECODER);
  pipeline.addLast(ENCODER);

  // and then business logic.
  pipeline.addLast(CLIENT_HANDLER);
}

客户端处理程序

public class ClientHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {

        System.out.println(msg);
    }

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

使用通道池向服务器发送数据:

final SimpleChannelPool pool = Client.poolMap.get(addr);

        Future<Channel> f = pool.acquire();

        f.addListener(new FutureListener<Channel>() {
            @Override
            public void operationComplete(Future<Channel> f) {

                if (f.isSuccess()) {

                    Channel ch = f.getNow();

                    ChannelFuture lastWriteFuture = null;
                    try {

                        lastWriteFuture = ch.writeAndFlush(my data here;


                         // Wait until all messages are flushed before closing the channel.
                        if (lastWriteFuture != null) {

                            lastWriteFuture.sync();
                        }
                    } catch (JsonProcessingException | InterruptedException e) {

                        e.printStackTrace();
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    // Release back to pool
                    pool.release(ch);
                }
            }

        });

如果我不使用通道池,那么一切正常并且我在 ClinetHandler 类中得到正确的响应。

我有什么建议吗?

我使用的是 Netty 4.0.28 final。

【问题讨论】:

  • 你能把服务器发送响应的代码贴出来吗?

标签: java netty


【解决方案1】:

我在#netty IRC 频道上得到了答案

所以我需要删除这一行

.handler(new ClientInitializer());

无论我的 ClientInitializer 正在做什么,我都需要在 SimpleChannelPoolHandler channelCreated 方法中执行此操作。

【讨论】:

    猜你喜欢
    • 2012-02-21
    • 2019-05-18
    • 2016-12-03
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2014-10-06
    相关资源
    最近更新 更多