Skip to content

Commit 7de0a70

Browse files
poutsmarstoyanchev
authored andcommitted
Netty support for (Async)RestTemplate
This commit introduces an AsyncClientHttpRequestFactory based on Netty 4, for use with the (Async)RestTemplate.
1 parent a13bb69 commit 7de0a70

9 files changed

+608
-21
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ project("spring-web") {
672672
optional("commons-fileupload:commons-fileupload:1.3.1")
673673
optional("org.apache.httpcomponents:httpclient:4.3.5")
674674
optional("org.apache.httpcomponents:httpasyncclient:4.0.2")
675+
optional("io.netty:netty-all:4.0.23.Final")
675676
optional("com.fasterxml.jackson.core:jackson-databind:${jackson2Version}")
676677
optional("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${jackson2Version}")
677678
optional("com.google.code.gson:gson:${gsonVersion}")
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.io.IOException;
20+
import java.io.OutputStream;
21+
import java.net.URI;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.concurrent.ExecutionException;
25+
26+
import io.netty.bootstrap.Bootstrap;
27+
import io.netty.buffer.ByteBufOutputStream;
28+
import io.netty.buffer.Unpooled;
29+
import io.netty.channel.Channel;
30+
import io.netty.channel.ChannelFuture;
31+
import io.netty.channel.ChannelFutureListener;
32+
import io.netty.channel.ChannelHandlerContext;
33+
import io.netty.channel.SimpleChannelInboundHandler;
34+
import io.netty.handler.codec.http.DefaultFullHttpRequest;
35+
import io.netty.handler.codec.http.FullHttpRequest;
36+
import io.netty.handler.codec.http.FullHttpResponse;
37+
import io.netty.handler.codec.http.HttpVersion;
38+
39+
import org.springframework.http.HttpHeaders;
40+
import org.springframework.http.HttpMethod;
41+
import org.springframework.util.concurrent.ListenableFuture;
42+
import org.springframework.util.concurrent.SettableListenableFuture;
43+
44+
/**
45+
* {@link org.springframework.http.client.ClientHttpRequest} implementation that uses
46+
* Netty 4 to execute requests.
47+
*
48+
* <p>Created via the {@link Netty4ClientHttpRequestFactory}.
49+
*
50+
* @author Arjen Poutsma
51+
* @since 4.2
52+
*/
53+
class Netty4ClientHttpRequest extends AbstractAsyncClientHttpRequest implements ClientHttpRequest {
54+
55+
private final Bootstrap bootstrap;
56+
57+
private final URI uri;
58+
59+
private final HttpMethod method;
60+
61+
private final ByteBufOutputStream body;
62+
63+
Netty4ClientHttpRequest(Bootstrap bootstrap, URI uri, HttpMethod method, int maxRequestSize) {
64+
this.bootstrap = bootstrap;
65+
this.uri = uri;
66+
this.method = method;
67+
this.body = new ByteBufOutputStream(Unpooled.buffer(maxRequestSize));
68+
}
69+
70+
@Override
71+
public HttpMethod getMethod() {
72+
return this.method;
73+
}
74+
75+
@Override
76+
public URI getURI() {
77+
return this.uri;
78+
}
79+
80+
@Override
81+
protected OutputStream getBodyInternal(HttpHeaders headers) throws IOException {
82+
return body;
83+
}
84+
85+
@Override
86+
protected ListenableFuture<ClientHttpResponse> executeInternal(final HttpHeaders headers)
87+
throws IOException {
88+
final SettableListenableFuture<ClientHttpResponse> responseFuture =
89+
new SettableListenableFuture<ClientHttpResponse>();
90+
91+
ChannelFutureListener connectionListener = new ChannelFutureListener() {
92+
@Override
93+
public void operationComplete(ChannelFuture future) throws Exception {
94+
if (future.isSuccess()) {
95+
Channel channel = future.channel();
96+
channel.pipeline()
97+
.addLast(new SimpleChannelInboundHandler<FullHttpResponse>() {
98+
99+
@Override
100+
protected void channelRead0(
101+
ChannelHandlerContext ctx,
102+
FullHttpResponse msg) throws Exception {
103+
responseFuture
104+
.set(new Netty4ClientHttpResponse(ctx,
105+
msg));
106+
}
107+
108+
@Override
109+
public void exceptionCaught(
110+
ChannelHandlerContext ctx,
111+
Throwable cause) throws Exception {
112+
responseFuture.setException(cause);
113+
}
114+
});
115+
116+
FullHttpRequest nettyRequest =
117+
createFullHttpRequest(headers);
118+
119+
channel.writeAndFlush(nettyRequest);
120+
}
121+
else {
122+
responseFuture.setException(future.cause());
123+
}
124+
125+
}
126+
};
127+
128+
bootstrap.connect(uri.getHost(), getPort(uri)).addListener(connectionListener);
129+
130+
return responseFuture;
131+
132+
}
133+
134+
@Override
135+
public ClientHttpResponse execute() throws IOException {
136+
try {
137+
return executeAsync().get();
138+
}
139+
catch (InterruptedException ex) {
140+
throw new IOException(ex.getMessage(), ex);
141+
}
142+
catch (ExecutionException ex) {
143+
if (ex.getCause() instanceof IOException) {
144+
throw (IOException) ex.getCause();
145+
} else {
146+
throw new IOException(ex.getMessage(), ex);
147+
}
148+
}
149+
}
150+
151+
private static int getPort(URI uri) {
152+
int port = uri.getPort();
153+
if (port == -1) {
154+
if ("http".equalsIgnoreCase(uri.getScheme())) {
155+
port = 80;
156+
}
157+
else if ("https".equalsIgnoreCase(uri.getScheme())) {
158+
port = 443;
159+
}
160+
}
161+
return port;
162+
}
163+
164+
private FullHttpRequest createFullHttpRequest(HttpHeaders headers) {
165+
io.netty.handler.codec.http.HttpMethod nettyMethod =
166+
io.netty.handler.codec.http.HttpMethod.valueOf(method.name());
167+
168+
FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
169+
nettyMethod, this.uri.getRawPath(),
170+
this.body.buffer());
171+
172+
nettyRequest.headers()
173+
.set(io.netty.handler.codec.http.HttpHeaders.Names.HOST, uri.getHost());
174+
nettyRequest.headers()
175+
.set(io.netty.handler.codec.http.HttpHeaders.Names.CONNECTION,
176+
io.netty.handler.codec.http.HttpHeaders.Values.CLOSE);
177+
178+
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
179+
nettyRequest.headers().add(entry.getKey(), entry.getValue());
180+
}
181+
182+
return nettyRequest;
183+
}
184+
185+
186+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright 2002-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.http.client;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
22+
import io.netty.bootstrap.Bootstrap;
23+
import io.netty.channel.ChannelInitializer;
24+
import io.netty.channel.ChannelPipeline;
25+
import io.netty.channel.EventLoopGroup;
26+
import io.netty.channel.nio.NioEventLoopGroup;
27+
import io.netty.channel.socket.SocketChannel;
28+
import io.netty.channel.socket.nio.NioSocketChannel;
29+
import io.netty.handler.codec.http.HttpClientCodec;
30+
import io.netty.handler.codec.http.HttpObjectAggregator;
31+
import io.netty.handler.ssl.SslContext;
32+
33+
import org.springframework.beans.factory.DisposableBean;
34+
import org.springframework.beans.factory.InitializingBean;
35+
import org.springframework.http.HttpMethod;
36+
import org.springframework.util.Assert;
37+
38+
/**
39+
* {@link org.springframework.http.client.ClientHttpRequestFactory} implementation that
40+
* uses <a href="http://netty.io/">Netty 4</a> to create requests.
41+
*
42+
* <p>Allows to use a pre-configured {@link EventLoopGroup} instance - useful for sharing
43+
* across multiple clients.
44+
*
45+
* @author Arjen Poutsma
46+
* @since 4.2
47+
*/
48+
public class Netty4ClientHttpRequestFactory
49+
implements ClientHttpRequestFactory, AsyncClientHttpRequestFactory,
50+
InitializingBean, DisposableBean {
51+
52+
/**
53+
* The default maximum request size.
54+
* @see #setMaxRequestSize(int)
55+
*/
56+
public static final int DEFAULT_MAX_REQUEST_SIZE = 1024 * 1024 * 10;
57+
58+
private final EventLoopGroup eventLoopGroup;
59+
60+
private final boolean defaultEventLoopGroup;
61+
62+
private SslContext sslContext;
63+
64+
private int maxRequestSize = DEFAULT_MAX_REQUEST_SIZE;
65+
66+
private Bootstrap bootstrap;
67+
68+
/**
69+
* Creates a new {@code Netty4ClientHttpRequestFactory} with a default
70+
* {@link NioEventLoopGroup}.
71+
*/
72+
public Netty4ClientHttpRequestFactory() {
73+
int ioWorkerCount = Runtime.getRuntime().availableProcessors() * 2;
74+
eventLoopGroup = new NioEventLoopGroup(ioWorkerCount);
75+
defaultEventLoopGroup = true;
76+
}
77+
78+
/**
79+
* Creates a new {@code Netty4ClientHttpRequestFactory} with the given
80+
* {@link EventLoopGroup}.
81+
*
82+
* <p><b>NOTE:</b> the given group will <strong>not</strong> be
83+
* {@linkplain EventLoopGroup#shutdownGracefully() shutdown} by this factory; doing
84+
* so becomes the responsibility of the caller.
85+
*/
86+
public Netty4ClientHttpRequestFactory(EventLoopGroup eventLoopGroup) {
87+
Assert.notNull(eventLoopGroup, "'eventLoopGroup' must not be null");
88+
this.eventLoopGroup = eventLoopGroup;
89+
this.defaultEventLoopGroup = false;
90+
}
91+
92+
/**
93+
* Sets the default maximum request size. The default is
94+
* {@link #DEFAULT_MAX_REQUEST_SIZE}.
95+
* @see HttpObjectAggregator#HttpObjectAggregator(int)
96+
*/
97+
public void setMaxRequestSize(int maxRequestSize) {
98+
this.maxRequestSize = maxRequestSize;
99+
}
100+
101+
/**
102+
* Sets the SSL context.
103+
*/
104+
public void setSslContext(SslContext sslContext) {
105+
this.sslContext = sslContext;
106+
}
107+
108+
private Bootstrap getBootstrap() {
109+
if (this.bootstrap == null) {
110+
Bootstrap bootstrap = new Bootstrap();
111+
bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
112+
.handler(new ChannelInitializer<SocketChannel>() {
113+
@Override
114+
protected void initChannel(SocketChannel ch) throws Exception {
115+
ChannelPipeline pipeline = ch.pipeline();
116+
117+
if (sslContext != null) {
118+
pipeline.addLast(sslContext.newHandler(ch.alloc()));
119+
}
120+
pipeline.addLast(new HttpClientCodec());
121+
pipeline.addLast(new HttpObjectAggregator(maxRequestSize));
122+
}
123+
});
124+
this.bootstrap = bootstrap;
125+
}
126+
return this.bootstrap;
127+
}
128+
129+
@Override
130+
public void afterPropertiesSet() throws Exception {
131+
getBootstrap();
132+
}
133+
134+
private Netty4ClientHttpRequest createRequestInternal(URI uri, HttpMethod httpMethod) {
135+
return new Netty4ClientHttpRequest(getBootstrap(), uri, httpMethod, maxRequestSize);
136+
}
137+
138+
@Override
139+
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod)
140+
throws IOException {
141+
return createRequestInternal(uri, httpMethod);
142+
}
143+
144+
@Override
145+
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
146+
throws IOException {
147+
return createRequestInternal(uri, httpMethod);
148+
}
149+
150+
@Override
151+
public void destroy() throws InterruptedException {
152+
if (defaultEventLoopGroup) {
153+
// clean up the EventLoopGroup if we created it in the constructor
154+
eventLoopGroup.shutdownGracefully().sync();
155+
}
156+
}
157+
158+
159+
}

0 commit comments

Comments
 (0)