Class RestTemplateHelper

java.lang.Object
fr.becpg.repo.helper.RestTemplateHelper

public final class RestTemplateHelper extends Object
Helper class for RestTemplate configuration with optimized connection pooling, DNS cache management, and DNS resolution logging.
Author:
matthieu
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
    Custom request factory that logs DNS resolution and adds standard headers.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final org.apache.hc.core5.util.Timeout
    Constant CONNECTION_TIMEOUT
    private static final int
    Constant CONNECTION_TTL_SECONDS=5
    private static final org.apache.hc.core5.util.Timeout
    Constant DEFAULT_SOCKET_TIMEOUT
    private static final org.springframework.web.client.RestTemplate
    Constant defaultRestTemplate
    private static final org.slf4j.Logger
    Constant logger
    private static final org.apache.hc.core5.util.Timeout
    Constant LONG_SOCKET_TIMEOUT
    private static final org.springframework.web.client.RestTemplate
    Constant longTimeoutRestTemplate
    private static final int
    Constant MAX_CONNECTIONS_PER_ROUTE=20
    private static final int
    Constant MAX_TOTAL_CONNECTIONS=50
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Constructor for RestTemplateHelper.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static org.apache.hc.client5.http.config.ConnectionConfig
    buildConnectionConfig(org.apache.hc.core5.util.Timeout socketTimeout)
    Builds connection configuration with specified timeouts.
    static String[]
    Performs manual DNS resolution check for a hostname.
    private static org.apache.hc.client5.http.impl.classic.CloseableHttpClient
    createHttpClient(org.apache.hc.core5.util.Timeout socketTimeout)
    Creates a configured HTTP client with connection pooling and timeout settings.
    private static org.springframework.web.client.RestTemplate
    createRestTemplate(org.apache.hc.core5.util.Timeout socketTimeout)
    Creates a configured RestTemplate with the specified socket timeout.
    static org.springframework.web.client.RestTemplate
    Gets the default RestTemplate instance with standard timeout settings.
    static org.springframework.web.client.RestTemplate
    Gets the RestTemplate instance with extended timeout settings for long-running operations.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
      Constant logger
    • MAX_TOTAL_CONNECTIONS

      private static final int MAX_TOTAL_CONNECTIONS
      Constant MAX_TOTAL_CONNECTIONS=50
      See Also:
    • MAX_CONNECTIONS_PER_ROUTE

      private static final int MAX_CONNECTIONS_PER_ROUTE
      Constant MAX_CONNECTIONS_PER_ROUTE=20
      See Also:
    • CONNECTION_TTL_SECONDS

      private static final int CONNECTION_TTL_SECONDS
      Constant CONNECTION_TTL_SECONDS=5
      See Also:
    • CONNECTION_TIMEOUT

      private static final org.apache.hc.core5.util.Timeout CONNECTION_TIMEOUT
      Constant CONNECTION_TIMEOUT
    • DEFAULT_SOCKET_TIMEOUT

      private static final org.apache.hc.core5.util.Timeout DEFAULT_SOCKET_TIMEOUT
      Constant DEFAULT_SOCKET_TIMEOUT
    • LONG_SOCKET_TIMEOUT

      private static final org.apache.hc.core5.util.Timeout LONG_SOCKET_TIMEOUT
      Constant LONG_SOCKET_TIMEOUT
    • defaultRestTemplate

      private static final org.springframework.web.client.RestTemplate defaultRestTemplate
      Constant defaultRestTemplate
    • longTimeoutRestTemplate

      private static final org.springframework.web.client.RestTemplate longTimeoutRestTemplate
      Constant longTimeoutRestTemplate
  • Constructor Details

    • RestTemplateHelper

      private RestTemplateHelper()

      Constructor for RestTemplateHelper.

  • Method Details

    • getRestTemplate

      public static org.springframework.web.client.RestTemplate getRestTemplate()
      Gets the default RestTemplate instance with standard timeout settings.
      Returns:
      configured RestTemplate instance
    • getRestTemplateLongTimeout

      public static org.springframework.web.client.RestTemplate getRestTemplateLongTimeout()
      Gets the RestTemplate instance with extended timeout settings for long-running operations.
      Returns:
      configured RestTemplate instance with long timeout
    • createRestTemplate

      private static org.springframework.web.client.RestTemplate createRestTemplate(org.apache.hc.core5.util.Timeout socketTimeout)
      Creates a configured RestTemplate with the specified socket timeout.
      Parameters:
      socketTimeout - the socket timeout to apply
      Returns:
      configured RestTemplate instance
    • createHttpClient

      private static org.apache.hc.client5.http.impl.classic.CloseableHttpClient createHttpClient(org.apache.hc.core5.util.Timeout socketTimeout)
      Creates a configured HTTP client with connection pooling and timeout settings.
      Parameters:
      socketTimeout - the socket timeout to apply
      Returns:
      configured CloseableHttpClient
    • buildConnectionConfig

      private static org.apache.hc.client5.http.config.ConnectionConfig buildConnectionConfig(org.apache.hc.core5.util.Timeout socketTimeout)
      Builds connection configuration with specified timeouts.
      Parameters:
      socketTimeout - the socket timeout to apply
      Returns:
      configured ConnectionConfig
    • checkDnsResolution

      public static String[] checkDnsResolution(String hostname)
      Performs manual DNS resolution check for a hostname.
      Parameters:
      hostname - the hostname to resolve
      Returns:
      array of resolved IP addresses, or empty array if resolution fails