This commit is contained in:
jinyu
2015-12-31 15:07:43 +08:00
parent c8cb22b69a
commit e043cd90a7
3 changed files with 39 additions and 29 deletions
@@ -16,6 +16,7 @@ import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.timeout.ReadTimeoutHandler;
import java.io.IOException;
import java.net.InetAddress;
@@ -25,6 +26,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
@@ -91,6 +93,12 @@ public class Netty4HttpClient extends AbstractHttpClient {
sslEngine.setUseClientMode(true);
channel.pipeline().addFirst(
new SslHandler(sslEngine));
if (params != null && params.getReadTimeout() > 0) {
channel.pipeline().addFirst(
new ReadTimeoutHandler(params
.getReadTimeout(),
TimeUnit.MILLISECONDS));
}
}
channel.pipeline().addLast(new RequestHandler(future));
channel.writeAndFlush(uriRequest);
@@ -39,26 +39,30 @@ public class Netty4HttpClientFactory extends HttpClientFactory {
this.workerThreads = workerThreads;
}
private volatile Bootstrap bootstrap;
@Override
public HttpClient newInstance() {
Bootstrap b = new Bootstrap();
b.option(ChannelOption.SO_KEEPALIVE, true).option(
ChannelOption.TCP_NODELAY, true);
EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads);
b.group(workerGroup).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel channel)
throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new HttpContentDecompressor());
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new HttpResponseDecoder());
pipeline.addLast(new HttpObjectAggregator(
Integer.MAX_VALUE));
}
});
return new Netty4HttpClient(b);
if (bootstrap == null) {
bootstrap = new Bootstrap();
bootstrap.option(ChannelOption.SO_KEEPALIVE, true).option(
ChannelOption.TCP_NODELAY, true);
EventLoopGroup workerGroup = new NioEventLoopGroup(workerThreads);
bootstrap.group(workerGroup).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel channel)
throws Exception {
ChannelPipeline pipeline = channel.pipeline();
pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new HttpContentDecompressor());
pipeline.addLast(new ChunkedWriteHandler());
pipeline.addLast(new HttpResponseDecoder());
pipeline.addLast(new HttpObjectAggregator(
Integer.MAX_VALUE));
}
});
}
return new Netty4HttpClient(bootstrap);
}
}