Added docs for customization executors

This commit is contained in:
Marcin Grzejszczak
2017-04-18 16:20:24 +02:00
parent 9dc626a873
commit a879dc9e30
2 changed files with 47 additions and 9 deletions

View File

@@ -596,6 +596,16 @@ Here you can see an example of how to pass tracing information with `TraceableEx
include::../../../../spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/TraceableExecutorServiceTests.java[tags=completablefuture,indent=0]
----
===== Customization of Executors
Sometimes you need to set up a custom instance of the `AsyncExecutor`. In the following snippet you
can see an example of how to set up such a custom `Executor`.
[source,java]
----
include::../../../../spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/async/MultipleAsyncRestTemplateTests.java[tags=custom_executor,indent=0]
----
=== Messaging
Spring Cloud Sleuth integrates with http://projects.spring.io/spring-integration/[Spring Integration]. It creates spans for publish and

View File

@@ -22,6 +22,7 @@ import java.util.concurrent.Executor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -39,6 +40,10 @@ 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.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.AsyncRestTemplate;
@@ -47,18 +52,26 @@ import static org.assertj.core.api.BDDAssertions.then;
/**
* @author Marcin Grzejszczak
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MultipleAsyncRestTemplateTests.Config.class,
@RunWith(SpringRunner.class) @SpringBootTest(
classes = { MultipleAsyncRestTemplateTests.Config.class,
MultipleAsyncRestTemplateTests.CustomExecutorConfig.class },
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MultipleAsyncRestTemplateTests {
@Autowired @Qualifier("customAsyncRestTemplate") AsyncRestTemplate asyncRestTemplate;
@Autowired AsyncConfigurer executor;
@Test
public void should_start_context_with_custom_async_client() throws Exception {
then(this.asyncRestTemplate).isNotNull();
}
@Test
public void should_start_context_with_custom_executor() throws Exception {
then(this.executor).isNotNull();
then(this.executor.getAsyncExecutor()).isInstanceOf(LazyTraceExecutor.class);
}
//tag::custom_async_rest_template[]
@Configuration
@EnableAutoConfiguration
@@ -95,6 +108,28 @@ public class MultipleAsyncRestTemplateTests {
}
}
//end::custom_async_rest_template[]
//tag::custom_executor[]
@Configuration
@EnableAutoConfiguration
@EnableAsync
static class CustomExecutorConfig extends AsyncConfigurerSupport {
@Autowired BeanFactory beanFactory;
@Override public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// CUSTOMIZE HERE
executor.setCorePoolSize(7);
executor.setMaxPoolSize(42);
executor.setQueueCapacity(11);
executor.setThreadNamePrefix("MyExecutor-");
// DON'T FORGET TO INITIALIZE
executor.initialize();
return new LazyTraceExecutor(this.beanFactory, executor);
}
}
//end::custom_executor[]
}
class CustomClientHttpRequestFactory implements ClientHttpRequestFactory {
@@ -112,11 +147,4 @@ class CustomAsyncClientHttpRequestFactory implements AsyncClientHttpRequestFacto
throws IOException {
return null;
}
}
class YourCustomThreadPoolTaskExecutor implements Executor {
@Override public void execute(Runnable command) {
}
}