@Override public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception { ResponseEntity<String> stringResponseEntity = null; try (CloseableHttpAsyncClient hc = createCloseableHttpAsyncClient()) { for (int i = 0; i < requestOptions.getCount(); i++) { final HttpHeaders headers = new HttpHeaders(); for (Map.Entry<String, String> e : requestOptions.getHeaderMap().entrySet()) { headers.put(e.getKey(), Collections.singletonList(e.getValue())); } final HttpEntity<Void> requestEntity = new HttpEntity<>(headers); AsyncRestTemplate template = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory(hc)); final ListenableFuture<ResponseEntity<String>> exchange = template.exchange(requestOptions.getUrl(), HttpMethod.GET, requestEntity, String.class); stringResponseEntity = exchange.get(); System.out.println(stringResponseEntity.getBody()); } return stringResponseEntity; } }
@Test public void restartWithKeepAlive() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); this.container = factory .getEmbeddedServletContainer(exampleServletRegistration()); this.container.start(); HttpComponentsAsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory(); ListenableFuture<ClientHttpResponse> response1 = clientHttpRequestFactory .createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET) .executeAsync(); assertThat(response1.get(10, TimeUnit.SECONDS).getRawStatusCode()).isEqualTo(200); this.container.stop(); this.container = factory .getEmbeddedServletContainer(exampleServletRegistration()); this.container.start(); ListenableFuture<ClientHttpResponse> response2 = clientHttpRequestFactory .createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET) .executeAsync(); assertThat(response2.get(10, TimeUnit.SECONDS).getRawStatusCode()).isEqualTo(200); clientHttpRequestFactory.destroy(); }
@Test public void restartWithKeepAlive() throws Exception { AbstractEmbeddedServletContainerFactory factory = getFactory(); this.container = factory .getEmbeddedServletContainer(exampleServletRegistration()); this.container.start(); HttpComponentsAsyncClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsAsyncClientHttpRequestFactory(); ListenableFuture<ClientHttpResponse> response1 = clientHttpRequestFactory .createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET) .executeAsync(); assertThat(response1.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200)); this.container.stop(); this.container = factory .getEmbeddedServletContainer(exampleServletRegistration()); this.container.start(); ListenableFuture<ClientHttpResponse> response2 = clientHttpRequestFactory .createAsyncRequest(new URI(getLocalUrl("/hello")), HttpMethod.GET) .executeAsync(); assertThat(response2.get(10, TimeUnit.SECONDS).getRawStatusCode(), equalTo(200)); }
public SleepServerApiClient() throws Exception { connectionManager = new PoolingNHttpClientConnectionManager( new DefaultConnectingIOReactor(IOReactorConfig.DEFAULT)); connectionManager.setMaxTotal(20000); connectionManager.setDefaultMaxPerRoute(20000); RequestConfig config = RequestConfig.custom().setConnectTimeout(120000) .build(); CloseableHttpAsyncClient httpClient = HttpAsyncClientBuilder.create() .setConnectionManager(connectionManager) .setDefaultRequestConfig(config).build(); HttpComponentsAsyncClientHttpRequestFactory requestFactory = new HttpComponentsAsyncClientHttpRequestFactory( httpClient); client = new AsyncRestTemplate(requestFactory); }
public ApiHttpClient(final String channelAccessToken) { RequestConfig requestConfig = RequestConfig.custom() .setConnectionRequestTimeout(timeoutInMillis) .setConnectTimeout(timeoutInMillis) .build(); CloseableHttpAsyncClient asyncClient = HttpAsyncClientBuilder.create() .setDefaultRequestConfig(requestConfig) .addInterceptorLast((HttpRequest httpRequest, HttpContext httpContext) -> { httpRequest.addHeader("X-Line-ChannelToken", channelAccessToken); httpRequest.addHeader("Content-Type", "application/json; charser=UTF-8"); httpRequest.removeHeaders("Accept"); httpRequest.addHeader("Accept", "application/json; charset=UTF-8"); }) .setMaxConnTotal(maxConnections) .setMaxConnPerRoute(maxConnections) .disableCookieManagement() .build(); asyncRestTemplate = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory(asyncClient)); asyncRestTemplate.setErrorHandler(new ApiResponseErrorHandler()); httpHeaders = new HttpHeaders(); httpHeaders.set("X-Line-ChannelToken", channelAccessToken); httpHeaders.setContentType(new MediaType("application", "json", Charset.forName("UTF-8"))); List<MediaType> list = new ArrayList<>(); list.add(new MediaType("application", "json", Charset.forName("UTF-8"))); httpHeaders.setAccept(list); objectMapper = new ObjectMapper(); objectMapper.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); objectMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance())); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); }
@Bean public NIORestClient restClient(){ HttpComponentsAsyncClientHttpRequestFactory rest = new HttpComponentsAsyncClientHttpRequestFactory(); rest.setConnectionRequestTimeout(connectionRequestTimeout); rest.setReadTimeout(readTimeout); rest.setConnectTimeout(connectTimeout); return new NIORestClient(new AsyncRestTemplate(rest)); }
@Bean public AsyncRestTemplate asyncRestTemplateSupportHal(MappingJackson2HttpMessageConverter jackson2ConverterSupportHal) throws Exception { // HttpComponentsAsyncClientHttpRequestFactory internally uses NIO AsyncRestTemplate asyncRestTemplate = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory(asyncHttpClient())); asyncRestTemplate.setMessageConverters( Arrays.asList(jackson2ConverterSupportHal) ); return asyncRestTemplate; }
@Bean RestTemplate restTemplate() { return new RestTemplate(new HttpComponentsAsyncClientHttpRequestFactory()); }
@Bean public AsyncClientHttpRequestFactory clientHttpRequestFactory(CloseableHttpAsyncClient closeableHttpAsyncClient) { return new HttpComponentsAsyncClientHttpRequestFactory(closeableHttpAsyncClient); }
private AsyncRestTemplate buildRestTemplateWithBasicAuth(String username, String password) { HttpAsyncClientBuilder clientBuilder = HttpAsyncClientBuilder.create(); clientBuilder.setDefaultCredentialsProvider(buildBasicAuthProvider(username, password)); CloseableHttpAsyncClient client = clientBuilder.build(); return new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory(client)); }
@Bean public AsyncClientHttpRequestFactory asyncHttpRequestFactory() { return new HttpComponentsAsyncClientHttpRequestFactory(asyncHttpClient()); }