How to read server response in netty channel pool?

2.9k views Asked by At

I am using channel pool API in my client, to connect to server. When i send request to server it accept & work on it successfully but when server replies my client doesn't get that data.

Client Channel Pool:

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

ClientHandler

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

Sending data to server using channel pool:

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

        });

if i do not use channel pool then all work fine & i get the proper response in ClinetHandler class.

Any suggestion where i am breaking ?

I am using Netty 4.0.28 final.

1

There are 1 answers

0
Peeyush On BEST ANSWER

i got the answer on the #netty IRC channel

so i need to remove this line

.handler(new ClientInitializer());

And whatever my ClientInitializer is doing, i need to do that in the SimpleChannelPoolHandler channelCreated method.