Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在netty通道池中读取服务器响应?_Java_Netty - Fatal编程技术网

Java 如何在netty通道池中读取服务器响应?

Java 如何在netty通道池中读取服务器响应?,java,netty,Java,Netty,我在客户端使用通道池API连接到服务器。当我向服务器发送请求时,它会接受并成功处理该请求,但当服务器答复时,我的客户端无法获取该数据 客户端通道池: group = new NioEventLoopGroup(); final Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.SO_KEEPALIVE, true) .handler(

我在客户端使用通道池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());
}
};
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);
                }
            }

        });
ClientHandler

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();
    }
}
公共类ClientHandler扩展了SimpleChannelInboundHandler{
@凌驾
受保护的无效channelRead0(ChannelHandlerContext ctx,字符串msg)引发异常{
System.out.println(msg);
}
@凌驾
公共无效例外情况(ChannelHandlerContext ctx,可丢弃原因){
cause.printStackTrace();
ctx.close();
}
}
使用通道池向服务器发送数据:

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());
}
};
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);
                }
            }

        });
final-SimpleChannelPool-pool=Client.poolMap.get(addr);
Future f=pool.acquire();
f、 addListener(新的FutureListener(){
@凌驾
公共无效操作完成(未来f){
如果(f.isSuccess()){
通道ch=f.getNow();
ChannelFuture lastWriteFuture=null;
试一试{
lastWriteFuture=ch.writeAndFlush(此处为我的数据;
//等待所有消息刷新后再关闭通道。
if(lastWriteFuture!=null){
lastWriteFuture.sync();
}
}捕获(JsonProcessingException | InterruptedException e){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
//释放回池中
释放池(ch);
}
}
});
如果我不使用通道池,那么一切都可以正常工作&我在ClinetHandler类中得到正确的响应

有没有建议我在哪里分手

我正在使用Netty 4.0.28决赛。

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

所以我需要去掉这条线

.handler(new ClientInitializer());

无论我的ClientInitializer在做什么,我都需要在SimpleChannelPoolHandler
channelCreated
方法中完成这项工作。

您能将代码发布到服务器发送响应的地方吗?