Closed xmxsuperstar closed 11 years ago
@xmxsuperstar just fixed it in master. Can you verify ?
It works now, thanks a lot!
It seems that this issue occurs in netty4 final. The following is my test code for netty4 final:
package com.xx_dev.apn.proxy.testclient;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import javax.xml.parsers.FactoryConfigurationError;
import java.io.File;
import java.net.MalformedURLException;
public class TestHttpClient {
private static final Logger logger = Logger.getLogger(TestHttpClient.class);
private final String host;
private final int port;
public TestHttpClient(String host, int port) {
this.host = host;
this.port = port;
}
public void run() throws Exception {
// Configure the client.
Bootstrap b = new Bootstrap();
NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
b.group(eventLoopGroup).channel(NioSocketChannel.class).option(ChannelOption.AUTO_READ, false)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new LoggingHandler("LOGGER", LogLevel.INFO));
pipeline.addLast("codec", new HttpClientCodec());
pipeline.addLast("handler", new TestHttpClientHandler());
}
});
// Start the client.
ChannelFuture f = b.connect(host, port);
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (logger.isInfoEnabled()) {
logger.info("connect complete");
}
future.channel().read();
}
});
f.sync();
// Wait until the connection is closed.
f.channel().closeFuture().await();
} finally {
// Shut down the event loop to terminate all threads.
eventLoopGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new TestHttpClient("www.baidu.com", 80).run();
// new TestHttpClient("localhost", 8888).run();
}
}
and handler:
package com.xx_dev.apn.proxy.testclient;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.ReferenceCountUtil;
import org.apache.log4j.Logger;
public class TestHttpClientHandler extends ChannelInboundHandlerAdapter {
private static final Logger logger = Logger.getLogger(TestHttpClientHandler.class);
@Override
public void channelActive(ChannelHandlerContext ctx) {
logger.info("client channel active");
DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
"/");
request.headers().add("HOST", "www.baidu.com");
ctx.write(request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
logger.info("request write complete");
future.channel().read();
}
});
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
logger.info(msg);
if (msg instanceof HttpResponse) {
logger.info(((HttpResponse) msg).toString());
}
if (msg instanceof HttpContent) {
logger.info(msg.toString() + ((HttpContent) msg).content().readableBytes());
}
ReferenceCountUtil.release(msg);
ctx.read();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
logger.error(cause.getMessage(), cause);
ctx.close();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("client channel inactive");
}
}
When I use netty4 CR6 to download file from http server, I try to do some traffic control by using ChannelOption.AUTO_READ=false and read() opereation, but seems that the client hungs when read server response
My code: main class:
handler:
and the log is:
the messageReceived method of TestHttpClientHandler never been invoked and no more server response can be recived