Java Code Examples for org.springframework.http.client.AsyncClientHttpRequestInterceptor

The following examples show how to use org.springframework.http.client.AsyncClientHttpRequestInterceptor. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Bean
public SmartInitializingSingleton loadBalancedAsyncRestTemplateInitializer2(
        final List<AsyncRestTemplateCustomizer> customizers) {
    return new SmartInitializingSingleton() {
        @Override
        public void afterSingletonsInstantiated() {
            logger.info("init AsyncRestTemplateCircuitBreaker start");
            for (AsyncRestTemplate restTemplate : AsyncRestTemplateCircuitBreakerAutoConfiguration.this
                    .restTemplates) {
                AsyncRestTemplateCircuitInterceptor interceptor =
                        new AsyncRestTemplateCircuitInterceptor(circuitBreakerCore);
                logger.info("add AsyncRestTemplateCircuitInterceptor first");
                List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
                interceptors.add(0, interceptor);
                restTemplate.setInterceptors(interceptors);
            }
            logger.info("init AsyncRestTemplateCircuitBreaker end");
        }
    };
}
 
Example 2
@Bean
public SmartInitializingSingleton loadBalancedAsyncRestTemplateInitializer2(
        final List<AsyncRestTemplateCustomizer> customizers) {
    return new SmartInitializingSingleton() {
        @Override
        public void afterSingletonsInstantiated() {
            logger.info("Init AsyncRestTemplateRateLimiterConfiguration");
            for (AsyncRestTemplate restTemplate : AsyncRestTemplateRateLimiterConfiguration.this.restTemplates) {
                List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();
                logger.debug("AsyncRestTemplate init start, interceptor size:" + interceptors.size());
                AsyncClientHttpRequestInterceptor interceptor =
                        new AsyncRestTemplateRateLimiterInterceptor(serviceName);
                interceptors.add(0, interceptor);
                logger.debug("AsyncRestTemplate init end, Add AsyncRestTemplateRateLimiterInterceptor");
                restTemplate.setInterceptors(interceptors);
            }
        }
    };
}
 
Example 3
Source Project: haven-platform   Source File: DockerServiceFactory.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source Project: summerframework   Source File: RestTemplatePostProcessor.java    License: Apache License 2.0 5 votes vote down vote up
public void customize(final AsyncRestTemplate restTemplate) {
    if (restTemplate.getInterceptors().contains(this.metricsClientHttpRequestInterceptor)) {
        return;
    }
    UriTemplateHandler templateHandler = restTemplate.getUriTemplateHandler();
    templateHandler = this.metricsClientHttpRequestInterceptor.createUriTemplateHandler(templateHandler);
    restTemplate.setUriTemplateHandler(templateHandler);
    List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>();
    interceptors.add(this.metricsClientHttpRequestInterceptor);
    interceptors.addAll(restTemplate.getInterceptors());
    restTemplate.setInterceptors(interceptors);
}
 
Example 5
Source Project: sofa-tracer   Source File: SofaTracerRestTemplateBuilder.java    License: Apache License 2.0 5 votes vote down vote up
@Deprecated
public static AsyncRestTemplate buildAsyncRestTemplate() {
    AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate();
    List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<AsyncClientHttpRequestInterceptor>();
    AsyncRestTemplateRequestInterceptor asyncRestTemplateInterceptor = new AsyncRestTemplateRequestInterceptor(
        getRestTemplateTracer());
    interceptors.add(asyncRestTemplateInterceptor);
    asyncRestTemplate.setInterceptors(interceptors);
    return asyncRestTemplate;
}
 
Example 6
private void registerTracingInterceptor(InterceptingAsyncHttpAccessor restTemplate) {
    List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate.getInterceptors();

    for (AsyncClientHttpRequestInterceptor interceptor : interceptors) {
        if (interceptor instanceof TracingAsyncRestTemplateInterceptor) {
            return;
        }
    }

    log.debug("Adding " + TracingAsyncRestTemplateInterceptor.class.getSimpleName() + " to " + restTemplate);
    interceptors = new ArrayList<>(interceptors);
    interceptors.add(new TracingAsyncRestTemplateInterceptor(tracer, spanDecorators));
    restTemplate.setInterceptors(interceptors);
}
 
Example 7
@PostConstruct
public void init() {
	if (this.restTemplates != null) {
		for (AsyncRestTemplate restTemplate : this.restTemplates) {
			List<AsyncClientHttpRequestInterceptor> interceptors = new ArrayList<>(
					restTemplate.getInterceptors());
			interceptors.add(this.clientInterceptor);
			restTemplate.setInterceptors(interceptors);
		}
	}
}
 
Example 8
@Bean
public AsyncRestTemplateCustomizer asyncRestTemplateCustomizer(
		final AsyncLoadBalancerInterceptor loadBalancerInterceptor) {
	return restTemplate -> {
		List<AsyncClientHttpRequestInterceptor> list = new ArrayList<>(
				restTemplate.getInterceptors());
		list.add(loadBalancerInterceptor);
		restTemplate.setInterceptors(list);
	};
}
 
Example 9
private void assertLoadBalanced(AsyncRestTemplate restTemplate) {
	List<AsyncClientHttpRequestInterceptor> interceptors = restTemplate
			.getInterceptors();
	then(interceptors).hasSize(1);
	AsyncClientHttpRequestInterceptor interceptor = interceptors.get(0);
	then(interceptor).isInstanceOf(AsyncLoadBalancerInterceptor.class);
}
 
Example 10
AsyncClientHttpRequestFactory configureClient(AsyncClientHttpRequestInterceptor interceptor) {
  HttpComponentsAsyncClientHttpRequestFactory factory =
    new HttpComponentsAsyncClientHttpRequestFactory(asyncClient);
  factory.setReadTimeout(1000);
  factory.setConnectTimeout(1000);
  this.interceptor = interceptor;
  return factory;
}
 
Example 11
@Test public void autowiredWithBeanConfig() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(HttpTracingConfiguration.class);
  ctx.register(TracingAsyncClientHttpRequestInterceptor.class);
  ctx.refresh();

  ctx.getBean(AsyncClientHttpRequestInterceptor.class);
}
 
Example 12
@Test
public void testAsyncInterceptorNotRegistered() {
    for (AsyncClientHttpRequestInterceptor interceptor : asyncRestTemplate.getInterceptors()) {
        assertThat(interceptor).isNotInstanceOf(TracingRestTemplateInterceptor.class);
    }
}
 
Example 13
/**
 * Return the request interceptor that this accessor uses.
 */
public List<AsyncClientHttpRequestInterceptor> getInterceptors() {
    return this.interceptors;
}
 
Example 14
public static AsyncClientHttpRequestInterceptor create(Tracing tracing) {
  return create(HttpTracing.create(tracing));
}
 
Example 15
public static AsyncClientHttpRequestInterceptor create(HttpTracing httpTracing) {
  return new TracingAsyncClientHttpRequestInterceptor(httpTracing);
}
 
Example 16
/**
 * Set the request interceptors that this accessor should use.
 * @param interceptors the list of interceptors
 */
public void setInterceptors(List<AsyncClientHttpRequestInterceptor> interceptors) {
    this.interceptors = interceptors;
}