Giriş
Şu satırı dahil ederiz
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
Açıklaması şöyle. Yani bu sınıf altta HTTP nesnelerini havuzda tutar. Bir nevi Hikari Connection Pool gibidir.
Spring uses connection instances to connect external components using HTTP. Spring uses a connection pool to reuse them. Creating a connection for an HTTP request is an expensive process.
Altta Apache HttpComponents HttpClient sınıfını kullanır. Açıklaması şöyle.
Bu sınıfı kardeşi SimpleClientHttpRequestFactory sınıfıdır.
ClientHttpRequestFactory implementation that uses Apache HttpComponents HttpClient to create requests.
constructor metodu
Örnek
Şöyle yaparız
setConnectTimeout metodu
Şöyle yaparız.
TrustStrategy acceptingTrustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(java.security.cert.X509Certificate[] chain, String authType)
throws java.security.cert.CertificateException {
// TODO Auto-generated method stub
return true;
}
};
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
HttpClient httpClient;
httpClient = HttpClients.custom().setSSLSocketFactory(csf).setProxy(
new HttpHost(httpProxy, httpPort)).build();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-type", "text/xml");
httpHeaders.add("Accept", "text/xml");
httpHeaders.add("access-control-allow-origin", "*");
httpHeaders.add("content-encoding", "UTF-8");
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(timeout);
Örnek
ConnectionPool tanımlamak için şöyle yaparız
public RestTemplate createRestTemplateSslWithPooling() throws IOException,
CertificateException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()).build();SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext);Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("https", socketFactory).build();PoolingHttpClientConnectionManager poolingConnManager =
new PoolingHttpClientConnectionManager(socketFactoryRegistry);poolingConnManager.setMaxTotal(50);poolingConnManager.setDefaultMaxPerRoute(50);CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).setConnectionManager(poolingConnManager).build();HttpComponentsClientHttpRequestFactory factory =
new HttpComponentsClientHttpRequestFactory(httpClient);return new RestTemplate(factory);}
setConnectionRequestTimeout metodu
Havuzdan Http nesnesi almak için beklenecek süreyi belirtir
Örnek
Şöyle yaparız
RestTemplate restTemplate = new RestTemplate();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
// Set the connectionRequestTimeout value to 10 seconds
requestFactory.setConnectionRequestTimeout(10_000);
restTemplate.setRequestFactory(requestFactory);
Şöyle yaparız.
var requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setConnectTimeout(20_000);
requestFactory.setConnectionRequestTimeout(20_000);
requestFactory.setReadTimeout(30000);
Hiç yorum yok:
Yorum Gönder