I was referring the below code from a repository(https://github.com/springframeworkguru/mssc-brewery-client/blob/apache-client/src/main/java/guru/springframework/msscbreweryclient/web/config/NIORestTemplateCustomizer.java) for learning purpose. Where config client is using Restemplate to call api which will be build using dependency injection. And there is another class BlockingRestTemplateCustomizer which creates the configuration for resttemplate builder.
But according to the below code how it works and what if BlockingRestTemplateCustomizer bean is created first and then BreweryClient bean is created then as BlockingRestTemplateCustomizer has customize method which expects Resttemplate as dependency?. Whether this will throw exception? and how exactly RestTemplateCustomizer , Resttemplate and RestTemplateBuilder are related(asking this to understand better)?.
And also how to configure the apache-async-htttpclient
for Resttemplate
with connection pool as HttpComponentsAsyncClientHttpRequestFactory
is not there as part of spring 6.
BreweryClient.java
import guru.springframework.msscbreweryclient.web.model.BeerDto;
import guru.springframework.msscbreweryclient.web.model.CustomerDto;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.net.URI;
import java.util.UUID;
/**
* Created by jt on 2019-04-23.
*/
@ConfigurationProperties(prefix = "sfg.brewery", ignoreUnknownFields = false)
@Component
public class BreweryClient {
public final String BEER_PATH_V1 = "/api/v1/beer/";
public final String CUSTOMER_PATH_V1 = "/api/v1/customer/";
private String apihost;
private final RestTemplate restTemplate;
public BreweryClient(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}
public BeerDto getBeerById(UUID uuid){
return restTemplate.getForObject(apihost + BEER_PATH_V1 + uuid.toString(), BeerDto.class);
}
public URI saveNewBeer(BeerDto beerDto){
return restTemplate.postForLocation(apihost + BEER_PATH_V1, beerDto);
}
public void updateBeer(UUID uuid, BeerDto beerDto){
restTemplate.put(apihost + BEER_PATH_V1 + uuid, beerDto);
}
public void deleteBeer(UUID uuid){
restTemplate.delete(apihost + BEER_PATH_V1 + uuid );
}
public void setApihost(String apihost) {
this.apihost = apihost;
}
public CustomerDto getCustomerById(UUID customerId) {
return restTemplate.getForObject(apihost+ CUSTOMER_PATH_V1 + customerId.toString(), CustomerDto.class);
}
public URI saveNewCustomer(CustomerDto customerDto) {
return restTemplate.postForLocation(apihost + CUSTOMER_PATH_V1, customerDto);
}
public void updateCustomer(UUID customerId, CustomerDto customerDto) {
restTemplate.put(apihost + CUSTOMER_PATH_V1 + customerId, customerDto);
}
public void deleteCustomer(UUID customerId) {
restTemplate.delete(apihost + CUSTOMER_PATH_V1 + customerId);
}
}
BlockingRestTemplateCustomizer.java
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
/**
* Created by jt on 2019-08-08.
*/
@Component
public class BlockingRestTemplateCustomizer implements RestTemplateCustomizer {
public ClientHttpRequestFactory clientHttpRequestFactory(){
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(20);
RequestConfig requestConfig = RequestConfig
.custom()
.setConnectionRequestTimeout(3000)
.setSocketTimeout(3000)
.build();
CloseableHttpClient httpClient = HttpClients
.custom()
.setConnectionManager(connectionManager)
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
.setDefaultRequestConfig(requestConfig)
.build();
return new HttpComponentsClientHttpRequestFactory(httpClient);
}
@Override
public void customize(RestTemplate restTemplate) {
restTemplate.setRequestFactory(this.clientHttpRequestFactory());
}
}