private AsyncRestTemplate createNewRestTemplate(String addr) { // we use async client because usual client does not allow to interruption in some cases NettyRequestFactory factory = new NettyRequestFactory(); if(AddressUtils.isHttps(addr)) { try { initSsl(addr, factory); } catch (Exception e) { log.error("", e); } } final AsyncRestTemplate restTemplate = new AsyncRestTemplate(factory); List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>(); interceptors.add(new HttpAuthInterceptor(registryRepository)); if(!StringUtils.isEmpty(agentPassword)) { interceptors.add(new BasicAuthAsyncInterceptor("admin", agentPassword)); } restTemplate.setInterceptors(interceptors); return restTemplate; }
public TracingAsyncRestTemplateTest() { final AsyncRestTemplate restTemplate = new AsyncRestTemplate(); restTemplate.setInterceptors(Collections.<AsyncClientHttpRequestInterceptor>singletonList( new TracingAsyncRestTemplateInterceptor(mockTracer, Collections.<RestTemplateSpanDecorator>singletonList(new RestTemplateSpanDecorator.StandardTags())))); client = new Client<AsyncRestTemplate>() { @Override public <T> ResponseEntity<T> getForEntity(String url, Class<T> clazz) { ListenableFuture<ResponseEntity<T>> forEntity = restTemplate.getForEntity(url, clazz); try { return forEntity.get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); Assert.fail(); } return null; } @Override public AsyncRestTemplate template() { return restTemplate; } }; mockServer = MockRestServiceServer.bindTo(client.template()).ignoreExpectOrder(true).build(); }
private void registerTracingInterceptor(AsyncRestTemplate restTemplate) { List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); for (AsyncClientHttpRequestInterceptor interceptor: interceptors) { if (interceptor instanceof TracingAsyncRestTemplateInterceptor) { return; } } log.info("Adding " + TracingAsyncRestTemplateInterceptor.class.getSimpleName() + " to async rest template"); interceptors = new ArrayList<>(interceptors); interceptors.add(new TracingAsyncRestTemplateInterceptor(tracer)); restTemplate.setInterceptors(interceptors); }
@Bean public AsyncRestTemplateCustomizer asyncRestTemplateCustomizer( final AsyncLoadBalancerInterceptor loadBalancerInterceptor) { return new AsyncRestTemplateCustomizer() { @Override public void customize(AsyncRestTemplate restTemplate) { List<AsyncClientHttpRequestInterceptor> list = new ArrayList<>( restTemplate.getInterceptors()); list.add(loadBalancerInterceptor); restTemplate.setInterceptors(list); } }; }
private void assertLoadBalanced(AsyncRestTemplate restTemplate) { List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors(); MatcherAssert.assertThat(interceptors, hasSize(1)); AsyncClientHttpRequestInterceptor interceptor = interceptors.get(0); MatcherAssert.assertThat(interceptor, is(instanceOf(AsyncLoadBalancerInterceptor.class))); }