Class HttpComponentsMessageSender

All Implemented Interfaces:
org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, WebServiceMessageSender

public class HttpComponentsMessageSender extends AbstractHttpWebServiceMessageSender implements org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean
WebServiceMessageSender implementation that uses Apache HttpClient to execute POST requests.

Allows to use a pre-configured HttpClient instance, potentially with authentication, HTTP connection pooling, etc. Authentication can also be set by injecting a Credentials instance (such as the UsernamePasswordCredentials).

Alan Stewart, Barry Pitman, Arjen Poutsma, Greg Turnquist
See Also:
  • HttpClient
  • Constructor Details

    • HttpComponentsMessageSender

      public HttpComponentsMessageSender()
      Create a new instance of the HttpClientMessageSender with a default HttpClient that uses a default PoolingClientConnectionManager.
    • HttpComponentsMessageSender

      public HttpComponentsMessageSender(org.apache.http.client.HttpClient httpClient)
      Create a new instance of the HttpClientMessageSender with the given HttpClient instance.

      This constructor does not change the given HttpClient in any way. As such, it does not set timeouts, nor does it add the HttpComponentsMessageSender.RemoveSoapHeadersInterceptor.

      httpClient - the HttpClient instance to use for this sender
  • Method Details

    • setCredentials

      public void setCredentials(org.apache.http.auth.Credentials credentials)
      Sets the credentials to be used. If not set, no authentication is done.
      See Also:
      • UsernamePasswordCredentials
      • NTCredentials
    • getHttpClient

      public org.apache.http.client.HttpClient getHttpClient()
      Returns the HttpClient used by this message sender.
    • setHttpClient

      public void setHttpClient(org.apache.http.client.HttpClient httpClient)
      Set the HttpClient used by this message sender.
    • setConnectionTimeout

      public void setConnectionTimeout(int timeout)
      Sets the timeout until a connection is established. A value of 0 means never timeout.
      timeout - the timeout value in milliseconds
      See Also:
      • HttpConnectionParams.setConnectionTimeout(org.apache.http.params.HttpParams, int)
    • setReadTimeout

      public void setReadTimeout(int timeout)
      Set the socket read timeout for the underlying HttpClient. A value of 0 means never timeout.
      timeout - the timeout value in milliseconds
      See Also:
      • HttpConnectionParams.setSoTimeout(org.apache.http.params.HttpParams, int)
    • setMaxTotalConnections

      public void setMaxTotalConnections(int maxTotalConnections)
      Sets the maximum number of connections allowed for the underlying HttpClient.
      maxTotalConnections - the maximum number of connections allowed
      See Also:
      • PoolingClientConnectionManager.setMaxTotal(int)
    • setMaxConnectionsPerHost

      public void setMaxConnectionsPerHost(Map<String,String> maxConnectionsPerHost) throws URISyntaxException
      Sets the maximum number of connections per host for the underlying HttpClient. The maximum number of connections per host can be set in a form accepted by the java.util.Properties class, like as follows:

      The host can be specified as a URI (with scheme and port).

      maxConnectionsPerHost - a properties object specifying the maximum number of connection
      See Also:
      • PoolingClientConnectionManager.setMaxPerRoute(HttpRoute, int)
    • setAuthScope

      public void setAuthScope(org.apache.http.auth.AuthScope authScope)
      Sets the authentication scope to be used. Only used when the credentials property has been set.

      By default, the AuthScope.ANY is used.

      See Also:
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • createConnection

      public WebServiceConnection createConnection(URI uri) throws IOException
      Description copied from interface: WebServiceMessageSender
      Create a new WebServiceConnection to the specified URI.
      Specified by:
      createConnection in interface WebServiceMessageSender
      uri - the URI to open a connection to
      the new connection
      IOException - in case of I/O errors
    • createContext

      protected org.apache.http.protocol.HttpContext createContext(URI uri)
      Template method that allows for creation of a HttpContext for the given uri. Default implementation returns null.
      uri - the URI to create the context for
      the context, or null
    • destroy

      public void destroy() throws Exception
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean