netty客户端重用服务器端引导,导致请求从未完成
Netty服务器将收到的每个请求作为客户端请求到另一台服务器。几乎与代理类似,只是它不按原样转发接收到的消息。我尝试使用服务器netty客户端重用服务器端引导,导致请求从未完成,netty,Netty,Netty服务器将收到的每个请求作为客户端请求到另一台服务器。几乎与代理类似,只是它不按原样转发接收到的消息。我尝试使用服务器ChannelHandlerContext创建客户机,但请求从未完成 在最后一个双工处理程序中,如果test,客户机将收到响应,否则请求将出现 服务器最后处理程序: @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { log.info
ChannelHandlerContext
创建客户机,但请求从未完成
在最后一个双工处理程序中,如果test
,客户机将收到响应,否则请求将出现
服务器最后处理程序:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
log.info("receive: {}", msg);
if (!test) {
HttpClient.clientSend(ctx);
} else {
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1,
io.netty.handler.codec.http.HttpResponseStatus.OK,
Unpooled.wrappedBuffer(CONTENT));
response.headers().set(CONTENT_TYPE, "text/plain");
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
log.info("write: {}", msg);
ctx.write(msg, promise); // checked, it is success. but client never receives.
}
客户:
public static void clientSend(ChannelHandlerContext ctx) {
final Channel inboundChannel = ctx.channel();
try {
Bootstrap b = new Bootstrap();
b.group(inboundChannel.eventLoop())
.channel(inboundChannel.getClass())
.handler(new ClientChannelInitializer(inboundChannel));
// Make the connection attempt.
ChannelFuture f = b.connect(host,port);
Channel outboundChannel = f.channel();
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
// Prepare the HTTP request.
HttpRequest request =
new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, HttpMethod.POST, "targets", Unpooled.copiedBuffer(requestBody,
CharsetUtil.UTF_8));
// Send the HTTP request.
ChannelFuture wf = outboundChannel.writeAndFlush(request);
wf.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
log.info("write success");
} else {
log.info("write failed ", future.cause());
}
}
});
log.info("channel listener connected");
} else {
log.info("channel listener not connected");
}
}
}
);
}
当调用write
时,我可以看到msg
是正确的,并且ctx.write
成功,但是邮递员给出:
对我来说,您似乎没有设置与有效负载字节数匹配的“内容长度”标题。加上它就可以了