如何读取 netty 通道池中的服务器响应?
How to read server response in netty channel pool?
我在我的客户端中使用通道池 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 class 中得到正确的响应。
有什么建议我打破了吗?
我使用的是 Netty 4.0.28 final。
我在#netty IRC 频道上得到了答案
所以我需要删除这一行
.handler(new ClientInitializer());
无论我的 ClientInitializer 在做什么,我都需要在 SimpleChannelPoolHandler channelCreated
方法中完成。
我在我的客户端中使用通道池 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 class 中得到正确的响应。
有什么建议我打破了吗?
我使用的是 Netty 4.0.28 final。
我在#netty IRC 频道上得到了答案
所以我需要删除这一行
.handler(new ClientInitializer());
无论我的 ClientInitializer 在做什么,我都需要在 SimpleChannelPoolHandler channelCreated
方法中完成。