Netty support for (Async)RestTemplate
This commit introduces an AsyncClientHttpRequestFactory based on Netty 4, for use with the (Async)RestTemplate.
This commit is contained in:
committed by
Rossen Stoyanchev
parent
a13bb69cbe
commit
7de0a70f0c
@@ -22,9 +22,12 @@ import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.junit.After;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -34,8 +37,6 @@ import org.springframework.util.StreamUtils;
|
||||
import org.springframework.util.concurrent.ListenableFuture;
|
||||
import org.springframework.util.concurrent.ListenableFutureCallback;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJettyServerTestCase {
|
||||
|
||||
protected AsyncClientHttpRequestFactory factory;
|
||||
@@ -49,6 +50,13 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public final void destroyFactory() throws Exception {
|
||||
if (factory instanceof DisposableBean) {
|
||||
((DisposableBean) factory).destroy();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract AsyncClientHttpRequestFactory createRequestFactory();
|
||||
|
||||
|
||||
@@ -60,7 +68,11 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
||||
assertEquals("Invalid HTTP URI", uri, request.getURI());
|
||||
Future<ClientHttpResponse> futureResponse = request.executeAsync();
|
||||
ClientHttpResponse response = futureResponse.get();
|
||||
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
try {
|
||||
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -74,24 +86,24 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
||||
@Override
|
||||
public void onSuccess(ClientHttpResponse result) {
|
||||
try {
|
||||
System.out.println("SUCCESS! " + result.getStatusCode());
|
||||
System.out.println("Callback: " + System.currentTimeMillis());
|
||||
System.out.println(Thread.currentThread().getId());
|
||||
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, result.getStatusCode());
|
||||
}
|
||||
catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onFailure(Throwable ex) {
|
||||
System.out.println("FAILURE: " + ex);
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
});
|
||||
ClientHttpResponse response = listenableFuture.get();
|
||||
System.out.println("Main thread: " + System.currentTimeMillis());
|
||||
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
System.out.println(Thread.currentThread().getId());
|
||||
try {
|
||||
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
}
|
||||
finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -129,7 +141,7 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@Test
|
||||
public void multipleWrites() throws Exception {
|
||||
AsyncClientHttpRequest request = factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
|
||||
final byte[] body = "Hello World".getBytes("UTF-8");
|
||||
@@ -146,13 +158,17 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
||||
ClientHttpResponse response = futureResponse.get();
|
||||
try {
|
||||
FileCopyUtils.copy(body, request.getBody());
|
||||
fail("IllegalStateException expected");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// expected
|
||||
}
|
||||
finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
@Test
|
||||
public void headersAfterExecute() throws Exception {
|
||||
AsyncClientHttpRequest request = factory.createAsyncRequest(new URI(baseUrl + "/echo"), HttpMethod.POST);
|
||||
request.getHeaders().add("MyHeader", "value");
|
||||
@@ -163,6 +179,10 @@ public abstract class AbstractAsyncHttpRequestFactoryTestCase extends AbstractJe
|
||||
ClientHttpResponse response = futureResponse.get();
|
||||
try {
|
||||
request.getHeaders().add("MyHeader", "value");
|
||||
fail("UnsupportedOperationException expected");
|
||||
}
|
||||
catch (UnsupportedOperationException ex) {
|
||||
// expected
|
||||
}
|
||||
finally {
|
||||
response.close();
|
||||
|
||||
@@ -22,17 +22,20 @@ import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.After;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.StreamingHttpOutputMessage;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public abstract class AbstractHttpRequestFactoryTestCase extends
|
||||
AbstractJettyServerTestCase {
|
||||
@@ -40,10 +43,21 @@ public abstract class AbstractHttpRequestFactoryTestCase extends
|
||||
protected ClientHttpRequestFactory factory;
|
||||
|
||||
@Before
|
||||
public final void createFactory() {
|
||||
public final void createFactory() throws Exception {
|
||||
factory = createRequestFactory();
|
||||
if (factory instanceof InitializingBean) {
|
||||
((InitializingBean) factory).afterPropertiesSet();
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public final void destroyFactory() throws Exception {
|
||||
if (factory instanceof DisposableBean) {
|
||||
((DisposableBean) factory).destroy();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected abstract ClientHttpRequestFactory createRequestFactory();
|
||||
|
||||
@Test
|
||||
@@ -53,7 +67,11 @@ public abstract class AbstractHttpRequestFactoryTestCase extends
|
||||
assertEquals("Invalid HTTP method", HttpMethod.GET, request.getMethod());
|
||||
assertEquals("Invalid HTTP URI", uri, request.getURI());
|
||||
ClientHttpResponse response = request.execute();
|
||||
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
try {
|
||||
assertEquals("Invalid status code", HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -31,12 +31,11 @@ import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.junit.AfterClass;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.SocketUtils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class AbstractJettyServerTestCase {
|
||||
@@ -147,6 +146,8 @@ public class AbstractJettyServerTestCase {
|
||||
|
||||
private void echo(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
response.setContentType(request.getContentType());
|
||||
response.setContentLength(request.getContentLength());
|
||||
for (Enumeration<String> e1 = request.getHeaderNames(); e1.hasMoreElements();) {
|
||||
String headerName = e1.nextElement();
|
||||
for (Enumeration<String> e2 = request.getHeaders(headerName); e2.hasMoreElements();) {
|
||||
@@ -154,7 +155,7 @@ public class AbstractJettyServerTestCase {
|
||||
response.addHeader(headerName, headerValue);
|
||||
}
|
||||
}
|
||||
FileCopyUtils.copy(request.getInputStream(), response.getOutputStream());
|
||||
StreamUtils.copy(request.getInputStream(), response.getOutputStream());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.client;
|
||||
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class Netty4AsyncClientHttpRequestFactoryTests
|
||||
extends AbstractAsyncHttpRequestFactoryTestCase {
|
||||
|
||||
private static EventLoopGroup eventLoopGroup;
|
||||
|
||||
@BeforeClass
|
||||
public static void createEventLoopGroup() {
|
||||
eventLoopGroup = new NioEventLoopGroup();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void shutdownEventLoopGroup() throws InterruptedException {
|
||||
eventLoopGroup.shutdownGracefully().sync();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AsyncClientHttpRequestFactory createRequestFactory() {
|
||||
return new Netty4ClientHttpRequestFactory(eventLoopGroup);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void httpMethods() throws Exception {
|
||||
assertHttpMethod("patch", HttpMethod.PATCH);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.http.client;
|
||||
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class Netty4ClientHttpRequestFactoryTests
|
||||
extends AbstractHttpRequestFactoryTestCase {
|
||||
|
||||
private static EventLoopGroup eventLoopGroup;
|
||||
|
||||
@BeforeClass
|
||||
public static void createEventLoopGroup() {
|
||||
eventLoopGroup = new NioEventLoopGroup();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void shutdownEventLoopGroup() throws InterruptedException {
|
||||
eventLoopGroup.shutdownGracefully().sync();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ClientHttpRequestFactory createRequestFactory() {
|
||||
return new Netty4ClientHttpRequestFactory(eventLoopGroup);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Test
|
||||
public void httpMethods() throws Exception {
|
||||
assertHttpMethod("patch", HttpMethod.PATCH);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user