Enabling SSL Context for asyncRestTemplate

In order to enable asynchronous HTTP calls, we have AsyncRestTemplate.This makes writing asynchronous code as easy as synchronizing code.

We need to initiliaze asyncRestTemplate object like we have done in this post http://thebadengineer.com/asynchronous-calling/.
But sometimes we need to enable SSL for HTTPs site and also want to manage certificates for that.So the code is as belows:

                X509TrustManager testManager = TrustManagerUtils.getAcceptAllTrustManager();
                // Create a trust manager that does not validate certificate chains
                TrustManager[] trustManagers = new TrustManager[]{testManager};
                SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
                sslContext.init(null, trustManagers, null);

Now, we will need RequestConfig to set up timeout properties like below:

 RequestConfig requestConfig = RequestConfig.custom()
                        .setConnectionRequestTimeout(timeoutnumber)
                        .setConnectTimeout(connectionnumber)
                        .build();

we will use HttpAsyncClientBuilder.create() method to create the client.

import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.springframework.web.client.AsyncRestTemplate;

CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
                        .setDefaultRequestConfig(requestConfig)
  	                    .setSSLContext(sslContext)
                        .build();
//we can also use .setMaxConnTotal() and various other properties to configure

Finally pass the client while initializing the object like below:

asyncRestTemplate = new AsyncRestTemplate(new HttpComponentsAsyncClientHttpRequestFactory(client));

This asyncRestTemplate can be used to call different APIs over SSL context.

More info: https://www.baeldung.com/java-7-tls-v12
https://docs.spring.io/spring/docs/4.2.3.RELEASE_to_4.2.4.RELEASE/Spring%20Framework%204.2.4.RELEASE/org/springframework/web/client/AsyncRestTemplate.html