@Bean @Primary @ConditionalOnProperty(value = "spring.sleuth.web.async.client.factory.enabled", matchIfMissing = true) public TraceAsyncClientHttpRequestFactoryWrapper traceAsyncClientHttpRequestFactory() { ClientHttpRequestFactory clientFactory = this.clientHttpRequestFactory; AsyncClientHttpRequestFactory asyncClientFactory = this.asyncClientHttpRequestFactory; if (clientFactory == null) { clientFactory = defaultClientHttpRequestFactory(this.tracer); } if (asyncClientFactory == null) { asyncClientFactory = clientFactory instanceof AsyncClientHttpRequestFactory ? (AsyncClientHttpRequestFactory) clientFactory : defaultClientHttpRequestFactory(this.tracer); } return new TraceAsyncClientHttpRequestFactoryWrapper(this.tracer, this.spanInjector, asyncClientFactory, clientFactory, this.httpTraceKeysInjector); }
@Bean @Resource(name = "jsonV1Converter") public AsyncRestTemplate asyncRestTemplate(AsyncClientHttpRequestFactory asyncClientHttpRequestFactory, MappingJackson2HttpMessageConverter jsonConverter) { AsyncRestTemplate restTemplate = new AsyncRestTemplate(asyncClientHttpRequestFactory); // Replace the default json converter by our converter // Remove for(HttpMessageConverter httpMessageConverter : restTemplate.getMessageConverters()) { if (httpMessageConverter instanceof MappingJackson2HttpMessageConverter) { restTemplate.getMessageConverters().remove(httpMessageConverter); break; } } // Add restTemplate.getMessageConverters().add(jsonConverter); return restTemplate; }
@SuppressWarnings("unused") public static Http create( final String baseUrl, final UrlResolution urlResolution, final AsyncClientHttpRequestFactory requestFactory, final List<HttpMessageConverter<?>> converters, final List<Plugin> plugins) { return Http.builder() .baseUrl(baseUrl) .urlResolution(urlResolution) .requestFactory(requestFactory) .converters(converters) .plugins(plugins) .build(); }
private String registerAsyncClientHttpRequestFactory(final String id, final Client client) { return registry.registerIfAbsent(id, AsyncClientHttpRequestFactory.class, () -> { log.debug("Client [{}]: Registering RestAsyncClientHttpRequestFactory", id); final BeanDefinitionBuilder factory = genericBeanDefinition(RestAsyncClientHttpRequestFactory.class); factory.addConstructorArgReference(registerHttpClient(id, client)); factory.addConstructorArgValue(genericBeanDefinition(ConcurrentTaskExecutor.class) // we allow users to use their own ExecutorService, but they don't have to configure tracing .addConstructorArgValue(registerExecutor(id, client)) .getBeanDefinition()); return factory; }); }
/** * Create a new instance of the {@code AsyncRestTemplate} using the given * {@link AsyncClientHttpRequestFactory} and synchronous {@link RestTemplate}. * @param requestFactory the asynchronous request factory to use * @param restTemplate the synchronous template to use */ public AsyncRestTemplate(AsyncClientHttpRequestFactory requestFactory, RestTemplate restTemplate) { Assert.notNull(restTemplate, "'restTemplate' must not be null"); this.syncTemplate = restTemplate; setAsyncRequestFactory(requestFactory); }
@SuppressWarnings("unused") public static Rest create(final AsyncClientHttpRequestFactory requestFactory, final List<HttpMessageConverter<?>> converters, final String baseUrl, final Plugins plugins) { return Rest.builder() .requestFactory(requestFactory) .converters(converters) .baseUrl(baseUrl) .plugins(plugins.resolvePlugins()) .build(); }
private String registerAsyncClientHttpRequestFactory(final String id, final Defaults defaults, final Client client) { return registry.register(id, AsyncClientHttpRequestFactory.class, () -> { LOG.debug("Client [{}]: Registering RestAsyncClientHttpRequestFactory", id); final BeanDefinitionBuilder factory = genericBeanDefinition(RestAsyncClientHttpRequestFactory.class); factory.addConstructorArgReference(registerHttpClient(id, defaults, client)); factory.addConstructorArgReference(registerAsyncListenableTaskExecutor(id)); return factory; }); }
/** * According to the JavaDocs all Spring {@link AsyncClientHttpRequestFactory} implement * the {@link ClientHttpRequestFactory} interface. * * In case that it's not true we're setting the {@link SimpleClientHttpRequestFactory} * as a default for sync request processing. * * @see org.springframework.web.client.AsyncRestTemplate#AsyncRestTemplate(AsyncClientHttpRequestFactory) */ public TraceAsyncClientHttpRequestFactoryWrapper(Tracer tracer, SpanInjector<HttpRequest> spanInjector, AsyncClientHttpRequestFactory asyncDelegate, HttpTraceKeysInjector httpTraceKeysInjector) { super(tracer, spanInjector, httpTraceKeysInjector); this.asyncDelegate = asyncDelegate; this.syncDelegate = asyncDelegate instanceof ClientHttpRequestFactory ? (ClientHttpRequestFactory) asyncDelegate : defaultClientHttpRequestFactory(); }
public TraceAsyncClientHttpRequestFactoryWrapper(Tracer tracer, SpanInjector<HttpRequest> spanInjector, AsyncClientHttpRequestFactory asyncDelegate, ClientHttpRequestFactory syncDelegate, HttpTraceKeysInjector httpTraceKeysInjector) { super(tracer, spanInjector, httpTraceKeysInjector); this.asyncDelegate = asyncDelegate; this.syncDelegate = syncDelegate; }
Requester(final AsyncClientHttpRequestFactory requestFactory, final MessageWorker worker, final RequestArguments arguments, final List<Plugin> plugins) { this.requestFactory = requestFactory; this.worker = worker; this.arguments = arguments; this.plugin = Plugin.compound(plugins); }
DefaultHttp(final AsyncClientHttpRequestFactory requestFactory, final List<HttpMessageConverter<?>> converters, final Supplier<URI> baseUrlProvider, final UrlResolution resolution, final List<Plugin> plugins) { this.requestFactory = checkNotNull(requestFactory, "request factory"); this.worker = new MessageWorker(converters); this.baseUrlProvider = checkNotNull(baseUrlProvider, "base url provider"); this.arguments = RequestArguments.create().withUrlResolution(resolution); this.plugins = plugins; }
@Test public void shouldHandleExceptionDuringRequestCreation() throws URISyntaxException { exception.expect(UncheckedIOException.class); exception.expectCause(instanceOf(IOException.class)); exception.expectMessage("Could not create request"); final AsyncClientHttpRequestFactory factory = (uri, httpMethod) -> { throw new IOException("Could not create request"); }; Http.builder().requestFactory(factory).defaultConverters().build() .get("http://localhost/") .dispatch(series(), on(SUCCESSFUL).call(pass())); }
@Test public void shouldCreateBuilder() { final Rest rest = Rest.builder() .requestFactory(mock(AsyncClientHttpRequestFactory.class)) .build(); assertThat(rest, is(notNullValue())); }
private void registerAsyncClientHttpRequestFactory(final String id) { registry.registerIfAbsent(id, AsyncClientHttpRequestFactory.class, () -> { log.debug("Client [{}]: Registering mocked AsyncClientHttpRequestFactory", id); final BeanDefinitionBuilder factory = genericBeanDefinition(AsyncClientHttpRequestFactory.class); factory.addDependsOn(RiptideTestAutoConfiguration.SERVER_BEAN_NAME); factory.setFactoryMethodOnBean("getAsyncRequestFactory", RiptideTestAutoConfiguration.TEMPLATE_BEAN_NAME); return factory; }); }
@Bean public Http exampleHttp(final AsyncClientHttpRequestFactory requestFactory, final ClientHttpMessageConverters converters, final GaugeService gaugeService, final ScheduledExecutorService scheduler) { return Http.builder() .baseUrl("https://www.example.com") .urlResolution(UrlResolution.RFC) .requestFactory(requestFactory) .converters(converters.getConverters()) .plugin(new MetricsPlugin(gaugeService, new ZMONMetricsNameGenerator())) .plugin(new TransientFaultPlugin( FaultClassifier.create(ImmutableList.<Predicate<Throwable>>builder() .addAll(FaultClassifier.defaults()) .add(ConnectionClosedException.class::isInstance) .add(NoHttpResponseException.class::isInstance) .build()))) .plugin(new FailsafePlugin(scheduler) .withRetryPolicy(new RetryPolicy() .retryOn(TransientFaultException.class) .withBackoff(50, 2000, MILLISECONDS) .withMaxRetries(10) .withMaxDuration(2, SECONDS) .withJitter(0.2)) .withCircuitBreaker(new CircuitBreaker() .withFailureThreshold(5, 5) .withDelay(30, SECONDS) .withSuccessThreshold(3, 5) .withTimeout(3, SECONDS))) .plugin(new BackupRequestPlugin(scheduler, 10, MILLISECONDS)) .plugin(new TimeoutPlugin(scheduler, 3, SECONDS)) .plugin(new OriginalStackTracePlugin()) .plugin(new CustomPlugin()) .build(); }
@Bean public AsyncRestTemplate exampleAsyncRestTemplate(final AsyncClientHttpRequestFactory requestFactory, final ClientHttpMessageConverters converters) { final AsyncRestTemplate template = new AsyncRestTemplate(); final DefaultUriTemplateHandler handler = new DefaultUriTemplateHandler(); handler.setBaseUrl("https://www.example.com"); template.setUriTemplateHandler(handler); template.setAsyncRequestFactory(requestFactory); template.setMessageConverters(converters.getConverters()); return template; }
private TraceAsyncClientHttpRequestFactoryWrapper traceAsyncClientHttpRequestFactory() { ClientHttpRequestFactory clientFactory = this.clientHttpRequestFactory; AsyncClientHttpRequestFactory asyncClientFactory = this.asyncClientHttpRequestFactory; if (clientFactory == null) { clientFactory = defaultClientHttpRequestFactory(this.tracer); } if (asyncClientFactory == null) { asyncClientFactory = clientFactory instanceof AsyncClientHttpRequestFactory ? (AsyncClientHttpRequestFactory) clientFactory : defaultClientHttpRequestFactory(this.tracer); } return new TraceAsyncClientHttpRequestFactoryWrapper(this.tracer, this.spanInjector, asyncClientFactory, clientFactory, this.httpTraceKeysInjector); }
/** * According to the JavaDocs all Spring {@link AsyncClientHttpRequestFactory} implement * the {@link ClientHttpRequestFactory} interface. * * In case that it's not true we're setting the {@link SimpleClientHttpRequestFactory} * as a default for sync request processing. * * @see org.springframework.web.client.AsyncRestTemplate#AsyncRestTemplate(AsyncClientHttpRequestFactory) */ public TraceAsyncClientHttpRequestFactoryWrapper(Tracer tracer, HttpSpanInjector spanInjector, AsyncClientHttpRequestFactory asyncDelegate, HttpTraceKeysInjector httpTraceKeysInjector) { super(tracer, spanInjector, httpTraceKeysInjector); this.asyncDelegate = asyncDelegate; this.syncDelegate = asyncDelegate instanceof ClientHttpRequestFactory ? (ClientHttpRequestFactory) asyncDelegate : defaultClientHttpRequestFactory(); }
public TraceAsyncClientHttpRequestFactoryWrapper(Tracer tracer, HttpSpanInjector spanInjector, AsyncClientHttpRequestFactory asyncDelegate, ClientHttpRequestFactory syncDelegate, HttpTraceKeysInjector httpTraceKeysInjector) { super(tracer, spanInjector, httpTraceKeysInjector); this.asyncDelegate = asyncDelegate; this.syncDelegate = syncDelegate; }
@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); }
/** * Set the request factory that this accessor uses for obtaining {@link * org.springframework.http.client.ClientHttpRequest HttpRequests}. */ public void setAsyncRequestFactory(AsyncClientHttpRequestFactory asyncRequestFactory) { Assert.notNull(asyncRequestFactory, "'asyncRequestFactory' must not be null"); this.asyncRequestFactory = asyncRequestFactory; }
@Bean @DependsOn("server") public AsyncClientHttpRequestFactory exampleAsyncClientHttpRequestFactory(final AsyncRestTemplate template) { return template.getAsyncRequestFactory(); }
@Bean @DependsOn("server") public AsyncClientHttpRequestFactory fooAsyncClientHttpRequestFactory(final AsyncRestTemplate template) { return template.getAsyncRequestFactory(); }
@Bean @Qualifier("example") public AsyncClientHttpRequestFactory exampleAsyncClientHttpRequestFactory() { return mock(AsyncClientHttpRequestFactory.class, withSettings().extraInterfaces(ClientHttpRequestFactory.class)); }
public TraceAsyncRestTemplate(AsyncClientHttpRequestFactory asyncRequestFactory, Tracer tracer) { super(asyncRequestFactory); this.tracer = tracer; }
public TraceAsyncRestTemplate(AsyncClientHttpRequestFactory asyncRequestFactory, ClientHttpRequestFactory syncRequestFactory, Tracer tracer) { super(asyncRequestFactory, syncRequestFactory); this.tracer = tracer; }
public TraceAsyncRestTemplate(AsyncClientHttpRequestFactory requestFactory, RestTemplate restTemplate, Tracer tracer) { super(requestFactory, restTemplate); this.tracer = tracer; }
@Bean public AsyncClientHttpRequestFactory clientHttpRequestFactory(CloseableHttpAsyncClient closeableHttpAsyncClient) { return new HttpComponentsAsyncClientHttpRequestFactory(closeableHttpAsyncClient); }
@Test public void shouldConfigure() { final HttpBuilder builder = spy(HttpBuilder.class); final RestConfigurer unit = RestBuilder.simpleRequestFactory(newDirectExecutorService()); unit.configure(builder); final ArgumentCaptor<AsyncClientHttpRequestFactory> captor = ArgumentCaptor.forClass(AsyncClientHttpRequestFactory.class); verify(builder).requestFactory(captor.capture()); final AsyncClientHttpRequestFactory factory = captor.getValue(); assertThat(factory, is(instanceOf(SimpleClientHttpRequestFactory.class))); }
@Bean @DependsOn("server") public AsyncClientHttpRequestFactory githubAsyncClientHttpRequestFactory(final AsyncRestTemplate template) { return template.getAsyncRequestFactory(); }
public AsyncRest(AsyncClientHttpRequestFactory asyncRequestFactory) { super(asyncRequestFactory); }
public AsyncRest(AsyncClientHttpRequestFactory asyncRequestFactory, ClientHttpRequestFactory syncRequestFactory) { super(asyncRequestFactory, syncRequestFactory); }
public AsyncRest(AsyncClientHttpRequestFactory requestFactory, RestTemplate restTemplate) { super(requestFactory, restTemplate); }
public TraceAsyncRestTemplate(AsyncClientHttpRequestFactory asyncRequestFactory, Tracer tracer, ErrorParser errorParser) { super(asyncRequestFactory); this.tracer = tracer; this.errorParser = errorParser; }
public TraceAsyncRestTemplate(AsyncClientHttpRequestFactory asyncRequestFactory, ClientHttpRequestFactory syncRequestFactory, Tracer tracer, ErrorParser errorParser) { super(asyncRequestFactory, syncRequestFactory); this.tracer = tracer; this.errorParser = errorParser; }
public TraceAsyncRestTemplate(AsyncClientHttpRequestFactory requestFactory, RestTemplate restTemplate, Tracer tracer, ErrorParser errorParser) { super(requestFactory, restTemplate); this.tracer = tracer; this.errorParser = errorParser; }
@Bean AsyncClientHttpRequestFactory myAsyncClientFactory() { return new MyAsyncClientHttpRequestFactory(); }