Polish netty client support

Mostly updates based on:
https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-Code-Style
This commit is contained in:
Rossen Stoyanchev
2014-10-27 13:56:00 -04:00
parent e1207571fc
commit 083dece106
7 changed files with 90 additions and 88 deletions

View File

@@ -60,6 +60,7 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
private final ByteBufOutputStream body;
Netty4ClientHttpRequest(Bootstrap bootstrap, URI uri, HttpMethod method, int maxRequestSize) {
this.bootstrap = bootstrap;
this.uri = uri;
@@ -67,6 +68,7 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
this.body = new ByteBufOutputStream(Unpooled.buffer(maxRequestSize));
}
@Override
public HttpMethod getMethod() {
return this.method;
@@ -83,8 +85,7 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
}
@Override
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers)
throws IOException {
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers) throws IOException {
final SettableListenableFuture<ClientHttpResponse> responseFuture =
new SettableListenableFuture<ClientHttpResponse>();
@@ -93,42 +94,19 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
Channel channel = future.channel();
channel.pipeline()
.addLast(new SimpleChannelInboundHandler<FullHttpResponse>() {
@Override
protected void channelRead0(
ChannelHandlerContext ctx,
FullHttpResponse msg) throws Exception {
responseFuture
.set(new Netty4ClientHttpResponse(ctx,
msg));
}
@Override
public void exceptionCaught(
ChannelHandlerContext ctx,
Throwable cause) throws Exception {
responseFuture.setException(cause);
}
});
FullHttpRequest nettyRequest =
createFullHttpRequest(headers);
channel.pipeline().addLast(new RequestExecuteHandler(responseFuture));
FullHttpRequest nettyRequest = createFullHttpRequest(headers);
channel.writeAndFlush(nettyRequest);
}
else {
responseFuture.setException(future.cause());
}
}
};
bootstrap.connect(uri.getHost(), getPort(uri)).addListener(connectionListener);
this.bootstrap.connect(this.uri.getHost(), getPort(this.uri)).addListener(connectionListener);
return responseFuture;
}
@Override
@@ -142,7 +120,8 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
catch (ExecutionException ex) {
if (ex.getCause() instanceof IOException) {
throw (IOException) ex.getCause();
} else {
}
else {
throw new IOException(ex.getMessage(), ex);
}
}
@@ -163,17 +142,13 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
private FullHttpRequest createFullHttpRequest(HttpHeaders headers) {
io.netty.handler.codec.http.HttpMethod nettyMethod =
io.netty.handler.codec.http.HttpMethod.valueOf(method.name());
io.netty.handler.codec.http.HttpMethod.valueOf(this.method.name());
FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
nettyMethod, this.uri.getRawPath(),
this.body.buffer());
nettyMethod, this.uri.getRawPath(), this.body.buffer());
nettyRequest.headers()
.set(io.netty.handler.codec.http.HttpHeaders.Names.HOST, uri.getHost());
nettyRequest.headers()
.set(io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION,
io.netty.handler.codec.http.HttpHeaders.Values.CLOSE);
nettyRequest.headers().set(HttpHeaders.HOST, uri.getHost());
nettyRequest.headers().set(HttpHeaders.CONNECTION, io.netty.handler.codec.http.HttpHeaders.Values.CLOSE);
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
nettyRequest.headers().add(entry.getKey(), entry.getValue());
@@ -183,4 +158,26 @@ class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements
}
/**
* A SimpleChannelInboundHandler to update the given SettableListenableFuture.
*/
private static class RequestExecuteHandler extends SimpleChannelInboundHandler<FullHttpResponse> {
private final SettableListenableFuture<ClientHttpResponse> responseFuture;
public RequestExecuteHandler(SettableListenableFuture<ClientHttpResponse> responseFuture) {
this.responseFuture = responseFuture;
}
@Override
protected void channelRead0(ChannelHandlerContext context, FullHttpResponse response) throws Exception {
this.responseFuture.set(new Netty4ClientHttpResponse(context, response));
}
@Override
public void exceptionCaught(ChannelHandlerContext context, Throwable cause) throws Exception {
this.responseFuture.setException(cause);
}
}
}

View File

