0

I use Spring Boot and created a service (part of Microservices Design). I have the below method,

public static HttpClient getHttpClient() throws KeyManagementException, NoSuchAlgorithmException {

        log.info("Creating Http Client...");
        final SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                new SSLContextBuilder().build());
        final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
        connectionManager.setMaxTotal(200);
        connectionManager.setDefaultMaxPerRoute(20);

        return HttpClients.custom()
                .disableRedirectHandling()
                .setSSLSocketFactory(sslConnectionSocketFactory)
                .setConnectionManager(connectionManager)
                .build();
    }

I invoke this method multiple times, wanted to maintain only one instance and reuse it once created. Considering concurrent programming, can i use Singleton Pattern? and i see RestTemplate is pretty good approach rather than Apache Http Client as per below link,

RestTemplate vs Apache Http Client for production code in spring project

Suggestion are much appreciated.

Florian
  • 2,796
  • 1
  • 15
  • 25
parrotjack
  • 432
  • 1
  • 6
  • 18

2 Answers2

1

I would suggest you to use spring RestTemplate. Instantiate RestTemplate instance only once in your application and then use it in multiple service/component classes using dependency injection.

The best way to create an instance of RestTemplate is to register it as a spring bean in the spring configuration class. This will create instance of RestTemplate at the time of application start up. Below code, will create a single instance of RestTemplate and it can shared among multiple classes.

@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
//if you want to set any properties in RestTemplate, set here
return restTemplate;
}

Now to use RestTemplate in any of the service class, use dependency injection:

@Service
class TestService {

@Autowired
private RestTemplate restTemplate

public void invokeRemoteService(){
//Here you are using restTemplate 
  String response = 
      restTemplate.postForObject(url, request, String.class);
}

}
Reena Upadhyay
  • 1,977
  • 20
  • 35
0

First of all you should use the Dependency Injection of the spring core instead of using a method directly to get an instance working in Spring. The Dependency injection(DI)container will generate the required instance for you and inject it. With this you can configure the DI-Container how often an instance will be generated by setting a scope or using special annotations. A good explanation with code examples for DI in Spring with custom scopes can be found here: https://www.baeldung.com/spring-bean-scopes

Secondly i would recommend using the Spring RestTemplate and configure it for your needs via a RestTemplateBuilder or RestTemplateCustomizer https://www.baeldung.com/spring-rest-template-builder With the RestTemplate spring provides already a lot of Test setup classes to make JUnit testing easy out of the box and using the spring provided option is recommended if you are already working inside the spring framework.

Andi
  • 131
  • 1
  • 10