2

I am making REST calls using Spring Integration as below.

<int:gateway id="ServiceRequestGateway"
                 service-interface="com.company.security.integration.RequestGateway"
                 default-request-channel="RequestChannel"
                 default-reply-channel="ResponseChannel">
        <int:default-header name="Accept" value="application/json; v=5"/>
        <int:default-header name="Content-Type" value="application/json; v=5"/>
        <int:default-header name="ServiceType" expression="#args[1]"/>
    </int:gateway>

    <int-http:outbound-gateway
            id="Outbound_Gateway"
            request-channel="RequestChannel"
            reply-channel="ResponseChannel"
            header-mapper="headerMapper"
            url="${service.host}/{xyzServiceType}"
            http-method="POST"
            expected-response-type="java.lang.String"
            extract-request-payload="true">
        <int-http:uri-variable name="ServiceType" expression="headers['xyzServiceType']" />
    </int-http:outbound-gateway>

However there are lot of ssl handshake happening every call over the network. How can I reuse a connection and make multiple REST requests? Is it possible to use keep-alive?

Update-1

Added use of HttpComponentsClientHttpRequestFactory.

<int-http:outbound-gateway
        id="NPI_Tokenization_Outbound_Gateway"
        request-channel="NPITokenizationRequestChannel"
        reply-channel="ResponseChannel"
        header-mapper="headerMapper"
        url="${npi.turing.host}/{npiTuringType}"
        http-method="POST"
        expected-response-type="java.lang.String"
        extract-request-payload="true">
    <int-http:uri-variable name="npiTuringType" expression="headers['npiTuringType']" />
</int-http:outbound-gateway>

<bean id="requestFactory"
      class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
    <property name="connectTimeout" value="5000"/>
    <property name="readTimeout"    value="5000"/>
</bean>
Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94

1 Answers1

2

Consider to use HttpComponentsClientHttpRequestFactory instead of default SimpleClientHttpRequestFactory. In the Apache HTTP Client you can find enough options for connection management, including keep-alive: https://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118
  • See [this answer](http://stackoverflow.com/questions/35185025/default-keep-alive-time-for-a-httpconnection-when-using-spring-rest-template) for more links. – Gary Russell Mar 08 '17 at 15:25
  • Thank you, I added HttpComponentsClientHttpRequestFactory (updated my question above), however I don't see a property/example on how to configure keep-alive in context of Spring integration – Himalay Majumdar Mar 08 '17 at 15:37
  • It's not Spring Integration responsibility already. You have to add `request-factory` reference from the ``. Everything rest is up to `HttpComponentsClientHttpRequestFactory` configuration. Seems for me we gave you enough links to read on the matter... – Artem Bilan Mar 08 '17 at 15:39