@@ -45,9 +45,8 @@ import org.springframework.util.Assert;
* @author Arjen Poutsma
* @since 4.2
*/
public class Netty4ClientHttpRequestFactory
implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory,
InitializingBean, DisposableBean {
public class Netty4ClientHttpRequestFactory implements ClientHttpRequestFactory,
AsyncClientHttpRequestFactory, InitializingBean, DisposableBean {
/**
* The default maximum request size.
@@ -55,6 +54,7 @@ public class Netty4ClientHttpRequestFactory
*/
public static final int DEFAULT_MAX_REQUEST_SIZE = 1024 * 1024 * 10;
private final EventLoopGroup eventLoopGroup;
private final boolean defaultEventLoopGroup;
@@ -65,18 +65,19 @@ public class Netty4ClientHttpRequestFactory
private Bootstrap bootstrap;
/**
* Creates a new {@code Netty4ClientHttpRequestFactory} with a default
* Create a new {@code Netty4ClientHttpRequestFactory} with a default
* {@link NioEventLoopGroup}.
*/
public Netty4ClientHttpRequestFactory() {
int ioWorkerCount = Runtime.getRuntime().availableProcessors() * 2;
eventLoopGroup = new NioEventLoopGroup(ioWorkerCount);
defaultEventLoopGroup = true;
this.eventLoopGroup = new NioEventLoopGroup(ioWorkerCount);
this.defaultEventLoopGroup = true;
}
/**
* Creates a new {@code Netty4ClientHttpRequestFactory} with the given
* Create a new {@code Netty4ClientHttpRequestFactory} with the given
* {@link EventLoopGroup}.
*
* <p><b>NOTE:</b> the given group will <strong>not</strong> be
@@ -89,9 +90,10 @@ public class Netty4ClientHttpRequestFactory
this.defaultEventLoopGroup = false;
}
/**
* Sets the default maximum request size. The default is
* {@link #DEFAULT_MAX_REQUEST_SIZE}.
* Set the default maximum request size.
* <p>By default this is set to {@link #DEFAULT_MAX_REQUEST_SIZE}.
* @see HttpObjectAggregator#HttpObjectAggregator(int)
*/
public void setMaxRequestSize(int maxRequestSize) {
@@ -99,7 +101,9 @@ public class Netty4ClientHttpRequestFactory
}
/**
* Sets the SSL context.
* Set the SSL context. When configured it is used to create and insert an
* {@link io.netty.handler.ssl.SslHandler} in the channel pipeline.
* <p>By default this is not set.
*/
public void setSslContext(SslContext sslContext) {
this.sslContext = sslContext;
@@ -108,14 +112,14 @@ public class Netty4ClientHttpRequestFactory
private Bootstrap getBootstrap() {
if (this.bootstrap == null) {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
bootstrap.group(this.eventLoopGroup).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
protected void initChannel(SocketChannel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
if (sslContext != null) {
pipeline.addLast(sslContext.newHandler(ch.alloc()));
pipeline.addLast(sslContext.newHandler(channel.alloc()));
}
pipeline.addLast(new HttpClientCodec());
pipeline.addLast(new HttpObjectAggregator(maxRequestSize));
@@ -131,29 +135,26 @@ public class Netty4ClientHttpRequestFactory
getBootstrap();
}
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
return createRequestInternal(uri, httpMethod);
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod) throws IOException {
return createRequestInternal(uri, httpMethod);
}
private Netty4ClientHttpRequest createRequestInternal(URI uri, HttpMethod httpMethod) {
return new Netty4ClientHttpRequest(getBootstrap(), uri, httpMethod, maxRequestSize);
}
@Override
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod)
throws IOException {
return createRequestInternal(uri, httpMethod);
}
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
throws IOException {
return createRequestInternal(uri, httpMethod);
return new Netty4ClientHttpRequest(getBootstrap(), uri, httpMethod, this.maxRequestSize);
}
@Override
public void destroy() throws InterruptedException {
if (defaultEventLoopGroup) {
if (this.defaultEventLoopGroup) {
// clean up the EventLoopGroup if we created it in the constructor
eventLoopGroup.shutdownGracefully().sync();
this.eventLoopGroup.shutdownGracefully().sync();
}
}
}

View File

@@ -45,8 +45,7 @@ class Netty4ClientHttpResponse extends AbstractClientHttpResponse {
private HttpHeaders headers;
Netty4ClientHttpResponse(ChannelHandlerContext context,
FullHttpResponse nettyResponse) {
Netty4ClientHttpResponse(ChannelHandlerContext context, FullHttpResponse nettyResponse) {
Assert.notNull(context, "'context' must not be null");
Assert.notNull(nettyResponse, "'nettyResponse' must not be null");
this.context = context;
@@ -55,6 +54,7 @@ class Netty4ClientHttpResponse extends AbstractClientHttpResponse {
this.nettyResponse.retain();
}
@Override
public int getRawStatusCode() throws IOException {
return this.nettyResponse.getStatus().code();