Fix for inability to provide custom implementation of AsyncClientHttpRequestFactory (#342)

This commit is contained in:
Premanand Chandrasekaran
2016-07-19 04:13:18 -04:00
committed by Marcin Grzejszczak
parent 4a6a556af0
commit ee80b301e8
3 changed files with 156 additions and 102 deletions

View File

@@ -5,7 +5,7 @@
* 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
* 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,
@@ -29,7 +29,6 @@ import org.springframework.cloud.sleuth.autoconfig.TraceAutoConfiguration;
import org.springframework.cloud.sleuth.instrument.web.HttpTraceKeysInjector;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.AsyncListenableTaskExecutor;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.AsyncClientHttpRequestFactory;
@@ -54,15 +53,16 @@ import org.springframework.web.client.AsyncRestTemplate;
public class TraceWebAsyncClientAutoConfiguration {
@Autowired Tracer tracer;
@Autowired HttpTraceKeysInjector httpTraceKeysInjector;
@Autowired SpanInjector<HttpRequest> spanInjector;
@Autowired(required = false) ClientHttpRequestFactory clientHttpRequestFactory;
@Autowired(required = false) AsyncClientHttpRequestFactory asyncClientHttpRequestFactory;
@Autowired
private HttpTraceKeysInjector httpTraceKeysInjector;
@Autowired
private SpanInjector<HttpRequest> spanInjector;
@Autowired(required = false)
private ClientHttpRequestFactory clientHttpRequestFactory;
@Autowired(required = false)
private AsyncClientHttpRequestFactory asyncClientHttpRequestFactory;
@Bean
@Primary
@ConditionalOnProperty(value = "spring.sleuth.web.async.client.factory.enabled", matchIfMissing = true)
public TraceAsyncClientHttpRequestFactoryWrapper traceAsyncClientHttpRequestFactory() {
private TraceAsyncClientHttpRequestFactoryWrapper traceAsyncClientHttpRequestFactory() {
ClientHttpRequestFactory clientFactory = this.clientHttpRequestFactory;
AsyncClientHttpRequestFactory asyncClientFactory = this.asyncClientHttpRequestFactory;
if (clientFactory == null) {

View File

@@ -1,92 +0,0 @@
/*
* Copyright 2013-2016 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.cloud.sleuth.instrument.web.client;
import java.io.IOException;
import java.net.URI;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.AsyncClientHttpRequest;
import org.springframework.http.client.AsyncClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.AsyncRestTemplate;
import static org.assertj.core.api.BDDAssertions.then;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
* @author Marcin Grzejszczak
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {
TraceWebAsyncClientAutoConfigurationTest.TestConfiguration.class }, webEnvironment = RANDOM_PORT)
public class TraceWebAsyncClientAutoConfigurationTest {
@Autowired AsyncRestTemplate asyncRestTemplate;
@Autowired MySyncClientHttpRequestFactory mySyncClientHttpRequestFactory;
@Autowired MyAsyncClientHttpRequestFactory myAsyncClientHttpRequestFactory;
@Test
public void should_inject_to_async_rest_template_custom_client_http_request_factory() {
then(this.asyncRestTemplate.getAsyncRequestFactory()).isInstanceOf(TraceAsyncClientHttpRequestFactoryWrapper.class);
TraceAsyncClientHttpRequestFactoryWrapper wrapper = (TraceAsyncClientHttpRequestFactoryWrapper) this.asyncRestTemplate.getAsyncRequestFactory();
then(wrapper.syncDelegate).isSameAs(this.mySyncClientHttpRequestFactory);
then(wrapper.asyncDelegate).isSameAs(this.myAsyncClientHttpRequestFactory);
then(this.asyncRestTemplate).isInstanceOf(TraceAsyncRestTemplate.class);
}
// tag::async_template_factories[]
@EnableAutoConfiguration
@Configuration
public static class TestConfiguration {
@Bean
ClientHttpRequestFactory mySyncClientFactory() {
return new MySyncClientHttpRequestFactory();
}
@Bean
AsyncClientHttpRequestFactory myAsyncClientFactory() {
return new MyAsyncClientHttpRequestFactory();
}
}
// end::async_template_factories[]
private static class MySyncClientHttpRequestFactory implements ClientHttpRequestFactory {
@Override public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod)
throws IOException {
return null;
}
}
private static class MyAsyncClientHttpRequestFactory implements AsyncClientHttpRequestFactory {
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
throws IOException {
return null;
}
}
}

View File

@@ -0,0 +1,146 @@
/*
* Copyright 2013-2016 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.cloud.sleuth.instrument.web.client;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.*;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.AsyncRestTemplate;
import java.io.IOException;
import java.net.URI;
import static org.assertj.core.api.BDDAssertions.then;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
/**
* @author Marcin Grzejszczak
*/
@RunWith(Enclosed.class)
public class TraceWebAsyncClientAutoConfigurationTests {
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {
CustomSyncAndAsyncClientFactory.TestConfiguration.class }, webEnvironment = RANDOM_PORT)
public static class CustomSyncAndAsyncClientFactory {
@Autowired AsyncRestTemplate asyncRestTemplate;
@Test
public void should_inject_to_async_rest_template_custom_client_http_request_factory() {
then(this.asyncRestTemplate.getAsyncRequestFactory()).isInstanceOf(TraceAsyncClientHttpRequestFactoryWrapper.class);
TraceAsyncClientHttpRequestFactoryWrapper wrapper = (TraceAsyncClientHttpRequestFactoryWrapper) this.asyncRestTemplate.getAsyncRequestFactory();
then(wrapper.syncDelegate).isInstanceOf(MySyncClientHttpRequestFactory.class);
then(wrapper.asyncDelegate).isInstanceOf(MyAsyncClientHttpRequestFactory.class);
then(this.asyncRestTemplate).isInstanceOf(TraceAsyncRestTemplate.class);
}
// tag::async_template_factories[]
@EnableAutoConfiguration
@Configuration
public static class TestConfiguration {
@Bean
ClientHttpRequestFactory mySyncClientFactory() {
return new MySyncClientHttpRequestFactory();
}
@Bean
AsyncClientHttpRequestFactory myAsyncClientFactory() {
return new MyAsyncClientHttpRequestFactory();
}
}
// end::async_template_factories[]
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {
CustomSyncClientFactory.TestConfiguration.class }, webEnvironment = RANDOM_PORT)
public static class CustomSyncClientFactory {
@Autowired AsyncRestTemplate asyncRestTemplate;
@Test
public void should_inject_to_async_rest_template_custom_client_http_request_factory() {
then(this.asyncRestTemplate.getAsyncRequestFactory()).isInstanceOf(TraceAsyncClientHttpRequestFactoryWrapper.class);
TraceAsyncClientHttpRequestFactoryWrapper wrapper = (TraceAsyncClientHttpRequestFactoryWrapper) this.asyncRestTemplate.getAsyncRequestFactory();
then(wrapper.syncDelegate).isInstanceOf(MySyncClientHttpRequestFactory.class);
then(wrapper.asyncDelegate).isInstanceOf(SimpleClientHttpRequestFactory.class);
then(this.asyncRestTemplate).isInstanceOf(TraceAsyncRestTemplate.class);
}
@EnableAutoConfiguration
@Configuration
public static class TestConfiguration {
@Bean
ClientHttpRequestFactory mySyncClientFactory() {
return new MySyncClientHttpRequestFactory();
}
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {
CustomASyncClientFactory.TestConfiguration.class }, webEnvironment = RANDOM_PORT)
public static class CustomASyncClientFactory {
@Autowired AsyncRestTemplate asyncRestTemplate;
@Test
public void should_inject_to_async_rest_template_custom_client_http_request_factory() {
then(this.asyncRestTemplate.getAsyncRequestFactory()).isInstanceOf(TraceAsyncClientHttpRequestFactoryWrapper.class);
TraceAsyncClientHttpRequestFactoryWrapper wrapper = (TraceAsyncClientHttpRequestFactoryWrapper) this.asyncRestTemplate.getAsyncRequestFactory();
then(wrapper.syncDelegate).isInstanceOf(SimpleClientHttpRequestFactory.class);
then(wrapper.asyncDelegate).isInstanceOf(AsyncClientHttpRequestFactory.class);
then(this.asyncRestTemplate).isInstanceOf(TraceAsyncRestTemplate.class);
}
@EnableAutoConfiguration
@Configuration
public static class TestConfiguration {
@Bean
AsyncClientHttpRequestFactory myAsyncClientFactory() {
return new MyAsyncClientHttpRequestFactory();
}
}
}
private static class MySyncClientHttpRequestFactory implements ClientHttpRequestFactory {
@Override public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod)
throws IOException {
return null;
}
}
private static class MyAsyncClientHttpRequestFactory implements AsyncClientHttpRequestFactory {
@Override
public AsyncClientHttpRequest createAsyncRequest(URI uri, HttpMethod httpMethod)
throws IOException {
return null;
}
}
}