org.springframework.http.client.AsyncClientHttpRequestInterceptor Java Examples

The following examples show how to use org.springframework.http.client.AsyncClientHttpRequestInterceptor. 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
Source File: AsyncRestTemplateCircuitBreakerAutoConfiguration.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
@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
Source File: AsyncRestTemplateRateLimiterConfiguration.java    From spring-cloud-formula with Apache License 2.0 6 votes vote down vote up
@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 File: DockerServiceFactory.java    From haven-platform with 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 File: RestTemplatePostProcessor.java    From summerframework with 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 File: SofaTracerRestTemplateBuilder.java    From sofa-tracer with 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
Source File: RestTemplateTracingAutoConfiguration.java    From java-spring-web with Apache License 2.0 5 votes vote down vote up
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
Source File: TraceWebAsyncClientAutoConfiguration.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@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
Source File: AsyncLoadBalancerAutoConfiguration.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Bean
public AsyncRestTemplateCustomizer asyncRestTemplateCustomizer(
		final AsyncLoadBalancerInterceptor loadBalancerInterceptor) {
	return restTemplate -> {
		List<AsyncClientHttpRequestInterceptor> list = new ArrayList<>(
				restTemplate.getInterceptors());
		list.add(loadBalancerInterceptor);
		restTemplate.setInterceptors(list);
	};
}
 
Example #9
Source File: AsyncLoadBalancerAutoConfigurationTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
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
Source File: ITTracingAsyncClientHttpRequestInterceptor.java    From brave with Apache License 2.0 5 votes vote down vote up
AsyncClientHttpRequestFactory configureClient(AsyncClientHttpRequestInterceptor interceptor) {
  HttpComponentsAsyncClientHttpRequestFactory factory =
    new HttpComponentsAsyncClientHttpRequestFactory(asyncClient);
  factory.setReadTimeout(1000);
  factory.setConnectTimeout(1000);
  this.interceptor = interceptor;
  return factory;
}
 
Example #11
Source File: TracingAsyncClientHttpRequestInterceptorAutowireTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void autowiredWithBeanConfig() {
  AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  ctx.register(HttpTracingConfiguration.class);
  ctx.register(TracingAsyncClientHttpRequestInterceptor.class);
  ctx.refresh();

  ctx.getBean(AsyncClientHttpRequestInterceptor.class);
}
 
Example #12
Source File: DisabledRestTemplateTracingAutoConfigurationTest.java    From java-spring-web with Apache License 2.0 4 votes vote down vote up
@Test
public void testAsyncInterceptorNotRegistered() {
    for (AsyncClientHttpRequestInterceptor interceptor : asyncRestTemplate.getInterceptors()) {
        assertThat(interceptor).isNotInstanceOf(TracingRestTemplateInterceptor.class);
    }
}
 
Example #13
Source File: InterceptingAsyncHttpAccessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the request interceptor that this accessor uses.
 */
public List<AsyncClientHttpRequestInterceptor> getInterceptors() {
    return this.interceptors;
}
 
Example #14
Source File: TracingAsyncClientHttpRequestInterceptor.java    From brave with Apache License 2.0 4 votes vote down vote up
public static AsyncClientHttpRequestInterceptor create(Tracing tracing) {
  return create(HttpTracing.create(tracing));
}
 
Example #15
Source File: TracingAsyncClientHttpRequestInterceptor.java    From brave with Apache License 2.0 4 votes vote down vote up
public static AsyncClientHttpRequestInterceptor create(HttpTracing httpTracing) {
  return new TracingAsyncClientHttpRequestInterceptor(httpTracing);
}
 
Example #16
Source File: InterceptingAsyncHttpAccessor.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Set the request interceptors that this accessor should use.
 * @param interceptors the list of interceptors
 */
public void setInterceptors(List<AsyncClientHttpRequestInterceptor> interceptors) {
    this.interceptors = interceptors;
}